Monday, March 25, 2013

Tricolor LED Project 3: Color mixing and Crossfades

This project just uses Arduino and the tricolor LED to make random mixed colors and crossfades. The LED is wired like in this diagram:  http://arduino.cc/en/uploads/Tutorial/readASCIIString_bb.png
Program code is below:

video



Program code:
int r,g,b;
int LEDs[] = {r,g,b};

int brightness = 3; 
int fadeAmount = 3; 
int pick1,pick2,pick3;
int PICKS[] = {pick1,pick2,pick3};

void setup()  { 
  for(int i = 0;i < 3;i++) {
    LEDs[i] = i+9;
    pinMode(LEDs[i],OUTPUT);
    PICKS[i] = random(3);
  }
  //Serial.begin(9600);
} 

void loop()  { 
  if(brightness == 3) {
    for(int i = 0;i < 3;i++) {
      PICKS[i] = random(3);
    }//end for
  }//endif
  for(int i = 0;i < 3;i++) {
      switch(PICKS[i]) {
      case 0:
        analogWrite(LEDs[i], brightness);
        break;
      case 1:
        analogWrite(LEDs[i], -brightness);
        break;
      case 2:
        analogWrite(LEDs[i], 255);
        break;
      }//end switch
  }//end for
  brightness += fadeAmount;
  if (brightness < 4 || brightness == 255) {
    fadeAmount = -fadeAmount ; 
  }     
  delay(30);                            
}


No comments :