Tab Content
Tab Title
Instructions
Tab Content
  1. Type: “delay(2000);” after “myservo.write(0);” and before the last curly bracket.

 

STEM Connections

This delay function will cause the program to pause and not read the next line for 2000 milliseconds (or 2 seconds). Meanwhile, the Servo has been instructed to move to 0°, which it will do and then remain there until it receives another command.

Remember, the loop function runs any lines of code that are between the curly brackets that follow it repeatedly. So, the program will not end after it moves the Servo to 0° and pauses for 2000 milliseconds. Because this line of code is part of the loop function, the program will return to the first line of code in the loop, which instructs the standard Servo to move to 0°.  Since the Servo is already at 0°, if we were to upload this code now the Servo would stay at 0° forever since we haven't told it to move to a different position.

Tab Title
Implementation
Tab Content

At the Thinkabit Lab, we have found that delay is often a hard concept for students to understand and address this by doing the following:

  • Ask students: In the LED Blink code, what caused the Arduino to pause before reading the next line of code?
  • Students should answer: a delay.
  • Explain that while the program pauses during this delay before it reads the next line of code, all previous actions will continue. For example, if the Servo was signaled to go to 0°, then it will continue to receive that signal until given another one, thus remaining at 0°.
  • Explain that although it might seem that the code is telling the Servo how long to stay at each position, it is not actually giving any instructions to the Servo. It is the time the Arduino waits before giving the Servo its next command. (Keep in mind that this concept gets especially difficult when you start controlling multiple objects at the same time).

 

If you are using the Thinkabit Lab Notebook:
 

Have students fill in the next blank on page 15:
delay(2000);                wait 2 seconds      

Alternatively, you can write the entire code on the computer and fill in the notebook at the end.

Tab Title
Troubleshooting
Tab Content

‘delay’ didn’t turn orange

  1. Check the spelling of the word “delay” and make sure that no letters are capitalized.

  2. 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.

Instruction Category
Video