A Tiny Light

Do you have an ATTiny85 and a shiftbright V2 sitting around doing nothing?!  Well you might not but I do.

Well I’ve got a solution to fix that! We can combine the 2 to give you a compact and relatively powerful flashlight! Now why would I want to do this other than because I need to burn parts? Well the shiftbright V2 is an interesting little thing. it’s essentially a 3 colour LED with a controller on it to set power levels and color. So you can’t just connect it to a battery and expect it to power up. It requires instruction! That is where the ATTiny85 comes in, we can use it to blast in some bits to set the LED.

You can find most of the code I used here http://docs.macetech.com/doku.php/shiftbrite to make it . (I’ll post my modified code below) . Also FYI I am coding the ATTiny using the arduino IDE.

The ATTiny85 as I mentioned before has 5 I/O pins (6 if you disable the reset pin) so in this project we’re going to use 4 of them for the shift bright and 1 for a button to toggle between pre-programmed light modes. (RED/WHITE/GREEN/BLUE/STROBE in this case).

The ATTiny85 will be determining how to change the status of the LEDS the way I did it was to time a button hold, to make sure the mode change was deliberate. what I wanted was that if you held the button down for a second but less than 2 seconds it will toggle between red/ white / green / blue, starting at red because that won’t kill your night vision too bad at night. Then if the button is held for more than 2 seconds it goes into a crazy strobe mode, good for when you want to party? I don’t know. To get out of the strobe mode Hold the button for 1-2 seconds and it defaults back to red.

Here’s a small video of it in action!

You can see the schematic below, it’s a pretty simple set up by the way the little 3 pin thing at the bottom is a 5V 1A regulator that I just realized the label is too small on.

Image

And here is the code, again most of it is just a modified version of the shiftbright code in the above link.

int datapin = 3; // DI
int latchpin = 2; // LI
int enablepin = 1; // EI
int clockpin = 0; // CI
unsigned long SB_CommandPacket;
int SB_CommandMode;
int SB_BlueCommand;
int SB_RedCommand;
int SB_GreenCommand;
long previousMillis = 0;
long previousMillisStrobe = 0;
int buttonMode =0;
int interval = 25;
int Button1 =0;
int Button2 =0;
int buttonHold =0;
int flashmode=0;
int countup=0;

// SHIFTBRIGHT code BY http://macetech.com/blog/node/54
void setup() {
pinMode(datapin, OUTPUT);
pinMode(latchpin, OUTPUT);
pinMode(enablepin, OUTPUT);
pinMode(clockpin, OUTPUT);
pinMode(4, INPUT);

digitalWrite(latchpin, LOW);
digitalWrite(enablepin, LOW);
}

void SB_SendPacket() {
SB_CommandPacket = SB_CommandMode & B11;
SB_CommandPacket = (SB_CommandPacket << 10) | (SB_BlueCommand & 1023);
SB_CommandPacket = (SB_CommandPacket << 10) | (SB_RedCommand & 1023);
SB_CommandPacket = (SB_CommandPacket << 10) | (SB_GreenCommand & 1023);

shiftOut(datapin, clockpin, MSBFIRST, SB_CommandPacket >> 24);
shiftOut(datapin, clockpin, MSBFIRST, SB_CommandPacket >> 16);
shiftOut(datapin, clockpin, MSBFIRST, SB_CommandPacket >> 8);
shiftOut(datapin, clockpin, MSBFIRST, SB_CommandPacket);

delay(1); // adjustment may be necessary depending on chain length
digitalWrite(latchpin,HIGH); // latch data into registers
delay(1); // adjustment may be necessary depending on chain length
digitalWrite(latchpin,LOW);
}
void strobe () { // This is the strobe mode, used to just switch colors of the LEDS at 25ms intervals
unsigned long currentMillis = millis();

if (currentMillis – previousMillisStrobe > 25)
{previousMillisStrobe = currentMillis; countup ++ ; // Count up tells it to switch when it’s incremented

if ( countup ==1 ){
SB_CommandMode = B00; // Write to PWM control registers
SB_RedCommand = 0; // Minimum red
SB_GreenCommand = 0; // Minimum green
SB_BlueCommand = 1023; // Maximum blue
SB_SendPacket();
}
if (countup ==2)
{previousMillisStrobe = currentMillis;
SB_CommandMode = B00; // Write to PWM control registers
SB_RedCommand = 0; // red
SB_GreenCommand = 1023; // green
SB_BlueCommand =0; // blue
SB_SendPacket();
}
if (countup==3)
{previousMillisStrobe = currentMillis;
SB_CommandMode = B00; // Write to PWM control registers
SB_RedCommand = 1023; // red
SB_GreenCommand = 0; // green
SB_BlueCommand =0; // blue
SB_SendPacket();
}

if (countup== 4)
{previousMillisStrobe = currentMillis;
SB_CommandMode = B00; // Write to PWM control registers
SB_RedCommand = 1023; // red
SB_GreenCommand = 1023; // green
SB_BlueCommand =1023; // blue
SB_SendPacket();
}
if (countup> 4){if (countup= 0);}} // the strobe mode quickly switch between all the colors then white

}
void loop() {
unsigned long currentMillis = millis();
Button1 = digitalRead(4);
SB_CommandMode = B01; // Write to current control registers
SB_RedCommand = 127; // Full current
SB_GreenCommand = 127; // Full current
SB_BlueCommand = 127; // Full current
SB_SendPacket();
if(currentMillis-previousMillis > interval) // Poles the button press at the interval
{
previousMillis = currentMillis;
if (Button1 == 1) {buttonHold++;}// if it was pressed increment the Button hold status
if(buttonHold <40 && Button1 == 0) {buttonHold =0;} // if the button was let fo at some point before the 1 second mark reset the button hold status
if (buttonHold > 40 && buttonHold <80 && Button1 ==0){flashmode ++;buttonHold =0; }// If it is held between 1-2s then toggle the LED colors
if (buttonHold > 80) {flashmode = -1;buttonHold =0;} // If it’s more than 2 seconds switch to strobe mode.
if (flashmode ==0)
{
SB_CommandMode = B00; // Write to PWM control registers
SB_RedCommand = 1023; // Maximum red
SB_GreenCommand = 0; // Minimum green
SB_BlueCommand = 0; // Minimum blue
SB_SendPacket();
}

if (flashmode ==1) {
SB_CommandMode = B00; // Write to PWM control registers
SB_RedCommand = 1023; // Minimum red
SB_GreenCommand = 1023; // Maximum green
SB_BlueCommand = 1023; // Minimum blue
SB_SendPacket();

}

if (flashmode == 2) {
SB_CommandMode = B00; // Write to PWM control registers
SB_RedCommand = 0; // Minimum red
SB_GreenCommand = 1023; // Maximum green
SB_BlueCommand = 0; // Minimum blue
SB_SendPacket();

}

if (flashmode == 3) {
SB_CommandMode = B00; // Write to PWM control registers
SB_RedCommand = 0; // Minimum red
SB_GreenCommand = 0; // Minimum green
SB_BlueCommand = 1023; // Maximum blue
SB_SendPacket();

}
if (flashmode > 3)
{ flashmode=0; }

if (flashmode == -1 ) {strobe ();}
}

}

Thanks for reading!  Heres a couple photos of it.

Attinylite Attinylite_1 Attinylite_2 Attinylite_3 Attinylite_4 Attinylite_5

Oh random fun fact, so from a typical 9V alkaline cell with about 560mAh on it, with this if you run it on white, (using all 3 colors) you should get a good 8 hours of light from it.  (60mAh for te LED and 7mAh for the ATTiny)

Advertisements