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 > A

    • Switch > B

    • Sparkles > D

    • Left Motor> M1

    • Right Motor > M2

    • Put the trackball under the switch module again.

  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, and move your do forever loop inside the IF block so it is run only when the switch is pressed.

    • Don't forget, you will need to put everything inside another do 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! We need to change the do forever loop to another type of loop - the do until loop.
    • Now we can activate the police robot with the switch, but we can't turn it off again!

    • We need to change the do forever loop to another type of loop - the do until loop.

    • The do until loop has a condition, just like the IF block - the loop will repeat but only until the condition is true!

    • Drag one in from the control menu and update your code to look like the picture.

    • Test it out - does it work as expected?

  6. You might find it doesn't work very well, and it sometimes takes a few attempts to turn the lights on or off.
    • You might find it doesn't work very well, and it sometimes takes a few attempts to turn the lights on or off.

    • 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 do until 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

    • This can happen many times before we have released the switch, so we have no idea whether the lights will be running or not!

  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 there is a block that lets us do just that - wait until. This block also has a condition, and will simply wait until the condition is true!
    • Luckily there is a block that lets us do just that - wait until.

    • This block also has a condition, and will simply wait until the condition is true!

    • Delete the example program you just made and put the police program back.

    • Drag two wait until blocks from the control menu.

    • Add them to your program like the second picture - can you guess what they will do?

    • 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 end of the do until loop.

  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