For this project you will need:
- One Crumble with USB lead;
- A computer with the Crumble software installed;
- One battery pack with batteries (Not rechargeable);
- One ultrasonic distance sensor;
- One Sparkle or Sparkle Baton;
- One buzzer;
- 11 croc-leads.
First of all, we need to connect up the battery pack to the Crumble (keep it turned off for now). We need to take care with this as we must make sure that the + on the battery pack goes into the + input of the Crumble, and – on the battery pack connects to the – one on the Crumble.
Next, we need to attach our ultrasonic distance sensor. We have connected the + and – on the sensor to the + and – on the battery pack, and then the trigger (T) pad to A on the Crumble and the echo (E) pad to B on the Crumble.
hint: you can use any of the four I/O pads (A, B, C or D) but we need D for our sparkles!
Then we are going to attach the Sparkle Baton (you can use one sparkle if you wish). We need to connect the + and – input on the Sparkle baton (arrow pointing right -> ) to the + and – output pads on the Crumble. Next, we need to attach the data input pad to D on the Crumble.
The last thing we need to connect is the buzzer. We are going to connect C on the Crumble to the + side of the buzzer, and the – side of the buzzer to the – on the Sparkle Baton.
The very first thing we are going to do now is to check that our ultrasonic distance sensor is working correctly. To do this we are going to create a variable, which we’ll call ‘distance’, and then repeatedly let the variable = ‘distance (cm) T:A E:B ‘ . When we run the program, we should be able to see our variable (distance) on the variables tab change when we move our hand closer and further away from the sensor. If this didn’t work, we would check our wiring is correct and that the battery pack is turned on.
Now we want to make something happen when the distance sensor detects something closer than 120cm. We will use an ‘if _else’ block, with the condition ‘if distance > 120’. If the distance is larger than 120cm, we want the Sparkles to be off. Whereas if it is closer, we want something to happen, so we will turn all the Sparkles to green.
Now we want to add another condition in. We are going to say that if the distance away from the sensor is in between 80 and 121 cm, the Sparkles will be green. If it is 80cm or less, it will be orange, and if it is more than 120cm, the Sparkles will be off.
Now we are going to put in our final conditions. If the distance is less than 81 cm, but more than 30, the Sparkle will shine orange, and when it is less than 31cm it the Sparkles will be red. This now means that if we are more than 120cm away from the sensor, the Sparkles will be off, but as we get closer to the sensor, the Sparkles will change from green, to orange and then to red.
To make this more like a real parking sensor, we need some kind of audible alert, so we will use our buzzer which is connected to C. To get the buzzer to ‘beep’ we are going to turn it on for 100 milliseconds, and then off for 500 milliseconds. If we wanted a longer pause, we could increase the second wait time, and for a shorter pause, we could decrease the second wait time.
To finalise our code, we are going to place versions of the aforementioned ‘beep’ code after each ‘Sparkle on’ statement. We have used the principal that the closer you are to the object, the faster the beeps need to be.
And there you have it! A reverse parking sensor, ready to be integrated into your projects.