≡ Menu

LED Patterns Using DIP Switch and Arduino

When I posted the “LED Control Using DIP Switch” sketch last year (a simple setup the turned on the LED corresponding to that switch position), I also had a slightly modified version of it in which the DIP switch controlled six different light patterns on the LEDs (scroll right, left, in, out, back and forth and random). It presented a “cleaned-up” version of the code using for loops and compared it to the “long-hand” version, showing the trade-off between ease of understanding and conciseness. Except that… I forgot to post it.

Last week someone contacted me asking a question about a similar project he is working on and when I wanted to refer him to this modified sketch I realized it wasn’t on the blog. (Here’s the original sketch and schematic for reference).

What follows below is the missing blog post (not anymore), a comparison between the more readable sketch (easier for beginners to understand) and the more concise version, in a series of snippets showing the main differences between the two versions of the sketch.

The concise version generates a binary sketch that occupies approximately 25% less memory and is almost half as long in lines of code. The entire source code listings are at the very bottom of this post.

Note: the line above each snippet reflects the modification that shortened the sketch.

1) not using #define directives for the LED and switch pins on the Arduino in order to use for loops:

    } else {
     // default: off
     digitalWrite(LED1, LOW);
     digitalWrite(LED2, LOW);
     digitalWrite(LED3, LOW);
     digitalWrite(LED4, LOW);
     digitalWrite(LED5, LOW);
     digitalWrite(LED6, LOW);
   }
    } else {
     // default: off
     for (i = 13; i >= 8; i--) {
       digitalWrite(i, LOW);
     }

 

2) using a state variable array (and consequently a for loop) as opposed to individual state variables:

s1state = digitalRead(S1);
s2state = digitalRead(S2);
s3state = digitalRead(S3);
s4state = digitalRead(S4);
s5state = digitalRead(S5);
s6state = digitalRead(S6);
for (i = 0, j = 7; i < 6, j >= 2; i++, j--) {
   state[i] = digitalRead(j);
}

 

3) if (x) versus if (x == 1) (when x is either 0 or 1, then the (x == 1) expression can be written as simply (x)):

    } else if (s5state == 1) {
     // scroll back and forth
    } else if (state[4]) {
     // scroll back and forth

 

4) long, repetitive code to randomly turn on or not each LED (for a random duration up to 300 milliseconds) replaced with for loop:

} else if (s6state == 1) {
  // random
  randomSeed(analogRead(3));
  onoroff = random(0, 2);
  millisecs = random(0, 301);
  digitalWrite(LED1, onoroff);
  delay(millisecs);
  randomSeed(analogRead(3));
  onoroff = random(0, 2);
  millisecs = random(0, 301);
  digitalWrite(LED2, onoroff);
  delay(millisecs);
  randomSeed(analogRead(3));
  onoroff = random(0, 2);
  millisecs = random(0, 301);
  digitalWrite(LED3, onoroff);
  delay(millisecs);
  randomSeed(analogRead(3));
  onoroff = random(0, 2);
  millisecs = random(0, 301);
  digitalWrite(LED4, onoroff);
  delay(millisecs);
  randomSeed(analogRead(3));
  onoroff = random(0, 2);
  millisecs = random(0, 301);
  digitalWrite(LED5, onoroff);
  delay(millisecs);
  randomSeed(analogRead(3));
  onoroff = random(0, 2);
  millisecs = random(0, 301);
  digitalWrite(LED6, onoroff);
  delay(millisecs);
}
} else if (state[5]) {
  // random
  for (i = 13; i >= 8; i--) {
    randomSeed(analogRead(i - 8));
    onoroff = random(0, 2);
    millisecs = random(0, 301);
    digitalWrite(i, onoroff);
    delay(millisecs);
  }
}

 

