Skip to main content

Introduction

What if we want to change the speed our robot is following the line, without stopping it and reprogramming? Let's use variables and switches to do this.

  1. We're going to need two switches - one to increase the speed, and one to decrease the speed.
    • We're going to need two switches - one to increase the speed, and one to decrease the speed.

    • Add the two switch modules to your robot, and plug them into P2 and P8.

    • You can remove the Sparkles for now - setup the robot just like the picture.

  2. Load your two sensor line follower program from the last lesson.
    • Load your two sensor line follower program from the last lesson.

    • Remove all the Sparkle blocks - they will make the program much longer and we don't have the Sparkles anymore.

    • Your program should look like the picture - your speeds and waits might be different, depending on what works best for you.

  3. Remember variables? Here's a quick reminder of what we can do with them: Call them anything we like (variable name)
    • Remember variables? Here's a quick reminder of what we can do with them:

    • Call them anything we like (variable name)

    • Store any number we like inside them (variable contents)

    • Change the contents at any time (add, subtract, multiply, divide and so on)

    • Access the contents at any time, so long as we know the name of the variable.

    • Add a new variable called speed

    • Right at the start of your program, set speed to 0

  4. Let's use the variable we have just created to set the motor speeds!
    • Let's use the variable we have just created to set the motor speeds!

    • Replace all the motor speeds in the motor blocks with the speed variable. We've done the first one for you!

    • What would happen if we tested the program now? Would the robot still follow the line?

    • Test it out and see if you're correct!

  5. Let's use the first switch connected to P2 to increase the speed.
    • Let's use the first switch connected to P2 to increase the speed.

    • Add an IF block right at the top of the program to check the switch.

    • If the switch is pressed, increase speed by 10.

    • Test out the program - can you work out what is wrong?

  6. Remember last time we used a switch to change something? We had to add something else so it didn't change too fast!
    • Remember last time we used a switch to change something? We had to add something else so it didn't change too fast!

    • We want to wait until the switch is not pressed anymore, so we only increase the speed once each time the switch is pressed.

    • Add a while loop after you increase speed by 10, that waits until we let go of the switch to fix this.

    • Test it out - make sure it works properly now!

  7. We don't want the value of speed to go over 100 - we can't go any faster!
    • We don't want the value of speed to go over 100 - we can't go any faster!

    • To do this, let's edit the IF block that checks the switch.

    • Change the condition so that it checks if the switch is pressed, AND speed is less than (<) 100.

    • You can find the less than operator, < , in the logic menu.

  8. Now its up to you! Add some more blocks to check the other switch (P8), and decrease the speed by 10 each time it is pressed.
    • Now its up to you!

    • Add some more blocks to check the other switch (P8), and decrease the speed by 10 each time it is pressed.

    • This time, you will need to make sure that speed is only decreased if it is more than 0.

    • We've given you all the blocks you need - just put them in the right order!

  9. This is a hard extension challenge, so don't worry if you find it difficult!
    • This is a hard extension challenge, so don't worry if you find it difficult!

    • Can you change the code so only one switch is needed?

    • The speed should increase with a short press, and decrease with a long press.

  10. If you're feeling really clever, add the Sparkle code back in once you've got rid of one switch!
    • If you're feeling really clever, add the Sparkle code back in once you've got rid of one switch!

    • For super advanced coders only - can you change the brightness of the Sparkles depending on the speed of the robot? For example, at maximum speed (100) they should be as bright as possible, and at 0 speed they should be off.

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