Scratch in Arduino Mode


Under Arduino mode, the instruction blocks will be tranfer to Arduino C++ into Arduino IDe, and user could modify code in Arduino IDe, and then upload the code into Robot. In that case, the Robot is running offline and it can not interact with Scratch’s stage.

In this example, we want to turn on an LED whenever you press tactile switch 1 of evive. The logic is similar to the previous example, you just have to change the hat block to Arduino Program and boolean block.

We will be continuously checking if the tactile switch is pressed. This will be either true or false. Now, we will use conditional statements, which will take decision to execute particular statements according to the result. In our case, this is if-else, where if the result is true, LED will be ON, else LED will be OFF.

Tactile Switch Logic

The next step is to express your logic in a way so that the computer can understand it.

  • Whenever you want to run a code in Arduino Mode, you have to start with a Arduino Program Hat block which is present in Arduino Extension in Robot Palette. Drag the block and drop the block in the script using mouse.
  • Now, we want to run the code forever. For these type of functions there is a C block named ‘Forever’, which runs the code inside it continuously in loop. Drag the block and snap it below the hat block.
  • To check the condition drag drop ‘If – Else’ block and place it inside forever block.
  • To check if the tactile switch is pressed or not we will use ‘read digital pin’ from Arduino Extension in Robot Palette. It gives true when Space key is pressed and false if the Space is not pressed. Place it inside the if else block.
  • If want to turn ON the LED, digital pin 13 should be HIGH, which can be done using ‘set digital pin’ block. Drag and snap the block inside if block. Next to the pin inside the block you can see white space where you have to input the pin number (13 in this case). To turn the pin HIGH, select the option at the end of the block.
  • Similarly, turn off the LED in the else block. The final script is given below.

Right click on the hat block and click upload to Arduino. Upload the code and you are done.