Microcontroller programs generally run in a loop, meaning that one or more statements are repeated over and over again. Remember that the loop function automatically repeats any code in its block (the statements in between its curly braces). Let’s try moving Serial.print("Hello!"); to the loop function. To slow down the rate at which the messages repeat, let’s also add a pause with the built-in delay(ms) function.
- Save HelloMessage as HelloRepeated.
- Move Serial.print("Hello!"); from setup to the loop function.
- Add delay(1000); on the next line.
- Compare your changes to the figure below and verify that they are correct.
- Upload the sketch to the Arduino and then open the Serial Monitor again.
The added line delay(1000) passes the value 1000 to the delay function’s ms parameter. It’s requesting a delay of 1000 milliseconds. 1 ms is 1/1000 of a second. So, delay(1000) makes the sketch wait for 1000/1000 = 1 second before letting it move on to the next line of code.