I am working on a project that will control an aquaruim light and I want to use a slide switch on an interrupt to switch between the two main "modes" of the controller. I am currently having a problem of it only switching the first time I switch the flip then being unresponsive until i hit the reset button.
code below
// Create an aquarium light with a controller assembly.
const int redPin = 11;
const int greenPin = 10;
const int bluePin = 9;
const int sw1 = 2;
const int btn1 = 3;
volatile int switch1 = LOW;
int button1 = 0;
int oldbtn = 0;
void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
//pinMode(sw1,INPUT);
pinMode(btn1,INPUT);
attachInterrupt(0,interrupt,CHANGE);
}
void loop() //method for switching between modes
{
switch1 = digitalRead(sw1);
if (switch1 == LOW){
monoColor();
}
else {
dayMode();
}
}
void monoColor() //the single color loop
{
int color = 0; //tells me which color I have active
while (switch1 == LOW){
button1 = digitalRead(btn1); //method for switching colors
if ((button1 == HIGH) && (oldbtn == LOW)){
if (color < 3){
color = color ++;
delay(75);
}
else {
color = 0;
delay(75);
}
}
oldbtn = button1; //this value is old store it
switch (color){
case 0:
digitalWrite (redPin,HIGH);
digitalWrite (greenPin,HIGH);
digitalWrite (bluePin,HIGH);
break;
case 1:
digitalWrite (redPin,LOW);
digitalWrite (greenPin,HIGH);
digitalWrite (bluePin,HIGH);
break;
case 2:
digitalWrite (redPin,LOW);
digitalWrite (greenPin,LOW);
digitalWrite (bluePin,HIGH);
break;
case 3:
digitalWrite (redPin,LOW);
digitalWrite (greenPin,LOW);
analogWrite (bluePin,50);
break;
} //close switch
} //close while loop
} //close monoColor
void dayMode()
{
//while (oldbtn == LOW){
digitalWrite (redPin, HIGH);
digitalWrite (greenPin, LOW);
digitalWrite (bluePin, LOW);
delay (1000000);
//}
}
void interrupt()
{
switch1 = !switch1;
}
please note that the dayMode had not been implemented and just turns the LEDs red as a signal to me)
The button is hooked up with an external pull-down resistor. I am also using a capacitor in parallel for debouncing.