Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Arduino code for my Halloween mask. It controls some RGB LEDs and red diffused LEDs. A button is used to change the "modes" of the mask.
// Mood Lamp
float RGB1[3];
float RGB2[3];
float INC[3];
int red, green, blue;
const int redPin = 11; //eyes red
const int greenPin = 10; //eyes green
const int bluePin = 9; //eyes blue
const int fader = 5; //fin leds
const int button = 2;
int state = 0;
void setup(){
randomSeed(analogRead(0));
RGB1[0] = 0;
RGB1[1] = 0;
RGB1[2] = 0;
RGB2[0] = random(256);
RGB2[1] = random(256);
RGB2[2] = random(256);
pinMode(button, INPUT);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(fader, OUTPUT);
}
void loop(){
switch(state){
//state machine
case 0:
//off state
while(!digitalRead(button)){
switch_off();
state = 0;
}
delay(500);
state = 1;
break;
case 1:
while(!digitalRead(button)){
yellow_eyes(250, 250, 210);
fin_fader(30, 40);
state = 1;
}
delay(500);
state = 2;
break;
case 2:
while(!digitalRead(button)){
digitalWrite(fader, HIGH);
mood_mode();
state = 2;
}
delay(500);
digitalWrite(fader, LOW);
state = 0;
}
}
//function used in the state machine
void switch_off(){
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
}
void yellow_eyes(int R, int G, int B){
analogWrite(redPin, R);
analogWrite(greenPin, G);
analogWrite(bluePin, B);
}
void fin_fader(int fade_in, int fade_out){
int i = 0;
while(i<255){
analogWrite(fader, i);
i = i+5;
delay(fade_in);
if(digitalRead(button))
return;
}
i = 255;
while(i>0){
analogWrite(fader, i);
i = i-5;
delay(fade_out);
if(digitalRead(button))
return;
}
digitalWrite(fader, LOW);
delay(500);
}
void mood_mode(){
randomSeed(analogRead(0));
for (int x=0; x<256; x++){
INC[x] = (RGB1[x] - RGB2[x]) / 256;
if(digitalRead(button))
return;
}
for (int x=0; x<256; x++){
red = int(RGB1[0]);
green = int(RGB1[1]);
blue = int(RGB1[2]);
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
delay(10);
RGB1[0] -= INC[0];
RGB1[1] -= INC[1];
RGB1[2] -= INC[2];
if(digitalRead(button))
return;
}
for (int x=0; x<3; x++){
RGB2[x] = random(556)-300;
RGB2[x] = constrain(RGB2[x], 0, 255);
if(digitalRead(button))
return;
delay(200);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment