Your First Variable
Part of the First Circuits section of Coddy's Arduino journey. Lesson 10 of 22.
A variable is a named box that holds a value. This line makes one:
int waitTime = 300;int is the type: the box holds a whole number (an integer). waitTime is its name, and 300 is the value put in it. You can then write the name wherever you would write the number:
delay(waitTime); // waits 300 msChange the value in one place, and every line that uses the name follows.
Make the variable at the top of the sketch, above setup(). A variable made inside setup() only exists inside setup(), so loop() could not use it.
A name is made of letters, digits and _, with no spaces, and upper and lower case count: waitTime and waittime are two different names. Serial.println(waitTime) prints the value, 300; Serial.println("waitTime") prints the word.
Challenge
EasyBlink the LED on pin 13 with a variable:
- at the top, make
int waitTime = 300; - use
waitTimein bothdelay()calls - in
setup(), print the value ofwaitTime
Press Run, then Check. Try changing 300 to another number and run it again.
Try it yourself
// make the variable here
void setup() {
pinMode(13, OUTPUT);
Serial.begin(9600);
// print the variable
}
void loop() {
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in First Circuits
2Talking to Your Computer
Hello, Serial MonitorPrint Versus PrintlnPrinting NumbersYour First VariableCounting UpWhen an Int Runs OutPractice on your own: Arduino playground