Hello,
I'm pretty new to Arduino but have programmed PIC's before. Anyway I'm really enjoying the Arduino resources and ease of getting up and running. I'm starting with what I think should be a pretty simple project to have an LED RGB display fade through the colours, but with a twist. I have 3 buttons. One to pause the colour cycling, one to change the intensity of the display, and at the moment one to reset the board when things go out of whack. It kind of works. I'm sure there are some bugs in there. Anyway, it's pretty fun to play with the buttons and watch the lights. I've borrowed code from people on here but by now is pretty heavily modified so I don't feel to bad about the fact I can't find links to where the code came from anymore. Anyway, here's some code, if it helps anyone that's great, if it is full of errors (which I'm sure there are some) sorry about that and feel free to comment / correct what you think is wrong with it. The code is I believe set up for common Anode LEDs.
//Good working mixer with interupt on pin 2
//set up for common Anode LED
//Button is setup for pullup type resistor(pin connected to +5v through resistor)
#define GREEN 10
#define BLUE 9
#define RED 11
#define delayTime 20
#define FADEBUTTON 2
#define DIMMERBUTTON 3
long debouncing_time = 15; //Debouncing Time in Milliseconds
volatile unsigned long last_micros;
void setup() {
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
pinMode(RED, OUTPUT);
digitalWrite(GREEN, LOW);
digitalWrite(BLUE, LOW);
digitalWrite(RED, LOW);
pinMode(FADEBUTTON, INPUT_PULLUP);
pinMode(DIMMERBUTTON, INPUT_PULLUP);
attachInterrupt(0, debounceInterrupt0, FALLING);
attachInterrupt(1, debounceInterrupt1, FALLING);
}
int redVal;
int blueVal;
int greenVal;
bool lightson = HIGH;
volatile bool fading = LOW;
volatile int dim = 0;
void loop() {
while(fading){
if(fading){
int redVal = (255 - dim);
int blueVal = 0;
int greenVal = 0;
for( int i = 0 ; i < 255 ; i += 1 ){
greenVal += 1;
redVal -= 1;
analogWrite( GREEN, (255 - greenVal) - dim );
analogWrite( RED, (255 - redVal) - dim );
delay( delayTime );
}
}
else{
break;
}
if(fading){
redVal = 0;
blueVal = 0;
greenVal = (255 - dim);
for( int i = 0 ; i < 255 ; i += 1 ){
blueVal += 1;
greenVal -= 1;
analogWrite( BLUE, (255 - blueVal) - dim );
analogWrite( GREEN, (255 - greenVal) - dim );
delay( delayTime );
}
}
else{
break;
}
if(fading){
redVal = 0;
blueVal = (255 - dim);
greenVal = 0;
for( int i = 0 ; i < 255 ; i += 1 ){
redVal += 1;
blueVal -= 1;
analogWrite( RED, (255 - redVal) - dim );
analogWrite( BLUE, (255 - blueVal) - dim );
delay( delayTime );
}
}
else{
break;
}
if (micros()<last_micros) last_micros = micros();
}
}
void debounceInterrupt0() {
if((long)(micros() - last_micros) >= debouncing_time * 1000) {
fading = !fading;
last_micros = micros();
}
}
void debounceInterrupt1() {
if((long)(micros() - last_micros) >= debouncing_time * 1000) {
if(dim<200){
dim = dim + 50;
}
else{
dim = 0;
}
if(!fading){
analogWrite( RED, (255 - redVal) - dim );
analogWrite( GREEN, (255 - greenVal) - dim );
analogWrite( BLUE, (255 - blueVal) - dim );
}
}
}