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 code for now - we don't have the sparkles anymore.

    • Your program should look like the picture - your speeds 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 at the start of the program, which is equal to 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.

  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 statement right at the top of the loop to check the switch.

    • We also need to add the line global speed at the top of the function. Speed is a variable that is created outside of the function - we can access it without this line, but to change it we need to tell the robot it is global, which just means it has already been created outside of the function.

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

    • There is a hint if you need it!

    • 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 use a while loop 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 the speed by 10 to fix this.

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

  7. The motor blocks cannot have a speed of over 100, so we need to make sure speed is never more than 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 statement that checks the switch.

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

    • Test it out!

  8. Now its up to you!
    • Now its up to you!

    • Add some more code 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.

  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