Robotics Arduino
Published © GPL3+

Arduino Menu with APDS 9960 Gesture Sensor Control

Learn how to create your gesture control for Arduino Menu,

BeginnerFull instructions provided1 hour5,752

Things used in this project

Hardware components

APDS-9960 Gesture Sensor
×1
Breadboard (generic)
Breadboard (generic)
×1
Arduino Nano R3
Arduino Nano R3
×1
Jumper wires (generic)
Jumper wires (generic)
×1
LED (generic)
LED (generic)
×2
RGB Backlight LCD - 16x2
Adafruit RGB Backlight LCD - 16x2
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Electronic Schematic of the Project

Code

Source Code

Arduino
#include "SoftwareSerial.h"
#include <LiquidCrystal_I2C.h> //Biblioteca I2C do LCD 16x2
#include "Adafruit_APDS9960.h" //INCLUSÃO DE BIBLIOTECA

Adafruit_APDS9960 apds; //OBJETO DO TIPO Adafruit_APDS9960 (I2C)

LiquidCrystal_I2C lcd(0x27,16,2);  //Configurando o endereco do LCD 16x2 para 0x27
SoftwareSerial mySoftwareSerial(10, 11); // RX, TX

byte count = 2;

void setup() 
{
  Serial.begin(9600); //INICIALIZA A SERIAL  
  Wire.begin(); //Inicializacao da Comunicacao I2C

  lcd.init();                      //Inicializacao do LCD
  lcd.backlight();

  for(byte i = 2; i <=4; i++)
  {
    pinMode(i, OUTPUT);
  }
  
  if(!apds.begin())
  { //SE O SENSOR NÃO FOR INICIALIZADO, FAZ
    Serial.println("Falha ao inicializar o dispositivo. Verifique as conexões!"); //IMPRIME O TEXTO NO MONITOR SERIAL
  }//SENÃO, FAZ
  else 
    Serial.println("Dispositivo inicializado!"); //IMPRIME O TEXTO NO MONITOR SERIAL

  //O MODO DETECÇÃO DE GESTO SERÁ HABILITADO QUANDO O SENSOR DETECTAR ALGO PRÓXIMO DO MÓDULO (APROXIME SEU DEDO E AFASTE)
  apds.enableProximity(true);
  apds.enableGesture(true);

  show_menu(count);
}

void loop()
{
    uint8_t gesture = apds.readGesture(); //FAZ A LEITURA DA DIREÇÃO DO GESTO

          //Aumenta o Volume
          if(gesture == APDS9960_UP)
          {
           count++;
           
           if(count > 4)
           {
            count = 4; 
           }
           
           show_menu(count);
          }
          
          //Retorna para a musica anterior
          if(gesture == APDS9960_LEFT)
          {
           if(count == 2)
            {
              digitalWrite(2, LOW);  
            }

            if(count == 3)
            {
              digitalWrite(3, LOW);  
            }

            if(count == 4)
            {
              digitalWrite(2, LOW);  
              digitalWrite(3, LOW);  
            }
          }
          
          //Avança para a proxima musica
          if(gesture == APDS9960_RIGHT)
          {
            if(count == 2)
            {
              digitalWrite(2, HIGH);  
            }

            if(count == 3)
            {
              digitalWrite(3, HIGH);  
            }

            if(count == 4)
            {
              digitalWrite(2, HIGH);  
              digitalWrite(3, HIGH);  
            }
          }
                    
          //Diminui volume
          if(gesture == APDS9960_DOWN)
          {
           count--;

           if(count < 2)
           {
            count = 2;
           }

           show_menu(count);
          }
}

void show_menu(byte option)
{
  if(option == 2)
  {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("-> LED1");
    lcd.setCursor(0,1);
    lcd.print("   LED2");

    return;
  }

  if(option == 3)
  {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("   LED1");
    lcd.setCursor(0,1);
    lcd.print("-> LED2");

    return;
  }

  if(option == 4)
  {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("-> LED1 and LED2");

    return;
  }
  
  return;
}

Credits

Robotics Arduino
42 projects • 66 followers
Contact

Comments

Please log in or sign up to comment.