page14
Blink Program with a Variable
1) Go back to the “Blink” sketch and edit it on the screen so it looks like the example on the right (changes are shown in bold).
2) Verify the sketch using the verify button.
3) Upload the sketch to the Arduino and check that it works.
int waitTime;
void setup() {
// initialize the digital pin as an output:
waitTime = 500;
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // set the LED on
delay(waitTime); // wait for delay specified
digitalWrite(LED_BUILTIN, LOW); // set the LED off
delay(waitTime); // wait for delay specified
}
Variable declaration: This line in the sketch creates a new variable called “waitTime”. A variable is a labelled box which is used to contain data used in the program. The data can be read or changed during the program.
In this case waitTime will contain integer values. We use the word “int” to tell the computer we want waitTime to be an integer.
A variable declared outside any function can be seen by all functions in a sketch.
Variable assignment: This statement puts a value (500) in to the variable waitTime.
Variable use: We can now use the variable name to mean the contents instead of actually writing “500”.