Jalal Mansoori
Published © GPL3+

Play Dinosaur Game Using Arduino and Python3

A great project for beginners! You can play Dinosaur game not by pressing any keyboard buttons but by motion of your hand.

BeginnerFull instructions provided5,210
Play Dinosaur Game Using Arduino and Python3

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Breadboard (generic)
Breadboard (generic)
×1
Photo resistor
Photo resistor
×1
Resistor 10k ohm
Resistor 10k ohm
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE
Python3

Story

Read more

Schematics

Circuit Diagram to make this project

Additional Jumper wires were use to give space for hand motion

Code

Arduino Code

Arduino
Download the file and paste in your sketch
/*
  This Program is about connecting arduino and python3 for playing google dinosaur game.
  In this sketch simply from using readings of ldr "up" command is sent to python3 which is use by pyautogui library to control 
  up arrow function.

  Note: You may have to change below threshold value based on the light intensity of your room. 
        Also value in delay() function
  
  By Jalal Mansoori
*/
int ldrValue=0;
int threshold=400; // Your threshold value may be different 
void setup() {
  // put your setup code here, to run once:

  Serial.begin(9600);// Getting our serial monitor ready to be use by python3
  pinMode( LED_BUILTIN, OUTPUT); // LED_BUILTIN means led on board connected with digital pin 13
  pinMode(A1, INPUT); //  ldr sensor is connected to analog pin A1
}

void loop() {
  // put your main code here, to run repeatedly:
  ldrValue=analogRead(A1); // Takes values from ldr sensor
 // Serial.println(ldrValue); // To check which value fits best depending on room light intensity you use this function by uncommenting it
  
  
  if(ldrValue> threshold)
  {
    digitalWrite(LED_BUILTIN ,LOW ); // So if ldrValue is greater than 400 threshold then do nothing led will be off
    
  }
  else
  {
      
     digitalWrite(LED_BUILTIN ,HIGH ); // Otherwise Turn led ON and Sent "up" command to python3 Thats all !
     Serial.println("up");
     delay(500);// you may have to change this value because delay was use to sent only one "up" command otherwise it will send multiple commands. 
  }
  
}

Python3 Code

Python
Download the file and paste in your python3 file
# Serial library use to connect python3 and arduino
import serial

# pyautogui library will be use to perform "up" arrow function
import pyautogui

#Creates object of Serial class. You may need to change port name
#Check your ardunio sketch in tools section to know port name
arduino=serial.Serial('COM1', 9600)

while 1:

    incoming_Data=arduino.readline() #readline() function will get data from serial monitor line by line
    if 'up' in incoming_Data.decode('utf-8'): #In python3 data needs to convert into byte so decode function is use for that 
        pyautogui.press('up')# function to perform up arrow functionality
        #print("up")

    incoming_Data="" # reinitialing incoming_Data to empty

Credits

Jalal Mansoori
3 projects • 46 followers
BS Computer Science | Passionate blogger | Learn, Create, and Share
Contact

Comments

Please log in or sign up to comment.