Project 1.02 Simple LED Chase

In this project we will use all four LEDs to blink a chase pattern. This example shows a very simple way to create a pattern using digitalWrites and delays.

Project Code:

/////////////////////////////////////////////////
// Project 1.02 LED Chase

byte LED1 = 13;
byte LED2 = 6;
byte LED3 = 7;
byte LED4 = 8;

void setup() {
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
}

void loop() {
  digitalWrite(LED4, LOW);
  digitalWrite(LED1, HIGH);
  delay(250);
  digitalWrite(LED1, LOW);
  digitalWrite(LED2, HIGH);
  delay(250);
  digitalWrite(LED2, LOW);
  digitalWrite(LED3, HIGH);
  delay(250);
  digitalWrite(LED3, LOW);
  digitalWrite(LED4, HIGH);
  delay(250);
}
/////////////////////////////////////////////////

*If you’re copying and pasting the code, or typing from scratch, delete everything out of a new Arduino sketch and paste / type in the above text.

Here we set the four variables needed to represent the pins that the LEDs are attached to:

byte LED1 = 13;
byte LED2 = 6;
byte LED3 = 7;
byte LED4 = 8;

Next, we need to tell the microcontroller that these pins are outputs:

void setup() {
  pinMode(LED1, OUTPUT); 
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
}

After that, we can sequentially turn on and off the LEDs to represent a chase pattern. To do this, we have to turn the last LED off, the next LED on, and then wait.

void loop() {
  digitalWrite(LED4, LOW);
  digitalWrite(LED1, HIGH);
  delay(250);
  digitalWrite(LED1, LOW);
  digitalWrite(LED2, HIGH);
  delay(250);
  digitalWrite(LED2, LOW);
  digitalWrite(LED3, HIGH);
  delay(250);
  digitalWrite(LED3, LOW);
  digitalWrite(LED4, HIGH);
  delay(250);
}

What you may have noticed about the code above is that something happened over and over again. Usually, when things happen over and over again, it is a sign that it should be in some kind of loop and/or function.

Previous
Previous

Project 1.03 Analog Write

Next
Next

Project 1.01 Blink x2