Claus-o-meter

Ho Ho Ho! The Christmas Spirit is only one potentiometer away with our penultimate hack! Let’s display it with 23 LEDs.

clausometer_hero

You’ll need:

  • 23 mixed LEDs
  • 3 shift registers
  • 1 potentiometer
  • Arduino Uno
  • Breadboard + wires
  • Cardboard
  • Paint

1) LEDs and shift registers
I think you’re becoming familiar with shift registers now; we’ve been using them a lot! (And the more LEDs, the more useful they get).
Like for the Chimney Climber game on Saturday, we are going to use 3 shift registers, and only 3 pins on the Arduino. (I hope you’ve got a big breadboard though!)

sleigh0

To start with, we’ll only make the lights switch on one after the other, that’s what the Arduino sketch below lets us do:

//Pin connected to Pin 12 of 74HC595 (Latch)
int latchPin = 8;
//Pin connected to Pin 11 of 74HC595 (Clock)
int clockPin = 12;
//Pin connected to Pin 14 of 74HC595 (Data)
int dataPin = 11;

int sequence[] =   {1,3,7,15,31,63,127,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255};
int sequence2[] = {0,0,0,0,0,0,0,0,1,3,7,15,31,63,127,255,255,255,255,255,255,255,255,255};
int sequence3[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,3,7,15,31,63,127,255,255,255};

void setup() 
{
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
}

void loop() 
{
  for (int i = 0; i < 24; i++) 
  {
    //set latchPin low to allow data flow
    digitalWrite(latchPin, LOW);
    shiftOut(sequence[i]);
    shiftOut(sequence2[i]);
    shiftOut(sequence3[i]);

    //set latchPin to high to lock and send data
    digitalWrite(latchPin, HIGH);

    if(i==23){
      delay(500);// we leave the last light on a bit longer
    } else {
      
      delay(200); //sets the speed, shorter means faster
    }
  }
}

void shiftOut(byte dataOut) 
{
  boolean pinState;
  digitalWrite(dataPin, LOW);
  digitalWrite(clockPin, LOW);
  for (int i=0; i<=7; i++) 
  {
    digitalWrite(clockPin, LOW);
    if ( dataOut & (1<<i) ) 
      {
        pinState = HIGH;
      }
    else 
      {
        pinState = LOW;
      }
  digitalWrite(dataPin, pinState);
  digitalWrite(clockPin, HIGH);
  }

digitalWrite(clockPin, LOW);
}

And what it looks like:

2) Santa’s Sleigh
Now, all we want to do it to map the lights above with a potentiometer, to be able to control them; and of course shape Santa’s sleigh.
sleigh1

(I probably should have told you by now that the Claus-o-meter comes from the film Elf, and people spreading the Christmas Spirit is what enables it to fly).
sleigh2

After wiring the potentiometer to pin A0, just amend the loop() function like so:

  int sensorReading = analogRead(potPin);
  // map the result to a range from 0 to the number of LEDs:
  int ledLevel = map(sensorReading, 0, 389, 0, 24);

  //print out sensorReading to know the maximum value you're getting, to map out the lights and potentiometer properly
  //Serial.println(sensorReading); 
  
  digitalWrite(latchPin, LOW);
  
  shiftOut(sequence[ledLevel]);
  shiftOut(sequence2[ledLevel]);
  shiftOut(sequence3[ledLevel]);
  
  digitalWrite(latchPin, HIGH);
  
  delay(10);

All you’ve got to do now is turn the potentiometer up to the level you think your Christmas Spirit is (you can sing along too, it might help 😉 )

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s