Lesson 100: Control a Servo Motor with Your Hand Gestures
In this lesson, we learn how to control a servo motor using hand gestures. For each gesture, we can take action to move the servo motor to a certain angle or a full motion. We've used an APDS-9960 gesture sensor, and a library needs to be installed on the Arduino IDE. Just copy the code below, open a new project by clicking on File>New, and then save and download it to your Arduino. You need a computer only once to send this program to the Arduino board; then you don't need it. Just power it up using a battery, power adapter, or USB cable, and it will work. For the servo motor, if you are using the small SG90 servo, you can power it up using the Arduino. But for larger motors, make sure to provide power from an independent battery or power source, as the Arduino cannot supply enough power to larger servo motors.
/*
* Arduino Step by Step Course by Robojax
* Lesson 100: Control Servo Motor with your hand gesture
* Using hand gesture RIGHT, LEFT, UP, DOWN, FAR, NEAR we control the motor in any way we want
* video details: https://youtu.be/BUSqppjNw-Y
* Code written based on sample from this code https://github.com/sparkfun/APDS-9960_RGB_and_Gesture_Sensor
* on February 13, 2022 by Ahmad Shamshiri.
*
* This code is part of Arduino Step by Step Course which starts here: https://youtu.be/-6qSrDUA5a8
*
* for library of this code visit http://robojax.com/
*
If you found this tutorial helpful, please support me so I can continue creating
content like this. Make a donation using PayPal by credit card https://bit.ly/donate-robojax
* This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.*
* This code has been downloaded from Robojax.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
APDS-9960 RGB and Gesture Sensor
Shawn Hymel @ SparkFun Electronics
May 30, 2014
https://github.com/sparkfun/APDS-9960_RGB_and_Gesture_Sensor
Tests the gesture sensing abilities of the APDS-9960. Configures
APDS-9960 over I2C and waits for gesture events. Calculates the
direction of the swipe (up, down, left, right) and displays it
on a serial console.
To perform a NEAR gesture, hold your hand
far above the sensor and move it close to the sensor (within 2
inches). Hold your hand there for at least 1 second and move it
away.
To perform a FAR gesture, hold your hand within 2 inches of the
sensor for at least 1 second and then move it above (out of
range) of the sensor.
Hardware Connections:
IMPORTANT: The APDS-9960 can only accept 3.3V!
Arduino Pin APDS-9960 Board Function
3.3V VCC Power
GND GND Ground
A4 SDA I2C Data
A5 SCL I2C Clock
2 INT Interrupt
Resources:
Include Wire.h and SparkFun_APDS-9960.h
This code is beerware; if you see me (or any other SparkFun
employee) at the local, and you've found our code helpful, please
buy us a round!
Distributed as-is; no warranty is given.
****************************************************************/
#include <Wire.h>
#include <Servo.h>
#include <SparkFun_APDS9960.h>
//see details in this video: https://youtu.be/BUSqppjNw-Y
int servoPin =9;
int downAngle =135;
int upAngle = 45;
int rightAngle = 0;
int leftAngle = 180;
int angleStep =2;
int angle =90; //initial Angle
const int minAngle = 0;
const int maxAngle = 180;
Servo myservo; // create servo object to control a servo
// Pins
#define APDS9960_INT 2 // Needs to be an interrupt pin
// Constants
// Global Variables
SparkFun_APDS9960 apds = SparkFun_APDS9960();
int isr_flag = 0;
void setup() {
myservo.attach(servoPin); // attaches the servo on pin 9 to the servo object
// Set interrupt pin as input
pinMode(APDS9960_INT, INPUT);
// Initialize Serial port
Serial.begin(9600);
Serial.println();
Serial.println(F("--------------------------------"));
Serial.println(F("SparkFun APDS-9960 - 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");
myservo.write(upAngle);
break;
case DIR_DOWN:
Serial.println("DOWN");
myservo.write(downAngle);
break;
case DIR_LEFT:
Serial.println("LEFT");
myservo.write(leftAngle);
break;
case DIR_RIGHT:
Serial.println("RIGHT");
myservo.write(rightAngle);
break;
case DIR_NEAR:
Serial.println("NEAR");
nearAction();
break;
case DIR_FAR:
Serial.println("FAR");
farAction();
break;
default:
Serial.println("NONE");
}
}
}
void nearAction()
{
for (angle = minAngle; angle <= maxAngle; angle += angleStep) {
// in steps of angleStep degree
myservo.write(angle);
delay(15);
}
for (angle = maxAngle; angle >= minAngle; angle -= angleStep) {
myservo.write(angle);
delay(15);
}
}//nearAction() ends
void farAction()
{
//do something here
}//farAction() ends
资源与参考
尚无可用资源。
文件📁
没有可用的文件。