The Assignment was to create a project that is controlled using Arduino Uno R3 :
The project should have at least one input and one output which are different from the Tutorials and the circuit should be designed and simulated using Tinkercad.
The idea of the project is not original as it has a reference you can access through this link but you should know that the code and the design of the circuit is original.
The project was to play the Dino and Cactus game without any human interaction (you would see me fixing the servo motor in place to hit the space button THAT DOES NOT COUNT ^^) which appears when you using google chrome and the internet access isn't there .
The reason why I choose this project is that i have tried it before and it didn't work as good as this time (explained more in problems and challenges)
The final out come ^^
I used:
For simulation and designing: Tinkercad
For coding: I used Arduino IDE (As you can see that the used sensor in the tinkercad simulation isn't connected to a module like the one i am using which would need different code from this one of tinkercad EXPLAINED later in the IMPLEMENTATION section)
Components:
Arduino Uno R3
USB cable (to connect the Arduino to the laptop to transfer code and power)
1 x Servo motor (SG90)
3 pins LDR sensor module
Male-Male jumpers
Female-Male jumpers
Testing red LED
1 x 220ohm resistor
Small breadboard
1 x Lipton green tea bag to fix the sensor to the screen^^
The preparation process has three parts:
Understanding how the project works.
Hardware connections (circuit design).
Coding
Understanding how the project works:
We have seen this game before and we know that when it starts it has a white (light = 1) back ground and the Dino and the Cactus in black (dark = 0).
the working systems starts with the LDR sensor sending the Arduino digital signals (1 or 0) to control the servo motor which presses the space button on the laptop keyboard.
As long as the sensor sends (1) to the arduino the servo motor stand still at (90) degree (not pressing the space button) but when a Cactus appears the sensor reads then sends (0) to the arduino to make the servo motor move to (30) degree (pressing the space button)
Hardware connections:
The connections on tinkercad are exactly (except the DO pin in the LDR module is connected to pin 7 as i said before the LDR sensor in tinkercad has different code and behaviour EXPLAINED later) the same as in real life I have used the same jumpers colours and the same pins on the Arduino so if you struggle with understanding the connection you can mimic the tinkercad circuit.
Hardware connections:
(1) Connection of the LDR sensor module:
The module as said before has three pins:
VCC (red, connected to 3.3V pin in the Arduino)
GND (orange, connected to one of the GND pins on the Arduino)
DO (yellow, which means Digital output, connected to pin 7)
I used female-male jumpers to connect these pins to a small breadboard then used same coloured male-male jumpers to connect these ends together and with the Arduino.
(2) Connection of the testing red LED:
the LED has two legs and have to be connected to a resistor (used 220ohm)
The positive terminal of the LED is connected to the resistor in series and then two a green male-male jumper to pin 6 in the Arduino and the negative terminal to the grey jumper which is connected to the GND node in the small breadboard (You can see the rest of the grey jumper in pic (1) ).
(3) the connection of the servo motor:
The servo motor has 3 pins:
Brown (GND, connected to a purple male-male jumper and then connected to the GND pin in the Arduino)
Red (Vin, connected to a white male-male jumper then connected to the 5V pin in the Arduino).
Orange (signal, connected to a yellow male-male jumper then to pin 8 in the Arduino)
A full view of the connections
Coding:
Here you will find the actual used code and to understand it you will have these comments to guide your way through.....
/*this is the assignment of week6 of Amr Y. Abdulrahiem
* the project is to play the no internet dino game without
* human interaction but only the first level in light mode.
*/
#include <Servo.h> //including the library of using servo motors for easier coding to use the angles value.
int ldr = 7; //assigning pin 7 to read the game screen and send digital signals to the arduino
int sensorstatus= 0; // a variable to store the values read by the LDR sensor
int led =6; // assigning pin 6 to a output a testing signal in the shape of lighting an LEd
Servo presser; // giving the servo motor a meaningful name a presser as it will press the space button
void setup ()
{
Serial.begin (9600); // making a serial connection to read the LDR sent values
pinMode(ldr, INPUT); // identifying the LDR pin as INPUT to the arduino.
pinMode (led, OUTPUT); //identifying the LED pin as output to the arduino.
presser.attach(8); // assigning the pin which the servo is connected to (PIN 8)
}
void loop ()
{
sensorstatus = digitalRead(ldr); //starting the code with reading the DIGITAL signals of the LDR
Serial.println (sensorstatus); // printing the these values to see is it working right or not and what will it reads when it is dark and light.
if (sensorstatus == 1) // making a condition that if the sensor reads 1 (dark) do this.
{
digitalWrite (led, HIGH); //light the LED
presser.write(0); // make the servo go to angle 0 or whatever angle that can press the space button.
}
else
{
digitalWrite (led, LOW); // turn of the LED
presser.write(90); // make the servo go to angle 90 to stop pressing the space button
}
}
And finally The Di-No internet game with almost no human interaction works .....
Things I 've learnt:
I didn't know that the Arduino Uno had an internal Pull up resistor that can be activated through the code...I asked instructor Ahmad Ibrahiem about why we use this line int the code PULL_UP with push buttons and that was his answer that there is a PULL_UP resistor inside the Arduino.Before that I used to connect the a pull up resistor in the circuit...
Pull UP resistor in hardware is shown in this pic >>>
the black jumper is conneted to the ground
the white jumper is conneted to the 5V pin
the orange jumper is connected to any digital signal pin
I didn't know the purpose of the potentiometer that existed in the LDR module until Instructor Ahmad Ibrahiem explained that it is used to adjust the Threshold point that the LDR can count it as an origin point or a reference to take action according the light intensity.
Challenges I faced:
At first I didn't notice the DO pin in the LDR module the it is make to output digital signals, I thought it was to send the OUTPUT whatever its type is so I used it to analogRead() the light intensity in the room, when I move the LDR closer to background of the game and when i move the LDR closer to the Cactus.
Then make If statements like if (lightintencity>30 && lightintencity<40){
servo.write(90); //which means the space button is not pressed. }else if ....
{
servo.write(0);
}
this was absolutely a waste of time and nerve-racking process as the goal is to find the precise values that the change has to happen when they be measured.
THE SOLUTION:
I finally noticed that the DO pin is not only OUT pin but it is a Digital OUTPUT pin so I used with the LDR module the digitalRead()
and controlled the Threshold point using the potentiometer on the module until I found the perfect value and finallly it worked.....
Things to be improved next time.....
As you saw in the reference link above that the maker there is using the LDR under light with ease but I tried many times it didn't work, you saw me play in absolute darkness the only source of light was the laptop screen..so I will see a walk around way to overcome this challenge.
Another thing need to improve is when the game switches to the dark mode the servo would be pressing the space button forever....A suggestion from Mennatallah Yousri is to use another LDR sensor module to be responsible for changing the values to suit the dark mode when it comes and returns back when the light mode comes back...
Title of Media
Title of Media