.:Info
Nick: Japala  Info
Age: 47
Location: Raahe

.:Projects by user

.:Albums

.:
Emergency lights with Teensy and addressable leds

Author: Japala
Used time: 2h
Cost: 40 dollars
Categories: Electronics

The code is based on an Arduino sketch that I found from the Bliptronics   site. I changed the pins to suit the new board and wrote couple of new   routines to flash the leds different ways.         

The Code:

#include <TimerOne.h>

//Example to control 5 RGB LED Modules.
//Bliptronics.com
//Ben Moyes 2009
//Modded by Jani 'Japala' Ponkko, 2011 - http://metku.net/
//
//
// Choose which 2 pins you will use for output.
// Can be any valid output pins.
int clockPin = 1;
int dataPin = 2;

byte  SendMode=0;   // Used in interrupt 0=start,1=header,2=data,3=data done
byte  BitCount=0;   // Used in interrupt
byte  LedIndex=0;   // Used in interrupt - Which LED we are sending.
byte  BlankCounter=0;  //Used in interrupt.
unsigned int BitMask;   //Used in interrupt.

//Holds the 15 bit RGB values for each LED.
//You'll need one for each LED, we're using 10 LEDs here.
//Note you've only got limited memory on the Arduino, so you can only control
//Several hundred LEDs on a normal arduino. Double that on a Duemilanove.

unsigned int Display[5];

void setup() {
  byte Counter;

  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);

  // Turn all LEDs off.
  for(Counter=0;Counter < 5; Counter++)
    Display[Counter]=Color(Counter,0,31-Counter);
 
  show();

  Timer1.initialize(5);           // initialize timer1, 25 microseconds refresh rate.
  Timer1.attachInterrupt(LedOut);  // attaches callback() as a timer overflow interrupt

}


//Interrupt routine.
//Frequency was set in setup(). Called once for every bit of data sent
//In your code, set global Sendmode to 0 to re-send the data to the pixels
//Otherwise it will just send clocks.
void LedOut()
{
  switch(SendMode)
  {
    case 3:            //Done..just send clocks with zero data
      digitalWrite(dataPin, 0);
      digitalWrite(clockPin, HIGH);
      digitalWrite(clockPin, LOW);
      break;
    case 2:               //Sending Data
      if (BitCount==0)    //First bit is always 1
        {  digitalWrite(dataPin, 1);
            BitMask=0x8000;//Init bit mask
        }
      else if(BitMask & Display[LedIndex])  //If not the first bit then output the next bits (Starting with MSB bit 15 down.)
        digitalWrite(dataPin, 1);
      else
        digitalWrite(dataPin, 0);
    
      BitMask>>=1;
      BitCount++;
    
      if(BitCount == 16)    //Last bit?
      {
        LedIndex++;        //Move to next LED
        if (LedIndex < 5) //Still more leds to go or are we done?
        {
          BitCount=0;      //Start from the fist bit of the next LED           
        }
        else
          SendMode=3;  //No more LEDs to go, we are done!
      }
      // Clock out data.
      digitalWrite(clockPin, HIGH);
      digitalWrite(clockPin, LOW);
      break;    
    case 1:            //Header
        if (BitCount < 32)            
        {
        digitalWrite(dataPin, 0);
        BitCount++;
        if(BitCount==32)
          {
            SendMode++;      //If this was the last bit of header then move on to data.
            LedIndex=0;
            BitCount=0;
          }
        }
      digitalWrite(clockPin, HIGH);
      digitalWrite(clockPin, LOW);

      break;
    case 0:            //Start
      if(!BlankCounter)    //AS SOON AS CURRENT pwm IS DONE. BlankCounter
      {
        BitCount=0;
        LedIndex=0;
        SendMode=1;
      }
      digitalWrite(clockPin, HIGH);
      digitalWrite(clockPin, LOW);

      break; 
  }
  //Keep track of where the LEDs are at in their pwm cycle.
  BlankCounter++;
}

void show()
{
  // The interrupt routine will see this as re-send LED color data.
  SendMode = 0;
}

// Create a 15 bit color value from R,G,B
unsigned int Color(byte r, byte g, byte b)
{
  //Take the lowest 5 bits of each value and append them end to end
  return( ((unsigned int)g & 0x1F )<<10 | ((unsigned int)b & 0x1F)<<5 | (unsigned int)r & 0x1F);
}


