/* *This code is for an instructable at entitled "Mood-Cube (Li-ion Mood Light)". *Code written by: Adam Berger * *The following code is presented "as-is". You may use this code in any project *, but I would appreciate a shout-out if you use it in published work. * *Good-luck! */ #define rainbowState 0 #define RGBFadeState 1 #define randState 2 #define colorChooserState 3 const int redPin=0, greenPin=1, bluePin=2, pot=3, but=4; int rand1, rand2, rand3, randomNum, state=0, time; unsigned long previousMillis=millis(), pMillis=millis(); boolean flag; //color chooser variables: int colorState = 0, redVal=0, greenVal=0, blueVal=0; void setup() { pinMode(redPin, OUTPUT); pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT); pinMode(pot, INPUT); pinMode(but ,INPUT); digitalWrite(but, HIGH);//sets the internal pull-up resistor } void loop() { //change change in state if(digitalRead(but)==LOW&&millis()-previousMillis>500){ state++; previousMillis=millis(); pMillis=millis(); } //check max. state if(state>3) state=0; //states: switch(state){ case rainbowState: rainbowFade(); break; case RGBFadeState: RGBFade(); break; case randState: randFlash(); break; case colorChooserState: colorChooser(); break; } } //flashes a random color void randFlash(){ if(flag){//this is just so the first time randFlash runs, it produces a color rand1 = rand()%255; rand2 = rand()%255; rand3 = rand()%255; } if((millis()-pMillis500){ colorState++; previousMillis=millis(); pMillis=millis(); } if(colorState>2){ colorState=0, state=0, redVal=0, greenVal = 0, blueVal=0; return; } switch(colorState){ case 0: redVal = map(analogRead(pot),0,1023,0,250); break; case 1: greenVal = map(analogRead(pot),0,1023,0,250); break; case 2: blueVal = map(analogRead(pot),0,1023,0,250); break; } softPWM(redPin, redVal, 20); softPWM(greenPin, greenVal, 20); softPWM(bluePin, blueVal, 20); } } //software PWM function that fakes analog output void softPWM(int pin, int freq, int sp) { //if(freq>5){ You may need to uncomment and add this if you get unusual behavior from your LED digitalWrite(pin,HIGH); //on delayMicroseconds(sp*freq); digitalWrite(pin,LOW); //off delayMicroseconds(sp*(255-freq)); } /* *THE FOLLOWING CODE WAS WRITTEN BY: "tecnocubeguy" *from his comment on another moodlight instructable *For more cool projects, visit his instructable page at: http://www.instructables.com/member/tecnocubeguy/ *Please give him some views, favorites, and respect! */ void rainbowFade(){ if(state==rainbowState) fadeUp(greenPin, redPin, bluePin); //red to yellow if(state==rainbowState) fadeDown(redPin, greenPin, bluePin); //yellow to green if(state==rainbowState) fadeUp(bluePin, greenPin, redPin); //green to cyan if(state==rainbowState) fadeDown(greenPin, bluePin, redPin); //cyan to blue if(state==rainbowState) fadeUp(redPin, bluePin, greenPin); //blue to purple if(state==rainbowState) fadeDown(bluePin, redPin, greenPin); //purple to red } void fadeUp(int fadePin, int onPin, int offPin) { //set constant colors digitalWrite(onPin, HIGH); digitalWrite(offPin, LOW); //set current brightness out of 1000 for(int bright = 1; bright < 1000; bright+=10) { //set PWM lengths int on = bright; int off = 1000 - bright; //software PWM for 'time' ms time = map(analogRead(pot),0,1023,1,100); for(int run = 0; run < time; run++) { if (digitalRead(but)==LOW&&millis()-previousMillis>500) {state++; previousMillis=millis(); return;} digitalWrite(fadePin, HIGH); delayMicroseconds(on); digitalWrite(fadePin, LOW); delayMicroseconds(off); } } } void fadeDown(int fadePin, int onPin, int offPin) { //set constant colors digitalWrite(onPin, HIGH); digitalWrite(offPin, LOW); //set current brightness out of 1000 for(int bright = 1; bright < 1000; bright+=10) { //set inverse PWM lengths int on = 1000 - bright; int off = bright; //software PWM for 'time' ms time = map(analogRead(pot),0,1023,1,100); for(int run = 0; run < time; run++) { if (digitalRead(but)==LOW&&millis()-previousMillis>500) {state++; previousMillis=millis(); return;} digitalWrite(fadePin, HIGH); delayMicroseconds(on); digitalWrite(fadePin, LOW); delayMicroseconds(off); } } }