Presentation is loading. Please wait.

Presentation is loading. Please wait.

Variables Lesson 3.

Similar presentations


Presentation on theme: "Variables Lesson 3."— Presentation transcript:

1 Variables Lesson 3

2 Starter What will this program do? What is a variable?
What is the variable in this program? How do you create variables?

3 Objective of the lesson
Use variables in your programming All of you will: Explain what a variable is used for. Most of you will: Switch servo motors on and off. Manipulate parts of Edbot and save the servo positions as a variable. Some of you will: Use variables and mathematical operators to work out the average of 3 numbers entered.

4 Alter this program What will this program do?
Open L3_Average.sb2 and enter the program. Change the program so that it will work out the average of the 3 numbers that have been entered and then ask the user to enter the average (the total divided by 3).

5 Possible answer

6 Recap on servos What are servos? What would servos 1 and 2 control?
Servos are small motors and their positions can be set from 0 to 300. What would servos 1 and 2 control? Servos 1 and 2 move Edbot’s arms from the shoulder.

7 Using Servos Open the program called L3_Mirror.sb2.
This program has been started for you and will set Edbot to the initial position and then turn servo 1 off. This will switch off servo 1’s motor so you can manipulate it manually. Create a new variable called servoposition and set it to 0. What will this line of code do?

8 Servos The servos on each side work by rotating between 0 and 300.
Opposite servos will need to use opposite values on the 0 – 300 scale. Therefore if servo 1 is set to 50, to make servo two move to the same position it will need to be set to =250. We want servo 2 to move so that it is at the same angle as servo 1. How can we do this? Why does this need to be inside a forever loop?

9 Altering your script We only want it to run this code if servo 1 has moved. Write an if statement so that if servo 1 position is over “servopostion + 1” OR servo 1 position is under “servoposition – 1” then it will set servopostion to Edbot’s servo 1 position and move servo 2 so that it mirrors this movement.

10 Remember you can create your own blocks
In the last lesson, we looked at how you can create your own blocks. Why would you create your own blocks? How can you create a block? How can you use that block in your programming?

11 Over to you… Create a block called Getservonum which will ask the user to enter a number. If they answer below 17 display the message “Thank you” and save their answer in a variable called servonum. However, if their answer is not below 17 say “Incorrect” and set the servonum variable to “all”. In the main program set Edbot to the initial position and set all the servo colours to off. Run the Getservonum block and set the servo whose number is servonum to red for 1 second then switch all the servo lights off again. Run the Getservonum block again but this time set the servo whose number is servonum to green for 1 second then switch all the servo lights off again. Run the Getservonum block a third time but this time set the servo whose number is servonum to yellow for 1 second before you switch all the servo lights off again.

12 Possible answer Here you can see we have defined a block “Getservonum” and it is used 3 times in the program to get the servo number. The variable “servonum” that is generated from that block is used in the main program.

13 Self-assessment Read the highlighted points and tick those that you feel you have achieved.


Download ppt "Variables Lesson 3."

Similar presentations


Ads by Google