Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Jai Krishna
Published © GPL3+

Gesture Based Media Control using Nano 33 BLE SENSE

Learn how to convert your Nano 33 BLE SENSE into a HID device and send keyboard commands to your computer.

BeginnerFull instructions provided2,046
Gesture Based Media Control using Nano 33 BLE SENSE

Things used in this project

Hardware components

Arduino Nano 33 BLE Sense
Arduino Nano 33 BLE Sense
×1

Story

Read more

Code

Gesture Based Media Controller

Arduino
/* 
Author: Bandi Jai Krishna
Date: 25/06/2021

Summary
The following sketch is for the Arduino Nano 33 BLE SENSE board, it uses the internal gesture sensor(APDS99690) for identifying gestures and then sends appropriate keyboard commands using the USBHID features of the board to control music.

Serial cannot be used for debugging, hence the LED_BUILTIN is being used, if the code runs successfully you should see the LED_BUILTIN stay lit up.
*/


#include <Arduino.h>
#include "mbed.h"
#include "USBKeyboard.h"
#include <Arduino_APDS9960.h>

USBKeyboard key;

// Declared weak in Arduino.h to allow user redefinitions.
int atexit(void (* /*func*/ )()) {
 return 0;
}
// Weak empty variant initialization function.
// May be redefined by variant files.
void initVariant() __attribute__((weak));
void initVariant() { }
void setupUSB() __attribute__((weak));
void setupUSB() { }

void setup() {
 pinMode(13, OUTPUT);
 digitalWrite(13, HIGH);
 if (!APDS.begin()) { 
   digitalWrite(13, LOW);
 }
}

void loop() {
 if (APDS.gestureAvailable()) {
   int gesture = APDS.readGesture();
   switch (gesture) {
     case GESTURE_UP:
       key.media_control(KEY_PLAY_PAUSE);
       break;
     case GESTURE_DOWN:
       key.media_control(KEY_MUTE);
       break;
     case GESTURE_LEFT:
       key.media_control(KEY_PREVIOUS_TRACK);
       break;
     case GESTURE_RIGHT:
       key.media_control(KEY_NEXT_TRACK);
       break;
     default:
       break;
   }
 }
}

int main(void)
{
 init();
 initVariant();
#if defined(USBCON)
 USBDevice.attach();
#endif
 setup();
 for (;;) {
   loop();
   if (serialEventRun) serialEventRun();
 }
 return 0;
}

Credits

Jai Krishna
1 project • 0 followers
Contact

Comments

Please log in or sign up to comment.