In this exercise we'll use the Open Roberta editor. Make sure that you see this picture in the upper right corner of your programming environment. The editor can be found on our website at the top of the navigation bar: Let's start - Editor.
First, you create a variable. Click on the + symbol in the start block. Then you have got to name the variable, e. g. random.
In the menu under Control, select the following block to play your following code in an infinite loop.
First select the if - do block in the control menu to set the condition. Then add get... gesture? Block from the Sensors menu to control the position sensor. In the selection you now change upright to on the back.
Next, select the set random block from the variables menu. To these you add the random integer value between 1 and 100 block, from the math menu, change the 1 to a 0 and the 100 to a 4.
Now the value of the random variable is randomly set to a number from zero to four.
Now add another if - do block and add the block = from the logic menu to it. Now fill the block with the random variable, from the variables menu and a 0 to set the condition for the value zero of the variable.
You set the color for your first condition by inserting the turn LED on color" block from the Action** menu and selecting a color from the selection.
You can duplicate this if-do block four times. To do so, right click on the if - do block and choose copy. For the other random numbers 1-4 of your duplicated blocks you can now choose the colors. Do the following in each of the 4 blocks:
- Change the random number.
- Choose a color.
- Put your blocks together.
To make the colors change slower, you can add a pause at the end of your infinite loop. For this you use the wait ms block, which you find under control. Try different waiting periods.
Done - download your script now and give it a try!
You can use even more colors by increasing the random numbers: In the block of random integer values between 0 and 4 you can enter a larger number. Then you have to create more *if - do blocks, for which you can set the LED colors.