Skip to main content

Introduction

Make a program using your Sparkle and buzzer modules, that turns your robot into a police robot to keep the planet safe.

  1. We're going to be using a lot of modules to turn our robot into a police car!
    • We're going to be using a lot of modules to turn our robot into a police car!

    • Carefully assemble your robot like the picture. The connections should be:

    • Buzzer > P2

    • Switch > P1

    • Sparkles > P0

    • Left Motor> M1

    • Right Motor > M2

  2. Now we know how to use the sparkle module, let's start by programming all the sparkles to flash red and blue like a police car.
    • Now we know how to use the sparkle module, let's start by programming all the sparkles to flash red and blue like a police car.

    • Your program should:

    • Turn all sparkles red

    • Wait half a second

    • Turn all sparkles blue

    • Wait half a second

    • Repeat this forever!

    • Make sure to test your program works - there are some hints about the blocks your will need in the picture if you need help.

  3. Let's add the buzzer to the flashing lights to make a siren!
    • Let's add the buzzer to the flashing lights to make a siren!

    • Add some blocks to your program so the buzzer is:

    • Buzzing when the sparkles are red

    • Off when the sparkles are blue

    • Again, there are some hints in the picture if you can't remember which blocks to use.

  4. Police cars don't have their lights and sirens on all the time, that would be very irritating!
    • Police cars don't have their lights and sirens on all the time, that would be very irritating!

    • Let's add a switch so the police robot only flashes and buzzes when we press it.

    • Add an IF block that checks if the switch is pressed.

    • Put your flashing/buzzing code inside a while true loop from the loops menu (this will just loop forever), and put this inside the if block.

    • That way, it will only start when the switch is pressed! There are some more hints of the blocks you need in the picture.

    • Don't forget, make sure your if block is inside the main forever loop, so the switch isn't just checked once!

  5. Now we can activate the police robot with the switch, but we can't turn it off again!
    • Now we can activate the police robot with the switch, but we can't turn it off again!

    • We can fix this by changing the while loop.

    • So far, we have just used the while loop like the forever loop. But it is much more useful than that.

    • While loops work just like an if block - they have a condition, and they will repeatedly run the code inside them, all the time the condition is true.

    • So a while loop with true as the condition will just run forever!

    • Change the while loop condition to be while digital read P1 = 0 - that way, the code in the loop will run all the time we are not pressing the switch!

    • Now our code should start the siren when we press the switch, and stop the siren when we press it again. Test it out - does it work as expected?

  6. You have probably found that we can't turn the siren on anymore!
    • You have probably found that we can't turn the siren on anymore!

    • Our problem is that the robot runs the program very, very fast.

    • Even if we just tap the switch, the robot thinks so quickly it thinks we are holding it down.

    • Look at the simplified version of the code in the picture:

    • As soon as we press the switch, the conclusion of the if block is run

    • The robot checks the condition of the while loop before we can possibly have time to take our finger off the switch, so the loop stops and the program goes back to the red arrow.

    • So the siren never gets activated!

  7. This can be hard to understand! Let's make another short program to understand this problem with an example.
    • This can be hard to understand! Let's make another short program to understand this problem with an example.

    • Drag your police program to the side (don't delete it!) and build the program in the picture.

    • This program turns the sparkles red if we press the switch, then blue if we press it again - test it out!

    • It is impossible to accurately make the sparkles red or blue, as the robot is too fast - we need to make it wait for us to let go of the switch to fix this.

  8. Luckily, we can use while loops to fix this.
    • Luckily, we can use while loops to fix this.

    • If we have an empty loop, with a condition of the switch being pressed, the loop will run all the time the switch is pressed.

    • This will effectively pause the program, until we let go of the switch!

    • Put your code back, and add two while loops like in the picture, to wait for the switch to be released again before starting the siren, and turning it off again.

    • Test out the program and make sure you understand it before moving on.

  9. You may have noticed that the sparkles stay blue after we turn off the police lights and siren - let's change them so they are green!
    • You may have noticed that the sparkles stay blue after we turn off the police lights and siren - let's change them so they are green!

    • Add some more sparkle blocks so that:

    • The sparkles are set to green at the start

    • They are reset to green '''after the siren is turned off.

  10. The final part of our police program is to make the robot chase after our criminal! Check the picture for what your code should look like so far.
    • The final part of our police program is to make the robot chase after our criminal!

    • Check the picture for what your code should look like so far.

    • Add some blocks to make your robot drive forwards at full speed when the switch is pressed and the sparkles are flashing, then stop when it is pressed again.

  11. For this extension challenge, you need to complete a few tasks:
    • For this extension challenge, you need to complete a few tasks:

    • Change the buzzer blocks so that your buzzer beeps faster than the sparkles change to make it sound more realistic.

    • When your robot is chasing the criminal, make it turn left and right in a weaving motion instead of just moving forwards.

Finish Line

Tech Camp

Member since: 10/03/2012

119 Guides authored

Team

Tech Camp Staff Member of Tech Camp Staff

6 Members

140 Guides authored