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 C and D.

  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 - we don't have space to plug the Sparkles in 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:
    • 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, let speed = 50

  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!

    • Program your robot and test to make sure it still works correctly. What speed will the motors be going at?

  5. Let's use the first switch connected to C to increase the speed.
    • Let's use the first switch connected to C 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.

    • There are some hint blocks if you need them!

    • 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 wait until block after you increase speed by 10 to fix this.

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

  7. You may have noticed that if you press the switch lots of times, strange things start to happen when the speed goes over 100. Program your robot and keep it plugged in - watch the value of speed on the variables screen and see what happens when it goes over 100.
    • You may have noticed that if you press the switch lots of times, strange things start to happen when the speed goes over 100.

    • Program your robot and keep it plugged in - watch the value of speed on the variables screen and see what happens when it goes over 100.

    • The motor blocks cannot have a speed of over 100, so we need to make sure speed is never more than 100!

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

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

    • You can find the less than operator, < , in the operators menu. We've started it for you - add the rest of the blocks and test it out!

  8. Now its up to you! Add some more blocks to check the other switch, 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, 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