T-Rex Run Chrome game Controller

by Apr 7, 2020Projects

Introduction:

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

Link for GitHub Repository for this code (click here)

System in Action:

Contents:

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

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>270 && 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 ==> 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 on the chrome (refer below animation) using a tape.

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

Creating a multiplication Skill in Alexa using python

Written By Monisha Macharla

Hi, I'm Monisha. I am a tech blogger and a hobbyist. I am eager to learn and explore tech related stuff! also, I wanted to deliver you the same as much as the simpler way with more informative content. I generally appreciate learning by doing, rather than only learning. Thank you for reading my blog! Happy learning!

RELATED POSTS

How to Simulate IoT projects using Cisco Packet Tracer

How to Simulate IoT projects using Cisco Packet Tracer

In this tutorial, let's learn how to simulate the IoT project using the Cisco packet tracer. As an example, we shall build a simple Home Automation project to control and monitor devices. Introduction Firstly, let's quickly look at the overview of the software. Packet...

How to design a Wireless Blind Stick using  nRF24L01 Module?

How to design a Wireless Blind Stick using nRF24L01 Module?

Introduction Let's learn to design a low-cost wireless blind stick using the nRF24L01 transceiver module. So the complete project is divided into the transmitter part and receiver part. Thus, the Transmitter part consists of an Arduino Nano microcontroller, ultrasonic...

How to implement Machine Learning on IoT based Data?

How to implement Machine Learning on IoT based Data?

Introduction The industrial scope for the convergence of the Internet of Things(IoT) and Machine learning(ML) is wide and informative. IoT renders an enormous amount of data from various sensors. On the other hand, ML opens up insight hidden in the acquired data....

Smart Display Board based on IoT and Google Firebase

Smart Display Board based on IoT and Google Firebase

Introduction In this tutorial, we are going to build a Smart Display Board based on IoT and Google Firebase by using NodeMCU8266 (or you can even use NodeMCU32) and LCD. Generally, in shops, hotels, offices, railway stations, notice/ display boards are used. They are...

Smart Gardening System – GO GREEN Project

Smart Gardening System – GO GREEN Project

Automation of farm activities can transform agricultural domain from being manual into a dynamic field to yield higher production with less human intervention. The project Green is developed to manage farms using modern information and communication technologies....

How to build a Safety Monitoring System for COVID-19

How to build a Safety Monitoring System for COVID-19

It is expected that the world will need to battle the COVID-19 pandemic with precautious measures until an effective vaccine is developed. This project proposes a real-time safety monitoring system for COVID-19. The proposed system would employ an Internet of Things...

VIDEOS – FOLLOW US ON YOUTUBE

EXPLORE OUR IOT PROJECTS

IoT Smart Gardening System – ESP8266, MQTT, Adafruit IO

Gardening is always a very calming pastime. However, our gardens' plants may not always receive the care they require due to our active lifestyles. What if we could remotely keep an eye on their health and provide them with the attention they require? In this article,...

How to Simulate IoT projects using Cisco Packet Tracer

In this tutorial, let's learn how to simulate the IoT project using the Cisco packet tracer. As an example, we shall build a simple Home Automation project to control and monitor devices. Introduction Firstly, let's quickly look at the overview of the software. Packet...

All you need to know about integrating NodeMCU with Ubidots over MQTT

In this tutorial, let's discuss Integrating NodeMCU and Ubidots IoT platform. As an illustration, we shall interface the DHT11 sensor to monitor temperature and Humidity. Additionally, an led bulb is controlled using the dashboard. Besides, the implementation will be...

All you need to know about integrating NodeMCU with Ubidots over Https

In this tutorial, let's discuss Integrating NodeMCU and Ubidots IoT platform. As an illustration, we shall interface the DHT11 sensor to monitor temperature and Humidity. Additionally, an led bulb is controlled using the dashboard. Besides, the implementation will be...

How to design a Wireless Blind Stick using nRF24L01 Module?

Introduction Let's learn to design a low-cost wireless blind stick using the nRF24L01 transceiver module. So the complete project is divided into the transmitter part and receiver part. Thus, the Transmitter part consists of an Arduino Nano microcontroller, ultrasonic...

Sending Temperature data to ThingSpeak Cloud and Visualize

In this article, we are going to learn “How to send temperature data to ThingSpeak Cloud?”. We can then visualize the temperature data uploaded to ThingSpeak Cloud anywhere in the world. But "What is ThingSpeak?” ThingSpeak is an open-source IoT platform that allows...

Amaze your friend with latest tricks of Raspberry Pi and Firebase

Introduction to our Raspberry Pi and Firebase trick Let me introduce you to the latest trick of Raspberry Pi and Firebase we'll be using to fool them. It begins with a small circuit to connect a temperature sensor and an Infrared sensor with Raspberry Pi. The circuit...

How to implement Machine Learning on IoT based Data?

Introduction The industrial scope for the convergence of the Internet of Things(IoT) and Machine learning(ML) is wide and informative. IoT renders an enormous amount of data from various sensors. On the other hand, ML opens up insight hidden in the acquired data....

Smart Display Board based on IoT and Google Firebase

Introduction In this tutorial, we are going to build a Smart Display Board based on IoT and Google Firebase by using NodeMCU8266 (or you can even use NodeMCU32) and LCD. Generally, in shops, hotels, offices, railway stations, notice/ display boards are used. They are...

Smart Gardening System – GO GREEN Project

Automation of farm activities can transform agricultural domain from being manual into a dynamic field to yield higher production with less human intervention. The project Green is developed to manage farms using modern information and communication technologies....