Project tutorial
T-Rex Run Chrome game Controller

T-Rex Run Chrome game Controller © GPL3+

In this post, I have used a LDR to sense the black obstacles (cactus )in this game and make the dino jump using a system of Arduino and pyt

  • 2 views
  • 0 comments
  • 0 respects

Components and supplies

About this project

Introduction:

In this post, I have used a LDR to sense the black obstacles (cactus )in this game and make the dino jump using a system of Arduino and python code.

Contents:

  • Hardware Required
  • Hardware Setup
  • Arduino Code
  • Serial Communication (brief)
  • Python Code
  • Final Step
  • Results

Hardware Required:

  • LDR
  • 10k resistor
  • Arduino nano
  • Few jumper wires

Hardware Setup:

First, we need to connect the LDR in series with the 10k resistor across +5v and Gnd.

Next, we take a jumper from the junction of LDR and Resistor to analog pin A0 (as shown below). In a way, we just made a voltage divider configuration and read the voltage drop across the LDR.

This value changes due to change in the amount of light incident on it.

Arduino Code:

In the below code we continuously read the analog value of the LDR sensor and if it comes inside the desired range for black (here : >270 & <400), we update the laststate variable to ‘1‘.

In the other case, first, we check if the laststate was 1 and print the same in the serial monitor for python code to read and next we update the laststate variable to 0.

int val=0;//variable to read and update A0 pin value
int laststate=0; // to update the state of present colour the LDR detects 1:black ,0:white
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(A0,INPUT);
}
void loop() {
val=analogRead(A0);
Serial.println(val);
laststate=0;// by default set to 0:white
while(val&gt;270 &amp;&amp; val<400)// this while loop runs until the sensor keeps on detecting black
{
val=analogRead(A0);
laststate=1;
}
if(laststate==1) //if black send 1 ==&gt; jump
{  Serial.println(1);
delay(10); 
}
}

Serial Communication (brief Idea):

We cannot control the keyboard strokes directly from Arduino IDE for Arduino nano. So we will make use of python to simulate the keystroke using python’s library pyautogui.

Also, for serial communication, we use library pyserial. Refer here

First, we will upload the code to Arduino and note down the Serial port to which it is connected to the computer.

Next we will run the below python code bring the setup alive.

Python Code:

import pyautogui
import serial                                 # add Serial library for Serial communication
import time
c=0
Arduino_Serial = serial.Serial('com9',9600)  #Create Serial port object called arduinoSerialData
while(1):
if(Arduino_Serial.read()=='1'):
pyautogui.keyDown('space')
time.sleep(.1)
pyautogui.keyUp('space')

Final Step :

Now once all the above steps are done and the Arduino code is uploaded. Simply place the LDR sensor in position to your Desktop Screen(refer below animation) using a tape.

Finally, run the python code to see this in action.

Results:

Code

Github Repository
The entire code and instructions are available in here

Schematics

Fritzing Circuit diagram
LDR connection with Arduino nano
Diagram2 noyswyqhdn
Github Repository
The entire code and instructions is available here.

Comments

Similar projects you might like

Arduino Pocket Game Console + A-Maze - Maze Game

Project tutorial by Alojz Jakob

  • 11,035 views
  • 8 comments
  • 28 respects

The Chrome Dino Game on an LCD Shield

Project tutorial by Unsigned_Arduino

  • 9,520 views
  • 2 comments
  • 22 respects

Humidity Controller

Project tutorial by Shaeroden

  • 4,050 views
  • 3 comments
  • 9 respects

Bubble Cannon Interactive Game Controller

Project tutorial by Jimmy Huang

  • 1,573 views
  • 0 comments
  • 1 respect

Footpad game controller

Project showcase by Kristof

  • 127 views
  • 0 comments
  • 2 respects

Google Chrome Dinosaur Game on 16x2 LCD Shield

Project showcase by brzi

  • 12,035 views
  • 10 comments
  • 37 respects
Add projectSign up / Login