Quick and dirty Christmas light fix with Arduino

Christmas is closing upon Do It Yourselfus once again, and I was greeted with not so merry news: the LED lights from last year did not work anymore. Being too late the shops are already closed to buy a new one, but you can't leave your family without the famous look of a Christmas tree. Let's see what we can do about this.
After prying the box open I saw 3 cables controlling the LEDs. Cutting them off and connecting them to +2,5V I realized that different segments light up with -2,5V, 0V and +2,5V on each cable. Theoretically this means 27 different states the string can produce (0, - and + on each cable). I immediately started to think on an easy oscillating circuit what I can make from the salvaged parts at home when I suddenly realized... I have an Arduino board laying around!
 
Ports 9, 10 and 11 are PWM outputs, makes it perfect for controlling LEDs. Each cable had a resistor built in so I didn't have to worry about burning them out. I had like 5 minutes to code the software, this is what I could come up with:

byte v9 = 128;
byte v10 = 128;
byte v11 = 128;
byte rndport = 0;
byte rndval = 0;
bool changed;

void setup() {
  Serial.begin(9600);
//  while (!Serial) {}
  pinMode(9,OUTPUT);
  pinMode(10,OUTPUT);
  pinMode(11,OUTPUT);
  randomSeed(analogRead(0));
}

byte rnd() {
  int rndnum = random(1000);
  if (rndnum < 333) { return 0; }
    else
  if (rndnum < 666) { return 1; }
    else { return 2; }
}
void loop() {
  analogWrite(9,v9);
  analogWrite(10,v10);
  analogWrite(11,v11);
  Serial.print("Port  9: ");
  Serial.println(v9);
  Serial.print("Port 10: ");
  Serial.println(v10);
  Serial.print("Port 11: ");
  Serial.println(v11);
  Serial.println();
  changed = false;
  while (!changed) {
    rndport = rnd();
    rndval = (rnd() * 128) - 1;
    if (rndport == 0) { changed = v9 != rndval; }
      else
    if (rndport == 1) { changed = v10 != rndval; }
      else
    if (rndport == 2) { changed = v11 != rndval; }
  }
  Serial.print("rndport = ");
  Serial.print(rndport);
  Serial.print(", rndval = ");
  Serial.println(rndval);
  if (rndport == 0) { v9 = rndval; }
    else
  if (rndport == 1) { v10 = rndval; }
    else
  if (rndport == 2) { v11 = rndval; }
  delay(2000); 
}

 
The result is a random segment changing state. And a happy family evening.
 
Christmas tree: hacked.
 

Add a comment

HTML code is displayed as text and web addresses are automatically converted.

This post's comments feed