Menu
CoddyTech

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 ms

Change 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 icon

Challenge

Easy

Blink the LED on pin 13 with a variable:

  • at the top, make int waitTime = 300;
  • use waitTime in both delay() calls
  • in setup(), print the value of waitTime

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);
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in First Circuits

Practice on your own: Arduino playground