• 1
    Working of the project in brief

    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.

    const int trigPin=11;  //DECLARE TRIG PIN AT D11
    int echoPin=10;         //DECLARE ECHO PIN AT D10
    int safezone=10; 
    void setup() 
    {
    pinMode(trigPin,OUTPUT);
    pinMode(echoPin,INPUT);
    Serial.begin(9600);
    }
    void loop()
    {
    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);
    }
    long microsecondsToCentimeters(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
    }