Pada kesempatan kali ini saya akan menjelakan mengenai bagaimana cara membuat sebuah alat untuk kendali dua buah servo dengan menggunakan gesture / gerakan tangan atau jari, sensor gesture yang dipakai APDS 9960. alat ini menggunakan Arduino dan servo SG90. untuk lebih jelasnya berikut adalah daftar komponen dan programnya.
a. Arduino Uno
b. Sensor Gesture APDS 9960
c. Servo SG90
d. Program Arduino IDE
#include <Wire.h>
#include <Servo.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;
Servo myservo1;
Servo myservo2;
void setup() {
myservo1.attach(9);
myservo2.attach(10);
myservo1.write(100);
myservo2.write(100);
// 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);
}
}
void interruptRoutine() {
isr_flag = 1;
}
void handleGesture() {
if ( apds.isGestureAvailable() ) {
switch ( apds.readGesture() ) {
case DIR_UP:
Serial.println("UP");
myservo1.write(150);
myservo2.write(150);
break;
case DIR_DOWN:
Serial.println("DOWN");
myservo1.write(50);
myservo2.write(50);
break;
case DIR_LEFT:
Serial.println("LEFT");
myservo1.write(50);
myservo2.write(50);
break;
case DIR_RIGHT:
Serial.println("RIGHT");
myservo1.write(150);
myservo2.write(150);
break;
case DIR_NEAR:
Serial.println("NEAR");
myservo1.write(100);
myservo2.write(100);
break;
case DIR_FAR:
Serial.println("FAR");
break;
default:
Serial.println("NONE");
}
}
}
#include <Servo.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;
Servo myservo1;
Servo myservo2;
void setup() {
myservo1.attach(9);
myservo2.attach(10);
myservo1.write(100);
myservo2.write(100);
// 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);
}
}
void interruptRoutine() {
isr_flag = 1;
}
void handleGesture() {
if ( apds.isGestureAvailable() ) {
switch ( apds.readGesture() ) {
case DIR_UP:
Serial.println("UP");
myservo1.write(150);
myservo2.write(150);
break;
case DIR_DOWN:
Serial.println("DOWN");
myservo1.write(50);
myservo2.write(50);
break;
case DIR_LEFT:
Serial.println("LEFT");
myservo1.write(50);
myservo2.write(50);
break;
case DIR_RIGHT:
Serial.println("RIGHT");
myservo1.write(150);
myservo2.write(150);
break;
case DIR_NEAR:
Serial.println("NEAR");
myservo1.write(100);
myservo2.write(100);
break;
case DIR_FAR:
Serial.println("FAR");
break;
default:
Serial.println("NONE");
}
}
}
e. VIDEO HASILNYA
No comments:
Post a Comment