Learn Arduino with Olympia Circuits
Learn Arduino
  • Home
    • Get Started
    • How to Use This Site
  • Electronics
    • The Basics
    • Electricity Flows like Water
    • Electronic Components
    • The Arno Board
  • Programming
    • The Basics
    • setup and loop Blocks
    • Variables and Arrays
    • Connecting with the Pins
    • Flow Control >
      • if Statement
      • Loops
      • Delays
    • Functions
    • Serial Communication
    • USB
    • Some Thoughts
  • Projects
    • Projects 1 >
      • 1.01: Blink
      • 1.02 Blink x2
      • 1.03 Blink Faster
      • 1.04 LED Chase!
      • 1.05 Wait To Blink
      • 1.06 Blink a Little Faster Now
      • 1.07 LED Fade
      • 1.08 RGB Blink
      • 1.09 Change RGB Color with SW1
      • 1.10 Fade RGB Colors
      • 1.11 Reaction Time Game
    • Projects 2 >
      • 2.01 Hello World
      • 2.02 Talk Back
      • 2.03 ASCII Values
      • 2.04 Ski Game
      • 2.05 Demonstration of the String Object
    • Projects 3 >
      • 3.01 Read the Potentiometer
      • 3.02 ASCIIbet Soup
      • 3.03 Potentiometer sets LED Brightness
      • 3.04 Potentiometer Sets Blink Rate
      • 3.05 LED Chase, Part II
    • Projects 4 >
      • 4.01 Bringing the Piezo to Life
      • 4.02 Controlling the Piezo with a Function
      • 4.03 Piezo C Major
      • 4.04 Piezo Greensleaves
      • 4.05 Piezo Metronome
      • 4.06 Piezo as an Input
      • 4.07 Piezo as an Input 2
      • 4.08 Metronome II
      • 4.09 Piezo Playback
      • 4.10 Piezo Fireworks
      • 4.11 Piezo Mosquito
    • Projects 5 >
      • 5.01 The Phototransistor
      • 5.02 Light and Sound
      • 5.03 Light and Sound II
    • Projects 6 >
      • 6.01 EEPROM
      • 6.02 I2C Address Scan
      • 6.03 Read the I2C Temperature Sensor
      • 6.04 High Temperature Alarm
    • Projects 7 >
      • 7.01 Arno Phone Home
      • 7.02 Keyboard Alphabet
      • 7.03 Move Mouse
      • 7.04 Draw Squares
    • Special Projects >
      • Bike Light Demo
  • References
    • Arno Pin Key
    • Arno Schematic
    • Project Index

Project 3.05 LED Chase, Part II 

In this project, we take control of the LED Chase!  (Project 1.03) using the thumbwheel potentiometer and the SW1 button.  We use the potentiometer to adjust the speed of the chase and we reverse its direction by pressing SW1.

Concepts:  ADC, analogRead, arrays, debounce 

Circuits:
  • Circuit 1
  • Circuit 2
  • Circuit 7


  • Select Sketch

    ///////////////////////////////////////////////////
    //Project 3.05 LED Chase II
    int LEDS[4] = {
    6,7,8,13};
    int isPress = HIGH;
    int SW1 = 1;
    int wait = 60;
    int potentiometer = A0;
    int potRead = 0;
    int steps = 1;
    int thisLED = 0;
    long now = 0;
    long lastPressed = 0;
    void setup(){
    for(int k = 0; k < 4; k++){
    pinMode(LEDS[k],OUTPUT);
    }
    pinMode(potentiometer,INPUT);
    pinMode(SW1,INPUT);
    }
    void loop(){
    isPress = digitalRead(SW1);
    potRead = analogRead(potentiometer)/4;
    if(millis() > now + potRead){
    digitalWrite(LEDS[thisLED],LOW);
    thisLED = thisLED + steps;
    if(thisLED < 0) thisLED = 3;
    if(thisLED > 3) thisLED = 0;
    digitalWrite(LEDS[thisLED],HIGH);
    now = millis();
    }
    if(isPress==LOW && millis() > lastPressed + 1000){
    steps = steps*-1;
    lastPressed = millis();
    }
    }
    ///////////////////////////////////////////////////

    Arrays help to simplify this program.  The pin numbers for the single LEDs are contained in the array LEDS:

    int LEDS[4] = {6,7,8,13};

    The potentiometer value is used to set when to switch to the next LED.  The variable now records when the LEDs where last switched.  The next switch occurs when millis() is greater than now + potRead.  We advance to the next LED in the array using the variable steps which can be either 1 (move forward in the array) or -1 (move backward):

      potRead = analogRead(POT1)/4;

      if(millis() > now + potRead){

        digitalWrite(LEDS[thisLED],LOW);

        thisLED = thisLED + steps;

        if(thisLED < 0) thisLED = 3;

        if(thisLED > 3) thisLED = 0;

        digitalWrite(LEDS[thisLED],HIGH);

        now = millis();

      }

    The variable steps switches between 1 and -1 when SW1 is pressed.  This changes the direction of rotation. We use the variable lastPress to record when steps was last switched.  We debounce SW1 by not allowing it to change steps more than once per second:

    if(isPress==LOW && millis() > lastPressed + 1000){

        steps = steps*-1;

        lastPressed = millis();

      }

    Back to Projects 3

    Copyright Olympia Circuits LLC 2014. All Rights Reserved.