Hi
I'm working on my first arduino project and have got a bit stuck. Im starting with a simple rgb light controller, with 3 modes, white, colour swirl and single pot colour setting. I've got it working except I am having to use 3 switches, an input switch, an interrupt switch and the reset switch.
I would like to cut this down to just the one switch that will cycle through the 3 modes but cannot get it to work. below is my code so far, probably a lot of faults with it but I'm learning! any help would be very appreciated!
const int buttonPin = 4; // the pin that the pushbutton is attached to
int ledRedPWMPin = 9; //red RGB // naming and placing the pin that the red LED is attached to
int ledGreenPWMPin = 10; //green RGB // the pin that the green LED is attached to
int ledBluePWMPin = 11; //blue RGB // the pin that the blue LED is attached to
int potPin= 0; // the pin that the 10k pot is attached to
float h;
int h_int;
int red=0, green=0, blue=0;
int FADESPEED=500; // higher number for longer fade in fade mode
int HOLD = 1; // hold fade colour 1 millis can be adjusted higher to have short fades and long holds
int val=0;
int val2=0;
void h2rgb(float h, int &RED, int &GREEN, int &BLUE);
int buttonPushCounter = 1; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setDisplayRGB(int r, int g, int b)
{
analogWrite(ledRedPWMPin, r); //using PWM pins means any shade
analogWrite(ledGreenPWMPin, g); // of red,green,blue
analogWrite(ledBluePWMPin, b); // 2^24 combinations
}
void setup() {
pinMode(buttonPin, INPUT); // initialize the button pin as a input:
pinMode(ledRedPWMPin, OUTPUT); // initialize the LED pin assigned earlier as an output:
pinMode(ledGreenPWMPin, OUTPUT);
pinMode(ledBluePWMPin, OUTPUT);
setDisplayRGB(0,0,0);
attachInterrupt(1, cancel, HIGH);
Serial.begin(9600); // initialize serial communication:
}
void cancel () {
buttonPushCounter=15
;delay,50;
}
void loop() {
buttonState = digitalRead(buttonPin); // read the pushbutton input pin:
delay(50);
if (buttonState != lastButtonState) { // compare the buttonState to its previous state
if (buttonState == HIGH) { // if the state has changed, increment the counter
// if the current state is HIGH then the button
// went from off to on:
buttonPushCounter++; // add 1 to counter
}
else {
}
}
lastButtonState = buttonState; // save the current state as the last state,
//for next time through the loop
if (buttonPushCounter == 0) { // selections
setDisplayRGB(0,0,0); // sets leds to off
Serial.println("OFF"); //Prints Off
}
if (buttonPushCounter == 1) {
setDisplayRGB(255,255,255); // Sets to White if push counter is 1
Serial.println("White");
}
if (buttonPushCounter == 2)
{
Serial.println("Fader");
int rd, gr, bl;
val2 = analogRead(potPin); // reads the value of the potentiometer (value between 0 and 1023)
val2 = map(val2, 0, 1023, 5, 1000);
FADESPEED=(val2);
setDisplayRGB(0,0,255); // Start with Blue
for (rd = 0; rd < 256; rd++) { // fade from blue to violet if red is between 0 and 255 add 1 each time around,end loop at 256 red
analogWrite(ledRedPWMPin, rd); // write above value to the blue LED
val2 = analogRead(potPin); // reads the value of the potentiometer (value between 0 and 1023)
val2 = map(val2, 0, 1023, 5, 1000); // converts to value between 5 and 1000 milliseconds; approx between 9 seconds and 25mins for complete cycle
FADESPEED=(val2); // gradually write in Red LED
delay(FADESPEED); // each step (255) delayed by FADESPEED amount set above Pot read/map setting
}
delay(HOLD); // Hold on Violet if wanted
for (bl = 255; bl > 0; bl--) { // fade from violet to red if blue is between 255 and 0 take off 1 each time around, end loop at 0 blue
analogWrite(ledBluePWMPin, bl); // write above value to the blue LED
val2 = analogRead(potPin); // reads the value of the potentiometer (value between 0 and 1023)
val2 = map(val2, 0, 1023, 5, 1000); // converts to value between 5 and 1000 milliseconds; approx between 9 seconds and 25mins for complete cycle
FADESPEED=(val2); // gradually write out Blue LED
delay(FADESPEED); // each step (255) delayed by FADESPEED amount set above Pot read/map setting
}
delay(HOLD); // Hold on Red if wanted
for (gr = 0; gr < 256; gr++) { // fade from red to yellow if green is between 0 and 255 add 1 each time around, end loop at 256 green
analogWrite(ledGreenPWMPin, gr); // write above value to the green LED
val2 = analogRead(potPin); // reads the value of the potentiometer (value between 0 and 1023)
val2 = map(val2, 0, 1023, 5, 1000); // converts to value between 5 and 1000 milliseconds approx between 9 seconds and 25mins for complete cycle
FADESPEED=(val2); // gradually write in Green LED
delay(FADESPEED); // each step (255) delayed by FADESPEED amount set above Pot read/map setting
}
delay(HOLD); // Hold on Yellow if wanted
for (rd = 255; rd > 0; rd--) { // fade from yellow to green if red is between 355 and 0 take off 1 each time around, end loop at 0 red
analogWrite(ledRedPWMPin, rd); // write above value to the red LED
val2 = analogRead(potPin); // reads the value of the potentiometer (value between 0 and 1023)
val2 = map(val2, 0, 1023, 5, 1000); // converts to value between 5 and 1000 milliseconds; approx between 9 seconds and 25mins for complete cycle
FADESPEED=(val2); // gradually write out Red LED
delay(FADESPEED); // each step (255) delayed by FADESPEED amount set above Pot read/map setting
}
delay(HOLD); // Hold on Green if wanted
for (bl = 0; bl < 256; bl++) { // fade from green to teal if blue is between 0 and 256 add 1 each time around, end loop at 256 blue
analogWrite(ledBluePWMPin, bl); // write above value to the blue LED
val2 = analogRead(potPin); // reads the value of the potentiometer (value between 0 and 1023)
val2 = map(val2, 0, 1023, 5, 1000); // converts to value between 5 and 1000 milliseconds; approx between 9 seconds and 25mins for complete cycle
FADESPEED=(val2); // gradually write in Blue LED
delay(FADESPEED); // each step (255) delayed by FADESPEED amount set above Pot read/map setting
}