Ikea Sun Light animated for Babies – SunBabyNight

Well… yes! Having children and the need to furnish their room, I thought, at the suggestion of my wife to add some effects to the lights of the same: it born soon Ikea Sun Light animated for Babies – SunBabyNight. When I bought the Ikea in the shape of the sunlight.

Essentially it is composed of two plastic pieces overlapped to a lamp-holder that carries up three lamps.

I decided to add some animated light effects, leaving the original lamps inside the sun.

I dismounted the plastic pieces. I had a led stipe 1 metre long that carries 5050 led type. I cut 12 segments with 3 led on each of them.

Assembling by glue these segments I reached the entire circumference.

Then I assembled an electronic board with Arduino Nano v3.0 on board. I used 12 output pins of Arduino to connect 12 transistors 2N2222 that are responsible to drive the powering of the led stripes. This is an image of the final board:

I designed the board to be plug-N-play installing fast plug connectors. In this way, in case of maintenance, the kit disassembling is quite fast and simple. The connectors 1, 2 e 3 provide power lines to the 12 segments, the first left botto 4 pins connector provide the ground lines to the led segments and the 4-pins connector on right bottom side is the main power supply and a kind of predisposition for an IRDA module to be used in the future for controlling animation and the whole system … maybe in further steps.

To obtain a scenic and addictive effect I chose to drive the segments by a modulated signal generated by the PWM. In this way I could power on and off fading the segments.

Unfortunately, Arduino Nano v3.0 haven’t this ability. It cannot drive all pins in PWM mode but only a few parts.

So, anyway, to obtain the effect my attention fell down on an external library freely available on the net: SoftPWM.

At the end the final result is what you can see in this video:

Following the simple code used inside the Arduino to obtain the effect:

/************************************************************************
  IKEA BABY SUN LIGHT
  Michele Ardito - www.micheleardito.info
  Progetto del 26.01.2016
*************************************************************************/

/************************************************************************
   LIBRERIA SOFTPWM
*************************************************************************/
#include <SoftPWM.h>
uint8_t leds[12] = {3, 6, 5, 4, 11, 8, 9, 7, A3, 12, A2, 10};

/************************************************************************
   Costanti e variabili di tempo 
*************************************************************************/
#define DELAY 120
#define DELAYSHORT 40
#define DELAYSHORTF 150
#define DELAYLONG 850
#define INITIALFADEOUT 100
// per il tipo di ciclo (scelta della animazione)
int ciclo = 0;
// ripetizioni del ciclo
int ripetizioni = 0;
/************************************************************************
   Variabili varie 
*************************************************************************/
int i, j;

/************************************************************************
   SETUP
*************************************************************************/
void setup() {
  // avvia la libreria
  SoftPWMBegin();
  // accende tutti i segmenti al max PWM
  for (int i = 0; i < 12; i++) {
    SoftPWMSet(leds[i], 255);
  }
  // imposta il tempo di fade in a 50ms e fadeout a 400ms per tutti i segmenti
  SoftPWMSetFadeTime(ALL, 100, 400);
  
  // if analog input pin 0 is unconnected, random analog
  // noise will cause the call to randomSeed() to generate
  // different seed numbers each time the sketch runs.
  // randomSeed() will then shuffle the random function.
  randomSeed(analogRead(0));

  // mantiene tutto acceso per 10sec
  delay(10000);
  // fadeout dei segmenti in maniera "pseudocasuale"
  SoftPWMSet(leds[11], 0);
  delay(INITIALFADEOUT);
  SoftPWMSet(leds[9], 0);
  delay(INITIALFADEOUT);
  SoftPWMSet(leds[4], 0);
  delay(INITIALFADEOUT);
  SoftPWMSet(leds[1], 0);
  delay(INITIALFADEOUT);
  SoftPWMSet(leds[2], 0);
  delay(INITIALFADEOUT);
  SoftPWMSet(leds[8], 0);
  delay(INITIALFADEOUT);
  SoftPWMSet(leds[7], 0);
  delay(INITIALFADEOUT);
  SoftPWMSet(leds[3], 0);
  delay(INITIALFADEOUT);
  SoftPWMSet(leds[0], 0);
  delay(INITIALFADEOUT);
  SoftPWMSet(leds[5], 0);
  delay(INITIALFADEOUT);
  SoftPWMSet(leds[10], 0);
  delay(INITIALFADEOUT);
  SoftPWMSet(leds[6], 0);
  
  // inizializza per il ciclo di fade out
  j = 11;
}

/************************************************************************
   LOOP
*************************************************************************/
void loop() {
  switch (ciclo) {
    case 0: 
      for (i = 0; i < 12; i++) {
        SoftPWMSet(leds[i], ripetizioni);
        j = i;
        delay(DELAYSHORT);
      }
      ripetizioni+=20;
      if (ripetizioni > 255) { ripetizioni = 0; ciclo = 1; }
      break;
    case 1:
      for (i = 0; i < 12; i++) {
        SoftPWMSet(leds[i], 255);
        SoftPWMSet(leds[j], 0);
        j = i;
        delay(DELAY - ripetizioni * 7);
      }
      ripetizioni++;
      if (ripetizioni > 10) { ripetizioni = 0; ciclo = 2; }
      break;
    case 2:
      for (i = 0; i < 12; i++) {
        SoftPWMSet(leds[i], random(10,255));
        delay(DELAYSHORT);
      }
      delay(DELAYLONG);
      for (i = 0; i < 12; i++) {
        SoftPWMSet(leds[i], 0);
        delay(DELAYSHORT);
      }
      delay(DELAYLONG);
      ripetizioni++;
      if (ripetizioni > 3) { ripetizioni = 0; ciclo = 3; }
      break;
    case 3:
      for (i = 0; i < 6; i++) {
        SoftPWMSet(leds[i], 255);
        SoftPWMSet(leds[i+6], 0);
      }
      delay(DELAYLONG - ripetizioni * 150);
      for (i = 0; i < 6; i++) {
        SoftPWMSet(leds[i], 0);
        SoftPWMSet(leds[i+6], 255);
      }
      delay(DELAYLONG - ripetizioni * 150);
      ripetizioni++;
      if (ripetizioni > 3) { ripetizioni = 0; ciclo = 4; }
      break;
    case 4:
      for (i = 0; i < 12; i++) {
        SoftPWMSet(leds[i], random(10,255));
      }
      delay(DELAYLONG - ripetizioni * 80);
      ripetizioni++;
      if (ripetizioni > 10) { ripetizioni = 0; ciclo = 5; }
      break;
    case 5:
      for (i = 0; i < 12; i++) {
        SoftPWMSet(leds[i], random(10,255));
      }
      delay(DELAYSHORTF);
      ripetizioni++;
      if (ripetizioni > 100) { ripetizioni = 0; ciclo = 0; }
      break;
  }
}