Here comes Santa Claus

Final day before Christmas! In the last few days, we’ve helped Santa climb the chimney, fly his sleigh, wrap gifts and find out who’s naughty or nice.
To use our last 24 LEDs, let’s wish him luck for his 24-hour delivery, and track his progress around the world.

santa_hero

You’ll need:

  • 24 red LEDs
  • 1 DC motor
  • Arduino UNO
  • Breadboard+ wires

1) Get your motor running
Let’s start by plugging the DC motor in, I looked up the wiring and code on Adafruit. I’ve prepared a silhouette of Father Christmas on his sleigh to make him spin around [the world] every time it is midnight somewhere on the planet.

santa1

I began using the sketch available in the tutorial mentioned above to determine what the speed of my motor should be, and I then amended it slightly, like so:

int motorPin = 3;
int speed = 160;
 
void setup() 
{ 
  pinMode(motorPin, OUTPUT);
 
 spinSanta();
} 
 
 
void loop() {}

void spinSanta() {
  analogWrite(motorPin, speed);
  delay(1000);
  analogWrite(motorPin, 0);
}

Now Santa is magically twirling and flying at full speed around the world to get his delivery done on time!

2) LED clock
For this second part, we’ll begin by wiring all 24 LEDs with 3 shift registers, connected to 3 pins on the Arduino. I’ve used shift registers a few times this month, so please refer to one of the previous activities, or have a look at the Knightrider example (adding a 3rd shift register, is exactly the same as adding the 2nd).

santa2

I am now going to test the sequence of the LEDs lighting up combined with Santa’s sleigh spinning, second by second. Here are the code and result:

//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 motorPin = 3;
int speed = 160;


int ledOn[] =   {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 ledOn2[] =  {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 ledOn3[] =  {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,3,7,15,31,63,127,255};

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

void loop() 
{
  for (int i = 0; i < 24; i++) 
  {
    digitalWrite(latchPin, LOW);
    shiftOut(ledOn[i]);
    shiftOut(ledOn2[i]);
    shiftOut(ledOn3[i]);
    digitalWrite(latchPin, HIGH);
    delay(1000);
    spinSanta();
  }
}

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);
}

void spinSanta() {
  analogWrite(motorPin, speed);
  delay(1000);
  analogWrite(motorPin, 0);
}

Finally, we want to be able to check the date and time to trigger a new LED and spin whenever it is midnight on the 25th of December somewhere in the world. My computer clock is set on Greenwich Mean Time, so I will need to check the time from December 24th, 12pm to December 25th, 11am.
Just like we did for Countdown to Christmas, we are going to check the date in Processing, and send it over Serial to Arduino.

import processing.serial.*;

Serial port;

int d;
int m;
int h;

int current_h = 0;

void setup() {
  size(500,500);
  port = new Serial(this, Serial.list()[8], 9600); //opening the Serial port
  port.bufferUntil('\n');
}

void draw() {
  d = day(); // built-in Processing function that gets the computer's date
  m = month();
  h = hour();
  
  if(m == 12){
    if(d == 24) {
      if(current_h != h){
        
        switch(h) {
         case 12:
         sendValue(""+0); 
         break;
         
         case 13:
         sendValue(""+1); 
         break;
         
         case 14:
         sendValue(""+2); 
         break;
         
         case 15:
         sendValue(""+3); 
         break;
         
         case 16:
         sendValue(""+4); 
         break;
         
         case 17:
         sendValue(""+5); 
         break;
         
         case 18:
         sendValue(""+6); 
         break;
         
         case 19:
         sendValue(""+7); 
         break;
         
         case 20:
         sendValue(""+8); 
         break;
         
         case 21:
         sendValue(""+9); 
         break;
         
         case 22:
         sendValue(""+10); 
         break;
         
         case 23:
         sendValue(""+11); 
         break;
         
        }
        
        current_h = h;
       }
      
      }else if(d == 25) { 
        if(current_h != h){
          
          switch(h) {
           case 0:
           sendValue(""+12); 
           break;
           
           case 1:
           sendValue(""+13); 
           break;
           
           case 2:
           sendValue(""+14); 
           break;
           
           case 3:
           sendValue(""+15); 
           break;
           
           case 4:
           sendValue(""+16); 
           break;
           
           case 5:
           sendValue(""+17); 
           break;
           
           case 6:
           sendValue(""+18); 
           break;
           
           case 7:
           sendValue(""+19); 
           break;
           
           case 8:
           sendValue(""+20); 
           break;
           
           case 9:
           sendValue(""+21); 
           break;
           
           case 10:
           sendValue(""+22); 
           break;
           
           case 1:
           sendValue(""+23); 
           break;
           
          }
          
          current_h = h;
        } 
    }
  }
}

void sendValue(String value) {
  port.write(value); //send the difference as string
  port.write('\n'); 
}

Finish up by reading the values sent in Arduino, triggering shiftOut parsing the value of the array with the index from the Serial reading as a parameter, and you’re done!

Providing that you leave the Processing sketch running in the background, this nice LED clock will update every time it’s time for Father Christmas to do his deliveries in a new time zone.
And today, you can also track Santa’s location on Google Maps!

That’s all from me! I hope I brought a bit of LED brightness to you this month and that you enjoyed all the activities.
Merry Christmas to y’all!

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