Here’s the full long version:

  • Binary sketch size: 3654 bytes
  • Code size was sacrificed in order to improve readability for beginners
  • Sketch length: 205 lines
  // www.TinkerHobby.com
  // Natalia Fargasch Norman
  // LED control via DIP switches

  // Arduino pins used for the LEDs
  #define LED1 13
  #define LED2 12
  #define LED3 11
  #define LED4 10
  #define LED5 9
  #define LED6 8

  // Arduino pins used for the switches
  #define S1 7
  #define S2 6
  #define S3 5
  #define S4 4
  #define S5 3
  #define S6 2

  // State of each switch (0 or 1)
  int s1state;
  int s2state;
  int s3state;
  int s4state;
  int s5state;
  int s6state;

  // Random values for LED state and delay
  long onoroff;
  long millisecs;

  void setup() {
    // pins for LEDs are outputs
    pinMode(LED1, OUTPUT);
    pinMode(LED2, OUTPUT);
    pinMode(LED3, OUTPUT);
    pinMode(LED4, OUTPUT);
    pinMode(LED5, OUTPUT);
    pinMode(LED6, OUTPUT);
    // pins for switches are inputs
    pinMode(S1, INPUT);
    pinMode(S2, INPUT);
    pinMode(S3, INPUT);
    pinMode(S4, INPUT);
    pinMode(S5, INPUT);
    pinMode(S6, INPUT);
  }

  void loop() {
    s1state = digitalRead(S1);
    s2state = digitalRead(S2);
    s3state = digitalRead(S3);
    s4state = digitalRead(S4);
    s5state = digitalRead(S5);
    s6state = digitalRead(S6);
    if (s1state == 1) {
      // scroll right
      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);
      digitalWrite(LED4, LOW);
      digitalWrite(LED5, HIGH);
      delay(250);
      digitalWrite(LED5, LOW);
      digitalWrite(LED6, HIGH);
      delay(250);
      digitalWrite(LED6, LOW);
    } else if (s2state == 1) {
      // scroll left
      digitalWrite(LED6, HIGH);
      delay(250);
      digitalWrite(LED6, LOW);
      digitalWrite(LED5, HIGH);
      delay(250);
      digitalWrite(LED5, LOW);
      digitalWrite(LED4, HIGH);
      delay(250);
      digitalWrite(LED4, LOW);
      digitalWrite(LED3, HIGH);
      delay(250);
      digitalWrite(LED3, LOW);
      digitalWrite(LED2, HIGH);
      delay(250);
      digitalWrite(LED2, LOW);
      digitalWrite(LED1, HIGH);
      delay(250);
      digitalWrite(LED1, LOW);
    } else if (s3state == 1) {
      // scroll in
      digitalWrite(LED1, HIGH);
      digitalWrite(LED6, HIGH);
      delay(250);
      digitalWrite(LED1, LOW);
      digitalWrite(LED6, LOW);
      digitalWrite(LED2, HIGH);
      digitalWrite(LED5, HIGH);
      delay(250);
      digitalWrite(LED2, LOW);
      digitalWrite(LED5, LOW);
      digitalWrite(LED3, HIGH);
      digitalWrite(LED4, HIGH);
      delay(250);
      digitalWrite(LED3, LOW);
      digitalWrite(LED4, LOW);
    } else if (s4state == 1) {
      // scroll out
      digitalWrite(LED3, HIGH);
      digitalWrite(LED4, HIGH);
      delay(250);
      digitalWrite(LED3, LOW);
      digitalWrite(LED4, LOW);
      digitalWrite(LED2, HIGH);
      digitalWrite(LED5, HIGH);
      delay(250);
      digitalWrite(LED2, LOW);
      digitalWrite(LED5, LOW);
      digitalWrite(LED1, HIGH);
      digitalWrite(LED6, HIGH);
      delay(250);
      digitalWrite(LED1, LOW);
      digitalWrite(LED6, LOW);
    } else if (s5state == 1) {
      // scroll back and forth
      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);
      digitalWrite(LED4, LOW);
      digitalWrite(LED5, HIGH);
      delay(250);
      digitalWrite(LED5, LOW);
      digitalWrite(LED6, HIGH);
      delay(250);
      digitalWrite(LED6, LOW);
      digitalWrite(LED5, HIGH);
      delay(250);
      digitalWrite(LED5, LOW);
      digitalWrite(LED4, HIGH);
      delay(250);
      digitalWrite(LED4, LOW);
      digitalWrite(LED3, HIGH);
      delay(250);
      digitalWrite(LED3, LOW);
      digitalWrite(LED2, HIGH);
      delay(250);
      digitalWrite(LED2, LOW);
    } else if (s6state == 1) {
      // random
      randomSeed(analogRead(3));
      onoroff = random(0, 2);
      millisecs = random(0, 301);
      digitalWrite(LED1, onoroff);
      delay(millisecs);
      randomSeed(analogRead(3));
      onoroff = random(0, 2);
      millisecs = random(0, 301);
      digitalWrite(LED2, onoroff);
      delay(millisecs);
      randomSeed(analogRead(3));
      onoroff = random(0, 2);
      millisecs = random(0, 301);
      digitalWrite(LED3, onoroff);
      delay(millisecs);
      randomSeed(analogRead(3));
      onoroff = random(0, 2);
      millisecs = random(0, 301);
      digitalWrite(LED4, onoroff);
      delay(millisecs);
      randomSeed(analogRead(3));
      onoroff = random(0, 2);
      millisecs = random(0, 301);
      digitalWrite(LED5, onoroff);
      delay(millisecs);
      randomSeed(analogRead(3));
      onoroff = random(0, 2);
      millisecs = random(0, 301);
      digitalWrite(LED6, onoroff);
      delay(millisecs);
    } else {
      // default: off
      digitalWrite(LED1, LOW);
      digitalWrite(LED2, LOW);
      digitalWrite(LED3, LOW);
      digitalWrite(LED4, LOW);
      digitalWrite(LED5, LOW);
      digitalWrite(LED6, LOW);
    }
  }

 

