Back to Arduino Step By Step Course

Lesson 100: Control Servo Motor with your hand gesture

If you don't like to see ads while video is being played you can purchase this course for $200 from Udemy or purchase YouTube premium Here

Part 9: Servo Motors

In This lesson we learn how to control a servo motor using hand gesture. For each gesture we could take action to move servo motor to a sertain angle or a full motion. We've used APDS-9960 Gesture sensor and a library is needed to be installed on Arduino IDE. Just copy this code below, open new project by clicking on File->New and then save and download it your Arduion. You need computer only once to send this program to Arduino board. then you don't need it. Just power up using a battery, power adapter or USB cable and it will work. for Servo motor if you are using the small SG90 servo, you can power it up using Arduino. But for Larger motors, make sure to provide power from independant battery or power source as Arduino can't supply enough power to larger Servo Motors.

Gestures detected


 /*
 * Arduio 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 motor in any way we want
 * video details: https://youtu.be/BUSqppjNw-Y
 * Code written based on sampel from this code https://github.com/sparkfun/APDS-9960_RGB_and_Gesture_Sensor
 * on Feb 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 download 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
        
   

The least I expect from you is to thumb up the video and subscribe to my channel. I appriciate that. .I have spent months making these lectures and writing code. You don't lose anything by subscribging to my channel. Your subscription is stamp of approval to my videos and more people can find them and in it turn it helps me. Thank you

If you found this tutorial helpful, please support me so I can continue creating content like this. support me via PayPal

**** AFFILIATE PROGRAM **** We are a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for us to earn fees by linking to Amazon.com and affiliated sites.