-
Type: “myservo.write(180);” after “delay(2000);”
STEM Connections
Remember, the write function determines the angle to which a standard Servo moves. So, the line of code “myservo.write(180);” tells the standard Servo to move to 180°.
Before the Arduino board runs this line of code, the Servo was at angle 0°. When the Arduino board runs this line of code, it will cause the standard Servo to rotate to the right from 0° to 180°.
In the previous two steps, you told the Servo to move to 0°, then pause at that angle for 2000 milliseconds (or 2 seconds). After adding this line, the Servo will move to 180°.
Again, the write function will have a different effect on the continuous Servo, but we will explore that more in later steps.
At the Thinkabit Lab, we provide an in-depth explanation of what it means to add the line of code, “myservo.write(180);” after “delay(2000);” as follows:
-
Explain that without adding this line of code with a different command, the standard Servo would remain at 0° indefinitely. A different command is needed for the Servo to move. Like the LED Blink code, where there were 2 states: State 1: LED on and State 2: LED off. Likewise, we also need (at least) 2 states for the standard Servo to see a back and forth motion. Without giving a new command to the Servo, the previous line of ‘delay’ would be unnecessary because there wouldn’t be another action to switch to.
-
Explain that the Arduino board reads through the code quickly, without the next delay, it will look like the Servo remains at 0° because the Servo needs time to move to the desired position.
If you are using the Thinkabit Lab Notebook:
Have students fill in the next blank on page 15:
myservo.write(180); Servo action '180'
Explain to students that this action varies depending on which Servo we are using. Alternatively, you could write "small Servo to 180°, big Servo spin fast right"
Alternatively, you can write the entire code on the computer and fill in the notebook at the end.
‘write’ didn’t turn orange
-
Check the spelling of the word “write” and make sure that no letters are capitalized.
-
Check to make sure that there is a period between “myservo” and “write”.
-
Check to make sure that there is a semicolon after “myservo.write(0);”
-
If it changed colors to a different color, the Arduino software may have been updated. As long as it is colored, the code should still work properly.