blog banner

Build your own ‘Stop the Black hole’ game with Arduino and Processing | Free Tutorial

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.

Scene 1 of the game.
Scene 2: The size of the black hole decreases when the hand or an object is in the ultrasonic sensor’s range, else it keeps expanding at 5 times the rate. The HC-SR04 Ultrasonic sensor is placed in front of the webcam.
Scene 3: The conclusion scene which displays the score.

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);

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);
digitalWrite(trigPin, HIGH);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance= duration*0.034/2;

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!

Leave a Reply

Your email address will not be published. Required fields are marked *