Hello everyone!!!
Welcome to a new Arduino based project. We all have once in our life time played the flappy bird game. How if we play it on our PC and controlled it using our Arduino?? Follow all the steps given below and by the end of this tutorial you will control the game using your microcontroller.
The reason we used the ultrasonic sensor here is to get the distance data between our hand and the sensor and use those values to adjust the height of the moving bird. The game is created in Processing and the Arduino communicates with it using the serial port. I have linked few images of the game above so take a look at them in order to get some idea about this project. SPONSOR LINK - UTSource.net.
2
Let's do the connections
First connect the SR-04 sensor to the Arduino board. As there is just one sensor to interface I won't add a circuit diagram for this project. The connections are as follows -
SR-04 >> Arduino Uno
Vcc >> 5V
Gnd >> Gnd
Trigger Pin >> Digital pin 11
Echo Pin >> Digital pin 10
That's it the connections are done.
3
Upload the Arduino code
Now time to upload the code to your Arduino board.
Copy the code from below.
Before uploading the code make sure to select the proper com port and baud rate as we will be using it for sending data to the game.
constint trigPin=11; //DECLARE TRIG PIN AT D11int echoPin=10; //DECLARE ECHO PIN AT D10int safezone=10;
voidsetup()
{
pinMode(trigPin,OUTPUT);
pinMode(echoPin,INPUT);
Serial.begin(9600);
}
voidloop()
{
long duration,cm; //DECLARE VARIABLES TO STORE SENSOR O/P
digitalWrite(trigPin,LOW); //MAKE THE TRIG PIN LOW
delayMicroseconds(2); //WAIT FOR FEW MICROSECONDS
digitalWrite(trigPin,HIGH); //NOW SET THE TRIG PIN
delayMicroseconds(5); //WAIT FOR FEW MICROSECONDS UNTIL THE TRIG PULSE IS SENT
digitalWrite(trigPin,LOW); //MAKE THE TRIG PIN LOW AGAIN
duration=pulseIn(echoPin,HIGH); //MAKE ECHO PIN HIGH AND STORE THE BOUNCED PULSE IN VARIABLE DURATION
cm=microsecondsToCentimeters(duration);
long inch= cm/2.54;
Serial.println(cm);
}
longmicrosecondsToCentimeters(long microseconds) //SPEED OF SOUND IS 29 uSEC PER CM{
return microseconds/29/2; //THE PULSE TRAVELS FROM THE SENSOR AND AGAIN COMES BACK SO WE DIVIDE IT BY 2 TO TAKE ONLY HALF OF THE TOTAL DISTANCE
}
I like your idea. I want to run the similar program for my Zalo PC program, you can see here about it https://thezalopc.com/