Light switch box
Explorations Dan Chen / January 14, 2015
This is a very simple LED controller that cycles through different lighting mode using attend 44.
https://www.youtube.com/watch?v=Eyu3wykHSuE&feature=youtu.be
Schematics for Attiny44 light controller
Note the usb port only supplies the power.
Code for Attiny 44
The button push will cycle through different modes of blinking, fading and brightness.
long randNumber;
long randNumberII;
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
const int buttonPin =8 ; // the number of the pushbutton pin
const int ledPin = 7; // the number of the LED pin
int mode=1;
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
//pinMode(buttonPin, INPUT);
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, HIGH); // turn on pullup resistors
}
void loop(){
buttonState = digitalRead(buttonPin);
if (mode==1){
analogWrite(ledPin, 62);
if (buttonState == LOW) { mode=2; delay (90); buttonState=HIGH;}
}
else if (mode==2){
analogWrite(ledPin, 125);
if (buttonState == LOW) { mode=3; delay (90); buttonState=HIGH;}
}
else if (mode==3){
analogWrite(ledPin, 255);
if (buttonState == LOW) { mode=4; delay (90); buttonState=HIGH;}
}
else if (mode==4){
for(int pos = 255; pos>=60; pos-=1) // goes from 180 degrees to 0 degrees
{
buttonState = digitalRead(buttonPin);
analogWrite(ledPin, pos);
if (buttonState == LOW) { mode=5; }
delay(5);
}
for(int pos = 60; pos <= 255; pos += 1) // goes from 0 degrees to 180 degrees { buttonState = digitalRead(buttonPin); if (buttonState == LOW) { mode=5; } analogWrite(ledPin, pos); delay(5); } if (buttonState == LOW) { mode=5; delay (90); buttonState=HIGH;} } else if (mode==5){ for(int pos = 255; pos>=60; pos-=5) // goes from 180 degrees to 0 degrees
{
buttonState = digitalRead(buttonPin);
analogWrite(ledPin, pos);
if (buttonState == LOW) { mode=6; }
delay(5);
}
for(int pos = 60; pos <= 255; pos += 5) // goes from 0 degrees to 180 degrees { buttonState = digitalRead(buttonPin); if (buttonState == LOW) { mode=6; } analogWrite(ledPin, pos); delay(5); } if (buttonState == LOW) { mode=6; delay (90); buttonState=HIGH;} } else if (mode==6){ randomglow(); } else if (mode==7){ if (buttonState == LOW) { mode=8; delay (90); buttonState=HIGH;} analogWrite(ledPin, 0); delay (50); analogWrite(ledPin, 255); delay (50); } else if (mode==8){ if (buttonState == LOW) { mode=1; delay (90); buttonState=HIGH;} analogWrite(ledPin, 0); } } void randomglow(){ randNumber = random(1, 5); randNumberII = random(100, 255); for(int pos = 255; pos>=randNumberII; pos-=randNumber) // goes from 180 degrees to 0 degrees
{
buttonState = digitalRead(buttonPin);
analogWrite(ledPin, pos);
if (buttonState == LOW) { mode=7;}
delay(5);
}
for(int pos = randNumberII; pos <= 255; pos += randNumber) // goes from 0 degrees to 180 degrees
{
buttonState = digitalRead(buttonPin);
analogWrite(ledPin, pos);
if (buttonState == LOW) { mode=7; }
delay(5);
}
if (buttonState == LOW) { mode=7; delay (90); buttonState=HIGH;}
}