Introduction: Moving by Distance
How to use the distance sensor’s HC-SR04 in a different way?
Using the measured distance as an indication for the direction can control something over the screen.
On that idea this little game is made "Moving by Distance".
How does it work?
Moving a ball (graphical) in to a box by two distance sensors.
The left sensor is used for moving right and left.
At the distance 15cm the ball shall not move.
Further away of the object shall move the ball to the left and how further away the ball shall move with more speed.
Moving the object closer then 15cm the ball shall move to the right and the speed will increase when getting closer to the sensor.
The sensor at the right is used for moving up and down, similar as the left sensor.
Further away than 15 cm shall move the ball up
Closer by shall move the ball down.
Move the ball in to the box. That's it.
If you have the sensors it is not difficullt to make it your self
See the next steps for how this is made.
Step 1: Hardware Set-up
For making this game “Moving by distance” is required:
- 2x HC-SR04’s
- 1x Arduino Uno
- USB cable for connecting the Arduino Uno
- the software application processing ( https://processing.org/ ).
- 2x breadboard.
- Jump wires and a cable with four wires.
The hardware is connected according the drawing above.
Where the Uno must be connected with your PC or laptop.
To create this game the following steps where made.
- Measuring the distance with Arduino and HC-SR04 and sending the measured value’s.
- Receiving the measured value’s within processing and calculate the movement of the ball and visualize.
Step 2: Measuring the Distance With an Uno and HC-SR04
The HC-SR04 is a sensor that measures the distance by sending a puls "Ultra sound" signal and measuring the time when this signal is bounced back.
The time between sending and receiving is an indication of the distance the nearest object is in front of the sensor. When the time is diveded by the speed of sound you know the distance.
As example the HC-SR04 is frequently used to tell a robot how far an object is.
But for now we are going to use it for moving a ball around on a screen.
Gladly we do not have to write a program for measuring the distance, this is already done and a library is already created. Library is called "NewPing.h"
To make use of this library the library has to be imported within the library manager.
Import the library "NewPing.h"
- Download the zip file "NewPing_v1.8. zip" from the site https://bitbucket.org/teckel12/arduino-new-ping/do...
- Open Arduino IDE.
- Select the tab "Sketch" and "Include Library" and "Add .ZIP Library"
- This will open a browser to select the just downloaded ZIP file, select the "NewPing_V1.8 .zip" and the library "NewPing.h" will be installed.
Now the program to receive the data from the HC-SR04 can be uploaded.
Upload the Arduino program
- Download the file "Arduino_sends_to_Pro.....".
- open the file in Arduino
- Upload the file to your Arduino.
- Test the program.
When wired correctly you can see the measured values within the Serial Monitor of Arduino.
Be sure the baudrate of the Serial Monitor is the same as within the program.
If the program works well and the distance measurement are working the Processing application can be started.
Stop the Serial Monitor of Arduino IDE because it is not possible to have readings by Processing and the Serial Monitor of Arduino.
Step 3: Processing the Data
Processing is a software application that can be downloaded from the site https://processing.org/
I believe Processing was created to visualize data.
In this case we use it to visualize the received data of Arduino by moving a ball on the screen.
After downloading the processing can be executed.
Download the file "Processing_Receives_..." within this document and open it with Processing.
If you run it your display should be like the picture above.
Within the software program there is more explanation of how it works
I have issue and that is that it not always start because of an error.
But after 1 or 2 restarts it must work.
Maybe someone can help to avoid this error?
Hope you like it, and you use it.
We have a be nice policy.
Please be positive and constructive.