Greetings coders and tinkerers, I’m bringing up yet another tutorial, explaining a game that I built using Arduino and Processing. So, to understand the coding part of the game, you’ll have to know about the hardware construction and even before that, you’ll have to understand what the game is all about. Here you go: Once the game is started, the player will have to place his hand at some distance from the Ultrasonic sensor to stop the Black hole from growing. Ironically, it is impossible to stop the black hole completely, so the score will be based on the duration for which the Player holds the black hole before it covers up his entire screen. Basically, this game is based on both Player’s action and probability since the HC-SR04 sensor is an Ultrasonic sensor, so the waves can easily penetrate the Player’s hand.



Here’s all that you’ll need for making this game:
- Arduino UNO/Nano
- HC-SR04 Ultrasonic Sensor
- Jumper Wires
- Arduino IDE
- Processing
After gathering the required materials, begin with assembling the circuit with the help of the following connections:
- VCC -> +5V
- GND -> GND
- Trig -> GPIO 9
- Echo -> GPIO 10
Understanding the Code
const int trigPin = 9;
const int echoPin = 10;
long duration;
int distance;
The most important part of a code is to declare and initialize a variable so, at the beginning, we have declared two variables named trigPin
and echoPin
and initialized them to their respective pin numbers.
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
As y’all know that whatever is written in the void setup()
block of the code, executes only at the beginning of the program. So, I’ve defined the trigPin
to give the output and the echoPin
to take input.
void loop() {
digitalWrite(trigPin, LOW);
delay(8);
digitalWrite(trigPin, HIGH);
delay(8);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance= duration*0.034/2;
Serial.println(distance);
}
As in the loop part, it gets repeated continously, so first of all the trigPin
is set to LOW and after a delay
of 8 milliseconds, it is set to HIGH and then to LOW again, to generate a wave, while the echoPin
will receive the wave and calculate the duration. To get the value of distance, I’ve used the general formula of an echo. That was enough for the arduino code, now it’s time to move on to the Processing part.
The Processing part is clearly commented and the whole code can be found in my github repository. The code is clearly understandable, but if you have any question, feel free to comment down below or message me on my social channels, which have been linked below in the Author Section.
Happy Tinkering!
Neel Adwani (or neeltron) is your friendly neighborhood techie, a coder, traveler, stargazer and as you all know, a blogger. He has an endless range of interests in a commendable number of fields. He is an introvert, so don’t mess with him. He’s open to suggestions and criticism, though.
Полезно
Awesome post! Keep up the great work! 🙂
Sustain the excellent job and delivering in the crowd!
Good day, neat websites you have going here.
Hello there. I found your website by the use of Google whilst searching for a related subject, your site came up. It seems to be good. I have bookmarked it in my google bookmarks to visit then.
Does your blog have a contact page? I’m having problems locating it but, I’d like to shoot you an email. I’ve got some suggestions for your blog you might be interested in hearing. Either way, great blog and I look forward to seeing it grow over time.