Upload
lemien
View
218
Download
2
Embed Size (px)
Citation preview
PROGRAMMINGBLOOKS“Data”
Constant BlockINDEX
Home
GeneralToolsProgramming Blocks
Data Logging
Action BlocksFlow BlocksSensor BlocksData BlockConstantVariableArrayLogicMathRoundCompareRangeTextRandom
Advanced Blocks
The Constant block lets you enter a value that you can use inseveral different places in your program. If you change thevalue of the constant, all places where you use the constant willget the updated value.
USING A CONSTANT
Mode Selector
Block Text Field
Output
Use the Mode Selector to select the type for the constant. See Data Types for moreinformation on the different Types.
MODES
Text - Numeric - Boolean - NumericArray - BooleanArray
Enter the value for the constant into the Block Text Field at the top of the block. Thisvalue is output to Value.
Example
This program uses a Constant block to provide the Power input for three differentMove Steering blocks. By changing the single value in the Constant block, all threeMove Steering blocks will get the new updated Power level.
Constant
Quick linksUsing a ConstantModesText - Numeric - Boolean -NumericArray - BooleanArrayInputs and Outputs
constant value in the block. To use the output, use a Data Wire to connect it toanother Programming Block.
Inputs andOutputs
Type Notes
Value (Depends on the blockmode)
Value of the constant in the BlockText Field
Value (Depends on the blockmode)
Value of the constant in the blockoutput
INPUTS AND OUTPUTS
The input of the Constant block is entered in the Block Text Field. The output is the
Variable BlockINDEX
Home
GeneralToolsProgramming Blocks
Data Logging
Action BlocksFlow BlocksSensor BlocksData BlockConstantVariableArrayLogicMathRoundCompareRangeTextRandom
Advanced Blocks
The Variable block lets you read or write a Variable in yourprogram. You can also create a new Variable and name it.
A Variable is a location in the memory of the EV3 Brick that canstore a data value. You can write to a Variable to store a datavalue. Later in the program, you can read from the Variable toaccess the stored value.
Each variable has a Type and a Name. The different Types are Numeric, Logic, Text,Numeric Array, and Logic Array. You can choose the Name of the variable, which isused to identify the variable.
See Data Types for more information on the different Types for a Variable.
The value of a variable can be changed while a program is running. Each time youwrite to a variable, any previous value is erased and replaced with the new value. Forexample, you could use a variable named “Max Light” to keep track of the highest lightintensity that your robot has measured from the Color Sensor so far. Each time therobot detects a higher value, it could write the new value into the “Max Light” variable.
ADDING A NEW VARIABLE
Mode Selector
Variable Selector
Value Input
To add a new variable to your project:
1. Insert or select a Variable block.2. Use the Mode Selector to choose Write mode.3. Choose the variable type (Numeric, Logic, Text, Numeric Array, or Logic Array).4. Click the Block Text Field on the top of the block and select “Add Variable”. The
New Variable dialog box will appear, as shown below.
Variable
Quick linksAdding a New VariableWriting to a VariableWriteReading from a VariableReadExamples Using VariablesInputs and Outputs
5. In the New Variable dialog box, enter a name for your variable and click OK. Avariable name can be a single letter, a word, several words, or any sequence ofletters and numbers.
Tips and Tricks
• Choosing a short variable name will make it easier to see the whole variable namewhen you use it in your program.
• Choose a name that will help you remember what the variable means, and how itis different from other variables in your program.
• Once you add a variable, it will be available for use in any of the programs in yourproject.
6. After you have added the variable, you can use the Variable block in Write mode toenter an initial Value for the variable.
WRITING TO A VARIABLE
Mode Selector
Variable Name
Input
Write
The Write mode lets you choose a variable that you have already added to yourprogram (see Adding a New Variable) and store a value in it.
To write to a variable:
1. Use the Mode Selector to choose the Write mode.2. Choose the Type of the variable.
3. Click the block text field to display the popup menu.4. Select Name of the variable that you want to use.
Tips and Tricks
You must choose the variable Type from the Mode Selector before choosing thevariable Name from the popup menu. The popup menu will only list the variables thatvariable Name from the popup menu. The popup menu will only list the variables thatmatch the selected Type.
5. You can now store a value into the variable using the Value input. You can eitherenter the value directly in the Value input, or you can use a Data Wire.
Tips and Tricks
You can write to a variable as many times as you want, but only the last value iskept. Writing a variable erases the previous value of the variable and replaces it withthe new value.
READING FROM A VARIABLE
Mode Selector
Variable Name
Output
Read
The Read mode lets you choose a variable that you have already added to yourprogram (see Adding a New Variable) and get the value stored in it.
To read from a variable:
1. Use the Mode Selector to choose the Read mode and the Type of the variable.2. Click the text field at the top of the block to display the popup menu, and select the
Name of the variable that you want to use.
Tips and Tricks
You must choose the variable Type from the Mode Selector before choosing thevariable Name from the popup menu. The popup menu will only list the variables thatmatch the selected Type.
3. You can now get the value of the variable from the Value output, and use this inyour program with a Data Wire.
Tips and Tricks
If you read from a variable that has never had a value written to it, the result will be0 for a Numeric variable, False for a Logic variable, empty text for a Text variable,and an empty array for a Numeric Array or Logic Array variable.
EXAMPLES USING VARIABLES
Example 1: Store a Value and Use it Later
This program uses a Variable block in Write mode to store a data value from theColor Sensor in the variable named “Light”, and then it uses a Variable block inRead mode to get the value and use it later in the program.
The program is designed to make a robot drive out and back. On the way back therobot will stop over a color that is at least as dark as the color it started over.
Tips and Tricks
The program above could also be done using a long Data Wire instead of a Variable,but for long programs, using Variables to store values can make the program easierto work with.
Example 2: Count Touch Sensor Presses
This program counts the number of times that a touch sensor is pressed anddisplays the count on the EV3 Display. It uses a Numeric variable named “Presses”to keep track of the number of times that the touch sensor has been pressed.
A Variable block in Write mode sets the initial value of “Presses” to 0 at thebeginning of the program. Inside the loop, the program adds 1 to the value storedin the variable each time the touch sensor is bumped. This is done by getting thecurrent value with a Variable block in Read mode, using a Math block and DataWires to add 1, and writing the result back into the variable with a Variable block inWrite mode. Finally, another Variable block in Read mode is used to get the currentvalue and display it.
INPUTS AND OUTPUTS
In Write mode, the Variable block has a single input. You can enter the input valuedirectly into the block. Alternatively, the value can be supplied by a Data Wire from theoutput of another Programming Block.
Input Type Notes
Value (Depends on the block mode) Value to store into the variable
In Read mode, the Variable block has a single output. To use the output, use a DataWire to connect it to another Programming Block.
Output Type Notes
Value (Depends on the block mode) Value of the selected variable
Array OperationsBlock
INDEX
Home
GeneralToolsProgramming Blocks
Data Logging
Action BlocksFlow BlocksSensor BlocksData BlockConstantVariableArrayLogicMathRoundCompareRangeTextRandom
Advanced Blocks
The Array Operations blockdoes operations on theNumeric Array and LogicArray data types. You cancreate an array, addelements, read and writeindividual elements, andget the length of an array.
See Data Types for moreinformation on arrays andother data types.
CHOOSE THE OPERATION
Mode Selector
Inputs
Output
Use the Mode Selector to choose the arrayoperation and the array type (Numeric or Logic).The Inputs and Outputs available will changedepending on the mode.
Modes: Append, Read at Index, Write at Index,Length
Quick linksChoose the Operation
Read at IndexWrite at Index
Inputs and Outputs
The Append mode adds an element to the end ofan array. You can also create a new array.
The Array In input specifies an existing array, andthe Value specifies the element to add to the endof the array. If you leave Array In blank, a newarray will be created with just the new element init.
Tips and Tricks
You can create a new array with multipleelements in it by entering an array directly intothe Array In input. See Data Types forinformation on how to enter an array directlyinto a block input.
The Array Out output will be a new arraycontaining the combined elements. The originalinput array is not affected.
This table shows some examples of the Append –Numeric mode.
Array In Value toAdd
Array Out
3 [3]
[1; 2; 3] 4 [1; 2; 3; 4]
[2; 1; 1;6]
1 [2; 1; 1; 6;1]
Read at Index
The Read at Index mode gets the value of anindividual element in an array. The Array In inputis the array to use. The element to get isspecified by the Index. The first element in anarray has an Index of 0, the second element hasan Index of 1, and so on. The value of the
Append
MODES
selected element is output in Value.
Tips and Tricks
The Index of the last element in an array with nelements is n-1.
This table shows some examples of the Read atIndex – Numeric mode.
Array In Index Value
[1; 2; 3] 0 1
[1; 2; 3] 2 3
Write at Index
The Write at Index mode changes the value of anindividual element in an array. The Array In inputis the original array. The element to change isspecified by the Index. The first element in anarray has an Index of 0, the second element hasan Index of 1, and so on.
The value to change the selected element to isspecified by the Value. A new array with theelement changed is output in Array Out. Theoriginal array in Array In is not affected.
This table shows some examples of the Write atIndex – Numeric mode.
Array In Index Value Array Out
[1; 2; 3] 0 5 [5; 2; 3]
[1; 2; 3] 2 0 [1; 2; 0]
Length
The Length mode gets the length of an array. Thelength of the array in the Array In input is outputin Length.
The length of an array is the number of elementsin the array. An empty array has a length of 0,and an array with a single element in has alength of 1.
INPUTS AND OUTPUTS
The inputs available for the Array Operationsblock will depend on the mode selected. You canenter the input values directly into the block.Alternatively, the input values can be supplied byData Wires from the outputs of otherProgramming Blocks.
Input Types Notes
ArrayIn
NumericArray, LogicArray
Array to operate on
Value Numeric,Logic
Value to append inAppend mode.
Value to change anelement to in Write atIndex mode.
Index Numeric Location of an arrayelement to access.0 = First element1 = Second elementLength – 1 = Lastelement
The output available will depend on the modeselected. To use an output, use a Data Wire toconnect it to another Programming Block.
Output Types Notes
ArrayOut
NumericArray, LogicArray
Array result of theoperation
Value Numeric,Logic
Value of an arrayelement in Read atIndex mode
Length Numeric Length of the array inLength mode
Logic Operations BlockINDEX
Home
GeneralToolsProgramming Blocks
Data Logging
Action BlocksFlow BlocksSensor BlocksData BlockConstantVariableArrayLogicMathRoundCompareRangeTextRandom
Advanced Blocks
The Logic Operations block does a Logic operation on its inputs,and outputs the result. A Logic operation takes inputs that areTrue or False, and produces a True/False output. The Logicoperations available are AND, OR, XOR, and NOT.
LOGIC OPERATIONS
Mode Selector
Inputs
Output
Choose the Logic operation you want to use by selecting a mode with the ModeSelector. The block will calculate the Result based on the Inputs, as shown in thistable.
Modes Inputs Used Result
AND A, B True if both A and B are True,otherwise False
OR A, B True if either A or B (or both) is True,False if both A and B are False
XOR A, B True if exactly one of A and B is True,False if both A and B are True,False if both A and B are False
NOT A True if A is False,False if A is True
Example
Logic
Quick linksLogic OperationsInputs and Outputs
This program will make a robot drive forward until either the Touch Sensor ispressed or the Color Sensor detects black. It uses the Logic OR mode to combinethe outputs of two sensor blocks into a single True or False result. A result of Truetells the loop to end, and then the robot is stopped.
INPUTS AND OUTPUTS
The inputs of the Logic Operations block provide the values for the Logic operation.Typically, you will supply both of the input values with Data Wires from the outputs ofother Programming Blocks.
Inputs Type Allowed Values Notes
A Logic True/False
B Logic True/False Not used in NOT mode
The output of the block gives the result of the operation. To use the output, use a DataWire to connect it to another Programming Block.
Output Type Notes
Result Logic Result of the Logic operation (True or False)
Math BlockINDEX
Home
GeneralToolsProgramming Blocks
Data Logging
Action BlocksFlow BlocksSensor BlocksData BlockConstantVariableArrayLogicMathRoundCompareRangeTextRandom
Advanced Blocks
The Math block does a math calculation on its inputs, andoutputs the result. You can do a simple math operation with oneor two inputs, or enter a formula with up to four inputs.
CHOOSE THE MATH OPERATION
Mode Selector
Inputs
Output
Choose the math operation you want to use by selecting a mode with the ModeSelector. After selecting the mode, you can choose values for the inputs. The inputsavailable will change depending on the mode.
SIMPLE MATH OPERATION MODES
The simple math operation modes calculate the Result with a single math operationusing one or two Inputs. These modes are shown in the table below.
Mode Inputs Used Output Result
Add A, B A + B
Subtract A, B A – B
Multiply A, B A × B
Divide A, B A ÷ B
Absolute Value A A if A ≥ 0 -A if A < 0The result is always ≥ 0.
Square Root A √A
Exponent A (base), N (Exponent) AN
Math
Quick linksChoose the Math OperationSimple Math Operation ModesAdvanced ModeInputs and Outputs
Exponent A (base), N (Exponent) AN
Advanced A, B, C, D A + B – C* D
Tips and Tricks
If the input values for a math operation result in an illegal operation, such as divisionby zero or the square root of a negative number, the output result will be an errorvalue. An error value may be interpreted as zero when used as the input for anotherprogramming block.
Example
This program subtracts 50 from the Reflected Light Intensity output of the ColorSensor block and uses the result as a Power input for a motor. This will make themotor turn backwards when the color sensor sees something dark and forwardswhen the sensor sees something light.
Advanced Mode
In the Advanced mode, the Math block can calculate a mathematical expression usingup to four inputs and several math operations in one step.
Use Data Wires to connect up to four Numeric values to the A, B, C, and D inputs.Unneeded inputs can be left blank or 0.
Click the Block Text Field at the top of the block to enter the mathematical expressionin text form to calculate. The expression can include the inputs by name as “A”, “B”,“C”, and “D”, numeric constants such as “50”, and math symbols such as “+”. You canalso use functions from the list displayed and additional parenthesis to change theorder of operations.
The result of the expression calculation is output in Result.
Example
In this program the Math block calculates a motor power using inputs from theColor Sensor and two Variables. The Reflected Light Intensity from the Color Sensoris wired to the A input, and the variables named “Gain” and “Power” are used for Band C. The expression “(A-50)*B+C” in the Math block subtracts 50 from the lightintensity, multiplies the result by value of “Gain”, and then adds the value of“Power”.
INPUTS AND OUTPUTS
The inputs of the Math block provide the values for the math calculation. You can enterthe input values directly into the block. Alternatively, the input values can be suppliedby Data Wires from the outputs of other Programming Blocks.
Inputs Type Notes
A Numeric First operand of a simple math operation
B Numeric Second operand of a simple math operation
A Numeric Base value in Exponent mode
N Numeric Exponent value in Exponent mode
C Numeric Input for Advanced mode
D Numeric Input for Advanced mode
The output of the Math block gives the result of the calculation. To use the output, usea Data Wire to connect it to another Programming Block.
Output Type Notes
Result Numeric Result of the math calculation
Round BlockINDEX
Home
GeneralToolsProgramming Blocks
Data Logging
Action BlocksFlow BlocksSensor BlocksData BlockConstantVariableArrayLogicMathRoundCompareRangeTextRandom
Advanced Blocks
The Round block rounds adecimal number to aninteger value. You canround a number up, down,or to the nearest integer.You can also truncate anumber to a certainnumber of decimal places.
CHOOSE THE MODE
Mode Selector
Inputs
Output
Use the Mode Selector to choose the type ofrounding to use. The different modes aredescribed below.
MODES
Round To Nearest
The Round To Nearest, Round Up, and RoundDown modes round a decimal number in theInput to an integer value in the Result. Round ToNearest uses standard rounding rules to round tothe nearest integer. Round Up always rounds up,and Round Down always rounds down.
Quick linksChoose the Mode
Round To NearestRound UpRound DownTruncateInputs and Outputs
Round Up
The Round To Nearest, Round Up, and RoundDown modes round a decimal number in theInput to an integer value in the Result. Round ToNearest uses standard rounding rules to round tothe nearest integer. Round Up always rounds up,and Round Down always rounds down.
Round Down
The Round To Nearest, Round Up, and RoundDown modes round a decimal number in theInput to an integer value in the Result. Round ToNearest uses standard rounding rules to round tothe nearest integer. Round Up always rounds up,and Round Down always rounds down.
This table shows example output results.
Input Round ToNearest
RoundUp
RoundDown
1.2 1 2 1
1.5 2 2 1
1.7 2 2 1
2.0 2 2 2
2.1 2 3 2
Truncate
The Truncate mode lets you eliminate any digitspast a specified decimal place in a decimalnumber. Any digits past the Number of Decimalsin the Input are eliminated in the Result. No otherdigits are affected (the output is not rounded).
This table shows example output results.
Input Number of Decimals Output
1.253 0 1
1.253 1 1.2
1.253 2 1.25
1.253 6 1.253
INPUTS AND OUTPUTS
You can enter the input values directly into theRound block. Alternatively, the values can besupplied by Data Wires from the outputs of otherProgramming Blocks.
Input Type Notes
Input Numeric Number to round
NumberofDecimals
Numeric Number of decimalplaces to keep inTruncate mode.
The output of the Round Block gives the result ofthe rounding. To use the output, use a Data Wireto connect it to another Programming Block.
Output Type Notes
Result Numeric Result of rounding theInput value
Compare BlockINDEX
Home
GeneralToolsProgramming Blocks
Data Logging
Action BlocksFlow BlocksSensor BlocksData BlockConstantVariableArrayLogicMathRoundCompareRangeTextRandom
Advanced Blocks
The Compare block compares two numbers to find out whetherthey are equal, or which number is greater. You can choose oneof six different comparisons. The output result is True or False.
COMPARE MODES
Mode Selector
Inputs
Output
Choose the type of comparison you want to use by selecting a mode with the ModeSelector. The block will calculate the Result output by comparing the two inputs A andB as shown in the table below.
Mode Inputs Used Output Result
Equal To A, B True if A = B, otherwise False
Not Equal To A, B True if A ≠ B, otherwise False
Greater Than A, B True if A > B, otherwise False
Less Than A, B True if A < B, otherwise False
Greater Than or Equal To A, B True if A ≥ B, otherwise False
Less Than or Equal To A, B True if A ≤ B, otherwise False
Compare
Quick linksCompare ModesInputs and Outputs
This block sequence tests to see if the value of the “power” variable is greater than100, and if so, it sets it to 100. The Compare block compares the value of thevariable to 100, and the Logic result is used by the Switch to choose whether tochange the value of the variable.
INPUTS AND OUTPUTS
The inputs of the Compare block are the two numbers to compare. You can enter theinput values directly into the block. Alternatively, the input values can be supplied byData Wires from the outputs of other Programming Blocks.
Input Type Notes
A Numeric First number for comparison
B Numeric Second number for comparison
The output of the block gives the result of the comparison. To use the output, use aData Wire to connect it to another Programming Block.
Output Type Notes
Result Logic Result of the comparison (True or False)
Example
Range BlockINDEX
Home
GeneralToolsProgramming Blocks
Data Logging
Action BlocksFlow BlocksSensor BlocksData BlockConstantVariableArrayLogicMathRoundCompareRangeTextRandom
Advanced Blocks
The Range block testswhether a number is insideor outside a specifiednumerical range. Theoutput result is True orFalse.
CHOOSE THE RANGE MODE
Mode Selector
Inputs
Output
Use the Mode Selector to choose whether to testif a number is Inside or Outside a range. TheRange block compares the Test Value input to therange specified by the Lower Bound and UpperBound inputs. The Result will be set to True orFalse, based on the result of the comparison.
MODES
Inside
The Inside mode sets the Result to True if theTest Value is inside the specified range.
Quick linksChoose the Range Mode
Inputs and Outputs
Outside
The Outside mode sets the Result to True if theTest Value is outside of the specified range.
INPUTS AND OUTPUTS
The inputs of the Range block provide the testvalue and the numeric range. You can enter theinput values directly into the block. Alternatively,the values can be supplied by Data Wires fromthe outputs of other Programming Blocks.
Input Type Notes
Test Value Numeric Number to test
LowerBound
Numeric Lowest number inrange
UpperBound
Numeric Highest number inrange
The output of the Range Block gives the result ofthe comparison. To use the output, use a DataWire to connect it to another Programming Block.
Output Type Notes
Result Logic Result of the rangecomparison (True orFalse)
Random BlockINDEX
Home
GeneralToolsProgramming Blocks
Data Logging
Action BlocksFlow BlocksSensor BlocksData BlockConstantVariableArrayLogicMathRoundCompareRangeTextRandom
Advanced Blocks
The Random block can output a random Numeric or Logic value.You can use the result of the Random block to make your robotrandomly choose from different actions.
CHOOSE THE OUTPUT TYPE
Mode Selector
Inputs
Output
Use the Mode Selector to choose whether to output a random Numeric value or arandom Logic value. After selecting the mode, you can choose the Inputs. The inputscontrol the range and probability of the Value output.
Modes: Numeric , Logic
MODES
Numeric
The Numeric mode outputs a random integer value. The Value will be set to a randominteger that is within the range specified by the Lower Bound and Upper Bound. Eachinteger value within this range is equally likely to be chosen.
Random
Quick linksChoose the Output TypeModesNumericLogicInputs and Outputs
This program makes a continuous series of tones of random frequencies in therange of 500 to 5000 Hz.
Logic
The Logic mode outputs a True or False value. The probability that a True value will beoutput is specified by the Probability of True input. This input is a percentage valuefrom 0 to 100. For example, a probability of 25 would result in a 25% chance that Trueis output and a 75% chance that False is output.
Example
This program makes the robot choose randomly between turning left and turningright, with a 50/50 chance of each.
Example
The inputs of the Random block control the range and probability of the output value.You can enter the input values directly into the block. Alternatively, the values can besupplied by Data Wires from the outputs of other Programming Blocks.
Input Type AllowedValues
Notes
LowerBounds
Numeric AnyNumber
Lower bound for Numeric output
UpperBounds
Numeric AnyNumber
Upper bound for Numeric output
Probability ofTrue
Numeric 0 to 100 Percentage probability of True resultfor Logic output
The output of the Random block gives the random value. To use the output, use aData Wire to connect it to another Programming Block.
Output Type Notes
Value Numeric or Logic Numeric in Numeric modeLogic in Logic mode
Tips and Tricks
To make your robot choose from more than two actions at random, you can use aRandom Block with the Numeric mode and a Switch Block in the Numeric mode. For
example, you could output a random number from 1 to 5 and use this value tochoose from five different cases in the Switch.
INPUTS AND OUTPUTS
The inputs of the Random block control the range and probability of the output value.
Text BlockINDEX
Home
GeneralToolsProgramming Blocks
Data Logging
Action BlocksFlow BlocksSensor BlocksData BlockConstantVariableArrayLogicMathRoundCompareRangeTextRandom
Advanced Blocks
The Text block can combine up to three text strings into onetext string.
MERGING TEXT STRINGS
Mode Selector
Inputs
Output
MODES
Merge
The Text block has a single mode named Merge. The input text strings A, B, and C willbe combined together (in this order) into a single text string. Any input that is notincluded will be skipped. The Result output is the combined text string.
Example
This program takes a Reflected Light Intensity measurement from the color sensorand uses it for the B input of the Text Block. The output number from the ColorSensor Block is automatically converted to text by a Data Wire Type Conversion(see Data Wires for more information). The Text Block then adds the text“Reflection is ” before the number and the text “%” after the number. The result isdisplayed as a single text string using a Display Block. If the Color Sensor output is50, the text “Reflection is 50%” would be displayed.
Text
Quick linksMerging Text StringsModesMergeInputs and Outputs
can enter the input values directly into the block. Alternatively, the values can besupplied by Data Wires from the outputs of other Programming Blocks.
Input Type Allowed Values Notes
A Text Any text Skipped if not included
B Text Any text Skipped if not included
C Text Any text Skipped if not included
The output of the Text Block gives the combined text string. To use the output, use aData Wire to connect it to another Programming Block.
Output Type Notes
Result Text String A followed by string B then string C
INPUTS AND OUTPUTS
The inputs of the Text Block provide up to three text strings to combine together. You