The EV3 has a three color LED that can display the following colors:
Green
Red
Orange
The examples shown below demonstrate different patterns in computer programming. Recognizing, understanding, and learning how to implement these patterns can help you to improve your programming skills.
The examples below are grouped into three main categories of behavior of the robot:
Regular Flashing (based on timers)
Irregular Flashing (using the random function)
Flashing based on Sensor Input (sensor values as well as event handlers)
Regular Flashing
Increment and Decrement
Use of the Not Operator "!" to Toggle Values
The Not Operator is a single character ("!") that can be used to reverse the Boolean value of a variable. Often this can be used to toggle a variable by assigning the Not variable such as:
a = !a
or:
Ok = !Ok
In the example below, a variable labeled showTheRedLED will contain a value of either true or false. The toggle function defined below will change the value each time the function is executed.
In the above example program you may have noticed that one (1) is being added to the showTheRedLED in the form of the expression:
showTheRedLED + 1
The value of the showTheRedLED variable is currently a Boolean value of either "true" or "false". In JavaScript (and many other languages), true and false are also represented by 1's and 0's. When adding a one (1) to the Boolean value we will end up with values that are 1's and 2's. This allows us to convert our true's and false's into red's and green's.
User-Defined Functions
JavaScript, like other languages, has built-in functions and also allows you to create your own user-defined functions. RobotJavaScript has about 50 built-in functions. The built-in functions perform some of the most basic actions/behaviors of the robot. You can create your own functions (i.e., user-defined functions) that are comprised of the simpler built-in functions as well as other user-defined functions.
You can create your own functions to perform tasks that you want the robot to perform. For example, you could create a function called "greenLED()" that would set the LED to the green color.
You need to use the keyword "function" when creating a function. This will be followed by the "identifier" (i.e., how your function shall be identified in the program). Two sets of symbols are then needed to complete the function definition. You will need to add a pair of parentheses and a pair of curly braces. The basic format of a function definition is shown below:
function myUserFunction() { }
Enter the statements that you want the robot to perform in between the curly braces. In the example below, we are going to simply set the LED using the built-in function: setLED(). To set the LED to a specific color, we enter the number of the color between the parentheses when 'calling' that function (i.e., when using that function in our program).
Use of Two Intervals
The setInterval() function will add the identifier (i.e., the name) of a function (greenLED or redLED in this example) to a stack of timers along with the number of milliseconds between each execution of that function. In the example below, the redLED() function will be executed once per second whereas the greenLED() function will be executed twice per second. Though the greenLED() function is executed twice as often as the redLED() function, the LED appears to give equal time to each since the LED is changed to red once per second--after the greenLED() function was executed.
The setInterval() function illustrates the use of a "call-back" function. The setInterval() function sets the interval of time between each subsequent execution of the "call-back" function. In other words, the "call-back" function is executed every "x" milliseconds. It is important to remember that, when entering a "call-back" function, only the identifier of the function (i.e., its name) is entered. You do not need to enter the parentheses that typically follow all function names.
Ternary Expressions
A Ternary expression is an abbreviated form of the if-then-else expression. The expression is created using the question mark and colon characters.
A condition (entered before the question mark) is evaluated. If the condition is true, then the expression that follows the question mark will be used, otherwise the expression that follows the colon will be used.
In the example below, the setLED() function will receive the value of either 2 or 1 depending on the value of the showTheRedLED variable.
Ternary expressions can include more than just literal numbers such as 1 and 2. You can execute functions (and even entire blocks of code) in the ternary expressions.
Modulus
The Modulus operator (shown as a single percent sign) returns the remainder of a division problem. For example, 5 divided by 2 would have a remainder of 1. Therefore, the expression "5 % 2" would have the value of 1 and "6 % 2" would have the value of 0. Any integer divided by 2 will have a remainder of either 0 or 1. Since the LED colors for green and red are 1 and 2, we can add 1 to the result of the modulus expression to obtain numbers that we need for the red and green colors.
In the example below, a counter is incremented each time the function is executed. The expression (counter % 2) will result in either 0 or 1. When we add 1 to the result of the modulus expression we can then obtain the colors of red and green.
The above code can be condensed a little further by allowing the increment to occur in the same statement as the setLED() so that we can set the LED and increment immediately.
Many math operators (+, -, *, /, %) can be combined with the assignment operator (=) to form a combination of operators such as: +=, -=, *=, /=, and %=. These combinations will perform both the math and the assignment operations. Instead of writing:
counter = counter + 2
you could write:
counter += 2
In our example of using modulus with 2 colors, we are going to want the equivalent expression of "counter = counter % 2" which can be written as:
counter %= 2
Because the result of the above expression will always be either 0 or 1, we will need to pre-increment the counter variable before using it as a parameter in the setLED() function.
Enumeration
JavaScript, unlike some other languages, does not include a keyword for enumeration. But an easy work-around is to create an object with enumerated properties. In the example below, a "colors" object is created with two properties ("green" and "red"). In this example, each property is assigned a unique value.
The use of enumeration enables the code to become easier to read.
Instead of:
setLED(showTheRedLED ? 2 : 1)
we can write:
setLED(showTheRedLED ? colors.red : colors.green)
Using a Switch Expression
In this example, a switch expression is used to select different cases (depending on the value of the variable being used in the switch).
Array
This is just an example of how to create an empty array and then push numbers onto the array. The elements of the array will contain values from 0 through 999. The array is then scanned and the setLED() function is passed the value of the array element again using "% 2 + 1".
Logical Or Expressions
Computers and robots are very efficient. They are so efficient, that they will refuse to spend time (and energy) evaluating expressions completely if the first part of an expression can determine the result of the entire expression. The Logical Or Expression considers if the value of either of two given expressions is true. As an example
a or b (written in computer language as: a || b )
If either a or b is true then the expression is evaluated to be true. If "a" is true, then the expression of "b" is never evaluated. The expression of "b" is only evaluated if "a" is false. The reason why the evaluation of "b" is dependent on "a" is because if "a" is true, then the entire expression evaluates to true regardless of the value of "b".
This conditional evaluation of an expression is similar to what occurs in an if-then-else expression. A previous example used the following if-then-else:
If we eliminated the true part of the expression, we would get:
if(showTheRedLED) else setLED(colors.green)
written on one line as:
if(showTheRedLED) else setLED(colors.green)
Given the way that the Logical Or Expression is evaluated, we can drop the if and replace the else as in:
showTheRedLED || setLED(colors.green)
Using a Logical Or Expression in this way may seem like an exercise in obfuscation. But it could have very practical uses in robotics. For example, if a robot must perform a series of steps in order to accomplish a mission you could create a function for each step and return a true or false depending on the success of that step. Program control will proceed starting at step 1 and continue until all steps are successfully completed. Any failure will result in the immediate stop at that step and the robot would then report the error. For example:
In the above statement the Logical Or Expression will require that all of the steps be completed successfully OR the error will be reported.
If an error occurs on step2() then steps 3 and 4 will not be evaluated.
Randomized Flashing
The random() function returns a random number between 0 and 1. In this example, if the random number is greater than 0.5, then the LED will be toggled.