And here’s the full “cleaned-up” version:

  • Binary sketch size: 2826 bytes
  • Sketch length: 119 lines
  // www.TinkerHobby.com
  // Natalia Fargasch Norman
  // LED control via DIP switches

  // Arduino pins used for the LEDs
  // LED1 13
  // LED2 12
  // LED3 11
  // LED4 10
  // LED5 9
  // LED6 8

  // Arduino pins used for the switches
  // S1 7
  // S2 6
  // S3 5
  // S4 4
  // S5 3
  // S6 2

  // State of each switch (0 or 1)
  int state[6];

  // Random values for LED state and delay
  long onoroff;
  long millisecs;

  // loop counters
  int i, j;

  // delay
  int d = 250;

  void setup() {
    // pins for LEDs are outputs
    // LEDs 1-6 on pins 13-8
    for (i = 13; i >= 8; i--) {
      pinMode(i, OUTPUT);
    }
    // pins for switches are inputs
    // switches 1-6 on pins 7-2
    for (i = 7; i >= 2; i--) {
      pinMode(i, INPUT);
    }
  }

  void loop() {
    for (i = 0, j = 7; i < 6, j >= 2; i++, j--) {
      state[i] = digitalRead(j);
    }

    if (state[0]) {
      // scroll right
      for (i = 13; i >= 8; i--) {
        digitalWrite(i, HIGH);
        delay(d);
        digitalWrite(i, LOW);
      }

    } else if (state[1]) {
      // scroll left
      for (i = 8; i <= 13; i++) {
         digitalWrite(i, HIGH);
         delay(d);
         digitalWrite(i, LOW);
       }

   } else if (state[2]) {
       // scroll in
       // light up LEDs on pins i and 8+(13-i)
       for (i = 13; i >= 11; i--) {
        digitalWrite(i, HIGH);
        digitalWrite(21 - i, HIGH);
        delay(d);
        digitalWrite(i, LOW);
        digitalWrite(21 - i, LOW);
      }

    } else if (state[3]) {
      // scroll out
      // light up LEDs on pins i and 8+(13-i)
      for (i = 11; i <= 13; i++) {
         digitalWrite(i, HIGH);
         digitalWrite(21 - i, HIGH);
         delay(d);
         digitalWrite(i, LOW);
         digitalWrite(21 - i, LOW);
       }

    } else if (state[4]) {
       // scroll back and forth
       for (i = 13; i >= 8; i--) {
        digitalWrite(i, HIGH);
        delay(d);
        digitalWrite(i, LOW);
      }
      for (i = 9; i <= 12; i++) {
        digitalWrite(i, HIGH);
        delay(d);
        digitalWrite(i, LOW);
      }

    } else if (state[5]) {
       // random
       for (i = 13; i >= 8; i--) {
        randomSeed(analogRead(i - 8));
        onoroff = random(0, 2);
        millisecs = random(0, 301);
        digitalWrite(i, onoroff);
        delay(millisecs);
      }      

    } else {
      // default: off
      for (i = 13; i >= 8; i--) {
        digitalWrite(i, LOW);
      }
    }
  }

Check out the video of this sketch in action.

Digiprove sealCopyright secured by Digiprove © 2014 Natalia Fargasch Norman
{ 2 comments… add one }
  • I thought i would stop by and see how things were going on your blog. All i can say is WHOA DEEP INTENSE.

  • rezi

    mrs can u make program like this, but the led on pin 0-7 and dip switch on pin 8,9,A0,A1,A2,A3,A4,A5

    please help

Leave a Comment