// Show a colour bar going up from 0 to 9
void ColorUp( unsigned int ColourToUse)
{
  byte Counter;
  for(Counter=0;Counter < 5; Counter++)
  {
    Display[Counter]=ColourToUse;
    show();
    delay(5);
  }
}

// Show a colour bar going down from 9 to 0
void ColorDown( unsigned int ColourToUse)
{
  byte Counter;

  for(Counter=5;Counter > 0; Counter--)
  {
    Display[Counter-1]=ColourToUse;
    show();
    delay(5);
  }
}


void loop() {

  unsigned int Counter, Counter2, Counter3;
 

 /*
  //USA style, red and blue alternating
  for(Counter=0;Counter < 2;Counter++)
  {
    ColorUp(Color(31,0,0));
    delay(100);
    ColorDown(Color(0,0,31));
    delay(100);
  }
  */

/*
  // Finnish police, all blue, all leds flash on/off at the same time
   for(Counter=0;Counter < 2;Counter++)
  {
    ColorUp(Color(0,0,0));
    delay(100);
    ColorDown(Color(0,0,31));
    delay(100);
  }
*/

/*
  // Finnish police, all blue, two fast flashes from side to side
  for(Counter=0;Counter < 2;Counter++)
  {
    Display[0]=Color(0,0,31);
    Display[1]=Color(0,0,31);
    Display[2]=Color(0,0,31);
    Display[3]=Color(0,0,0);
    Display[4]=Color(0,0,0);
    show();
    delay(100);
    Display[0]=Color(0,0,0);
    Display[1]=Color(0,0,0);
    Display[2]=Color(0,0,0);
    Display[3]=Color(0,0,0);
    Display[4]=Color(0,0,0);
    show();
    delay(100);
  }
  delay(500);
  for(Counter=0;Counter < 2;Counter++)
  {
     Display[0]=Color(0,0,0);
    Display[1]=Color(0,0,0);
    Display[2]=Color(0,0,31);
    Display[3]=Color(0,0,31);
    Display[4]=Color(0,0,31);
    show();
    delay(100);
    Display[0]=Color(0,0,0);
    Display[1]=Color(0,0,0);
    Display[2]=Color(0,0,0);
    Display[3]=Color(0,0,0);
    Display[4]=Color(0,0,0);
    show();
    delay(100);
  }
  delay(500);
*/

 /*
    //USA style, red and blue alternating
  for(Counter=0;Counter < 2;Counter++)
  {
    ColorUp(Color(31,0,0));
    delay(100);
    ColorDown(Color(0,0,31));
    delay(100);
  }
  */


 // USA style, red and blue alternating from side to side
  for(Counter=0;Counter < 2;Counter++)
  {
    Display[0]=Color(0,0,31);
    Display[1]=Color(0,0,31);
    Display[2]=Color(0,0,31);
    Display[3]=Color(31,0,0);
    Display[4]=Color(31,0,0);
    show();
    delay(100);
    Display[0]=Color(0,0,0);
    Display[1]=Color(0,0,0);
    Display[2]=Color(0,0,0);
    Display[3]=Color(0,0,0);
    Display[4]=Color(0,0,0);
    show();
    delay(100);
  }
  delay(500);
  for(Counter=0;Counter < 2;Counter++)
  {
     Display[0]=Color(31,0,0);
    Display[1]=Color(31,0,0);
    Display[2]=Color(0,0,31);
    Display[3]=Color(0,0,31);
    Display[4]=Color(0,0,31);
    show();
    delay(100);
    Display[0]=Color(0,0,0);
    Display[1]=Color(0,0,0);
    Display[2]=Color(0,0,0);
    Display[3]=Color(0,0,0);
    Display[4]=Color(0,0,0);
    show();
    delay(100);
  }
  delay(500);

 

}                       
         





Remove this ad by registering or by logging in.

Remove this ad by registering or by logging in.




Login with your Facebook account to comment:



11.03.2012 19:52 fknoobbliptronics leds are awesome.
10.10.2011 05:25 YarikoI wanna see how big fine you'll get when you install these to your car and go cruising :D And you need more different modes for how those flash. http://youtu.be/UKR5mrdFG_g
09.10.2011 21:00 JapalaNext we need sound. Hopefully this will be sorted in the next article... :)