Translate

KENDALI KIPAS ANGIN / FAN MENGGUNAKAN GESTURE TANGAN SENSOR APDS 9960

KENDALI KIPAS ANGIN / FAN MENGGUNAKA GESTURE TANGAN SENSOR APDS 9960 

            Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara untuk membuat sebuah alat yang bisa mengendalikan kipas angin menggunakan gesture tangan, jadi gesture tangan yang diberikan hanya swap ke kanan dan swap ke kiri untuk mengendalikan on/off kipas angin, sensor yang dipakai yaitu apds9960. untuk lebih jelasnya berikut adalah koding dan komponenya.

 

a. Arduino Uno


 

b. Sensor APDS9960


 

c. Modul Relay




d. Skema 




 

e. Program Arduino IDE 

#include <Wire.h>
#include <SparkFun_APDS9960.h>

// Pins
#define APDS9960_INT    2 // Needs to be an interrupt pin

// Constants

// Global Variables
SparkFun_APDS9960 apds = SparkFun_APDS9960();
int isr_flag = 0;

int speedx;

void setup() {
 
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
 
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(7, HIGH);
  digitalWrite(8, HIGH);
  digitalWrite(9, HIGH);
  digitalWrite(10, HIGH);

  // Set interrupt pin as input
  pinMode(APDS9960_INT, INPUT);

  // Initialize Serial port
  Serial.begin(9600);
  Serial.println();
  Serial.println(F("--------------------------------"));
  Serial.println(F("Y-GESTURE APDS - GestureTest"));
  Serial.println(F("--------------------------------"));
 
  // Initialize interrupt service routine
  attachInterrupt(0, interruptRoutine, FALLING);

  // Initialize APDS-9960 (configure I2C and initial values)
  if ( apds.init() ) {
    Serial.println(F("APDS-9960 initialization complete"));
  } else {
    Serial.println(F("Something went wrong during APDS-9960 init!"));
  }
 
 //  Start running the APDS-9960 gesture sensor engine
  if ( apds.enableGestureSensor(true) ) {
    Serial.println(F("Gesture sensor is now running"));
  } else {
    Serial.println(F("Something went wrong during gesture sensor init!"));
  }
}

void loop() {
  if( isr_flag == 1 ) {
    detachInterrupt(0);
    handleGesture();
    isr_flag = 0;
    attachInterrupt(0, interruptRoutine, FALLING);
  }

if(speedx == 0){
        digitalWrite(3, HIGH);
        digitalWrite(5, HIGH);
        digitalWrite(6, HIGH);
        digitalWrite(7, HIGH);
  }

if(speedx == 1){
        digitalWrite(3, HIGH);
        digitalWrite(5, LOW);
        digitalWrite(6, HIGH);
        digitalWrite(7, HIGH);
  }

if(speedx == 2){
        digitalWrite(3, HIGH);
        digitalWrite(5, HIGH);
        digitalWrite(6, LOW);
        digitalWrite(7, HIGH);
  }  

if(speedx == 3){
        digitalWrite(3, HIGH);
        digitalWrite(5, HIGH);
        digitalWrite(6, HIGH);
        digitalWrite(7, LOW);
  }  


if(speedx > 3){  
  speedx = 3;
}

if(speedx < 0){  
  speedx = 0;
}
}

void interruptRoutine() {
  isr_flag = 1;
}

void handleGesture() {
    if ( apds.isGestureAvailable() ) {
    switch ( apds.readGesture() ) {
      case DIR_UP:
        Serial.println("ATAS");
        speedx++;
        break;
        
      case DIR_DOWN:
        Serial.println("BAWAH");
        speedx--;        
        break;
        
      case DIR_LEFT:
        Serial.println("KIRI");
        digitalWrite(4, HIGH);
        digitalWrite(3, HIGH);
        digitalWrite(5, HIGH);
        digitalWrite(6, HIGH);
        digitalWrite(7, HIGH);
        break;
        
      case DIR_RIGHT:
        Serial.println("KANAN");

        digitalWrite(4, LOW);
        digitalWrite(3, HIGH);
        digitalWrite(5, LOW);
        digitalWrite(6, HIGH);
        digitalWrite(7, HIGH);
        break;
        
      case DIR_NEAR:
        Serial.println("NEAR");

        break;
      case DIR_FAR:
        Serial.println("FAR");

        break;
      default:
        Serial.println("NONE");
    }
  }
}

  

 

 f. VIDEO HASILNYA




No comments:

Post a Comment