Presentation is loading. Please wait.

Presentation is loading. Please wait.

Storing Values as Variables

Similar presentations


Presentation on theme: "Storing Values as Variables"— Presentation transcript:

1 Storing Values as Variables
Variable = a place to store a value

2 Sensor values can be stored as variable.
Values stored as number variables can be mathematically modified. Text values can have additional text added. The most common use is to compare variables against other values to see if the value meets a certain threshold. Common uses for Variables: Storing Sensor Values Counting

3 Storing and Displaying Sensor Values
Program a robot to go into a tunnel and take a light sensor reading after 1 rotation; come back out of the tunnel and displays the value.

4 Start with a program that will drive the robot into the tunnel and then back out of the tunnel.
1 Place two Move Blocks on the sequence beam. One going into the tunnel and another backing out of the tunnel. 1

5 Programming the sensor readings inside the tunnel
Place a light sensor block inside the code following the forward movement. Uncheck Generate light so that the sensor does not detect its own reflected light inside the tunnel.

6 Detecting the Light Sensor’s own Reflected Light
If the “Generate Light” checkbox is checked, the light sensor will turn on its own small light source and detect the color that is reflected back to it. With the Generate light function on, the light sensor can detect the amount of light that is reflected back from a variety of shades of colors. With the Generate light function off the light sensor can detect the amount of ambient light that is surrounding the light sensor.

7 Adding a Variable Block
From the Data Palette place a Variable Block after the Light Sensor Block.

8 Assigning Variable types
Light Sensors values are numbers. Configure the Variable Block to use the Number Variable called Number 1. The value must be stored so the Light Sensor must write the value under actions.

9 Wire the Intensity Output Plug on the Light Sensor Block to the Input Plug on the Variable Block.
The Variable Block now holds the value that will need to be displayed after the robot exits the tunnel.

10 Displaying Stored Data

11 After the Variable Read Block, add a Number to Text Block followed by a Display Block.
Wire the Blocks as shown. Configure the Display Block to display the converted Text.

12 Add a Wait Block at the end of the program to allow time to read the displayed value. Set the wait time to 10 Seconds. 10

13 Download and run the program
Download and run the program. Check the NXT screen for the Light Sensor Display Value. 33

14 Part Two: Storing and Displaying More Than One Sensor Values
Program the robot to go into a tunnel, take a light sensor reading after 1 rotations and then again after 3 more rotations; come back out of the tunnel and display both values.

15 Add a 2 rotation Movement Block after the first variable

16 Add another Sensor Block to read the new sensor value and a Variable Block to store the second value. 2

17 There is only one Number Variable called Number 1, that variable is holding the first sensor value. Create a new Number Variable. Go to Edit and Define Variables. Create a variable named Number 2. Make the Datatype a Number

18 Configure Variable Block 2 to write to the variable named Number 2
Configure Variable Block 2 to write to the variable named Number 2. Wire the output Plug on the Light Sensor Block to the input plug on the Variable Block.

19 Create a second variable display just like the first one
Create a second variable display just like the first one. Place the Variable Block before the Wait Block. Format the Variable Block to Read the Variable named Number 2.

20 Format the Display Block to Text.
Create a second Number to Text just like the first one. Place the Number to Text after the Number 2 Variable Block. Place a Display Block after the Number to Text Block. Wire the blocks together. Format the Display Block to Text.

21 Display Block Settings
The two Display Blocks are set to display the values in exactly the same place on the NXT screen. Change the First Display Block Display setting by dragging the Mindstorms NXT text to the top of the text box. Change the second Display Block by removing the check from Clear so that both numbers are displayed for 5 seconds.

22 Currently the robot goes forward 1 rotation then forward 2 more rotations equaling 3 rotations. Change the Reverse Motor Block from 1 rotation to 3 rotations so that the robot completely exits the tunnel before displaying the Sensor Values. 3

23 Download and Run the Program.
The robot should display two light sensor values on the NXT screen.


Download ppt "Storing Values as Variables"

Similar presentations


Ads by Google