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

Play Subway Surfers Using Gesture Sensor

This is how electronics engineer plays games.

IntermediateFull instructions provided743
Play Subway Surfers Using Gesture Sensor

Things used in this project

Hardware components

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

Software apps and online services

Arduino IDE
Arduino IDE
Visual Studio 2015
Microsoft Visual Studio 2015

Story

Read more

Code

Arduino Code

C/C++
#include <Arduino_APDS9960.h>

void setup() {
  Serial.begin(9600);
  while (!Serial);

  if (!APDS.begin()) {
    Serial.println("Error initializing APDS9960 sensor!");
  }

  // for setGestureSensitivity(..) a value between 1 and 100 is required.
  // Higher values makes the gesture recognition more sensible but less accurate
  // (a wrong gesture may be detected). Lower values makes the gesture recognition
  // more accurate but less sensible (some gestures may be missed).
  // Default is 80
  //APDS.setGestureSensitivity(80);

  
}
void loop() {
  if (APDS.gestureAvailable()) {
    // a gesture was detected, read and print to serial monitor
    int gesture = APDS.readGesture();

    switch (gesture) {
      case GESTURE_UP:
        Serial.println("UP");
        break;

      case GESTURE_DOWN:
        Serial.println("DOWN");
        break;

      case GESTURE_LEFT:
        Serial.println("LEFT");
        break;

      case GESTURE_RIGHT:
        Serial.println("RIGHT");
        break;

      default:
        // ignore
        break;
    }
  }
}

Python Code

Python
import serial                                       

from pynput.keyboard import Key, Controller           

ser = serial.Serial('COM4', 9600)                   

keyboard = Controller()

while True:
    data = ser.readline()
    
    if data.decode().strip() == "LEFT":
        keyboard.press("a")
        keyboard.release("a")
    

    if data.decode().strip() == "RIGHT":
        keyboard.press("d")
        keyboard.release("d")
    

    if data.decode().strip() == "UP":
        keyboard.press("w")
        keyboard.release("w")
    

    if data.decode().strip() == "DOWN":
        keyboard.press("s")
        keyboard.release("s")

Arduino and Python Code

Credits

PRABEEN RAJ
10 projects • 2 followers
Contact

Comments

Please log in or sign up to comment.