This circuit consists of an Arduino Nano microcontroller, an APDS-9960 RGB and Gesture Sensor, and a Micro Servo 9G. The Arduino Nano reads gesture inputs from the APDS-9960 sensor and controls the position of the Micro Servo based on specific gesture patterns.
#include <Wire.h>
#include <Adafruit_APDS9960.h>
#include <Servo.h>
Adafruit_APDS9960 apds;
Servo myservo;
const int servoPin = 9;
int upCount = 0;
int leftCount = 0;
void setup() {
Serial.begin(9600);
Wire.begin();
myservo.attach(servoPin);
myservo.write(0); // Initialize servo to 0°
if (!apds.begin()) {
Serial.println("Failed to initialize APDS-9960 sensor!");
while (1);
}
apds.enableGesture(true);
}
void loop() {
if (apds.gestureValid()) {
int gesture = apds.readGesture();
switch (gesture) {
case APDS9960_UP:
upCount++;
break;
case APDS9960_DOWN:
myservo.write(0); // Reset servo to 0°
upCount = 0;
leftCount = 0;
break;
case APDS9960_LEFT:
leftCount++;
break;
case APDS9960_RIGHT:
// No action for RIGHT gesture
break;
}
if (upCount == 2 && leftCount == 1) {
myservo.write(90); // Rotate servo to 90°
upCount = 0;
leftCount = 0;
}
}
}
Libraries Included:
Wire.h
: For I2C communication.Adafruit_APDS9960.h
: For interfacing with the APDS-9960 sensor.Servo.h
: For controlling the servo motor.Global Variables:
Adafruit_APDS9960 apds;
: Object for the APDS-9960 sensor.Servo myservo;
: Object for the servo motor.const int servoPin = 9;
: Pin number for the servo motor.int upCount = 0;
: Counter for UP gestures.int leftCount = 0;
: Counter for LEFT gestures.Setup Function:
Loop Function: