Having read all the documentation I could find online related to interrupts, I'm left a bit confused, and unable to solve this problem - please could you guys point me in the right direction?
My product is a bar of RGB LEDs, controlled from 3 PWM pins, one for each colour. I plan to program sequences of colour-changes into the bar, and have a button to change the colour sequence of the bar when the user wants.
If my main loop function polls the state of my switch, and finds it is turned ON, it will execute "programNumber++" . The program then scans down a large SWITCH structure, finds the right colour sequence to play from the value of "programNumber" and plays it. Simple!
BUT when the colour sequences get to larger lengths (say, 30 seconds long, for example) when the user presses the switch, the program might not be polling the switch state, and won't execute "programNumber++" , to change to the next program.
SO I try interrupts. Now, when the button is pressed, the interrupt pin's state changes (rises from high to low, in this case) and executes my interrupt service routine (I think that's the right terminology?). I've called my ISR function "buttonPressed" and it executes "programNumber++". Say this interrupt occurs in the middle of a 30 second colour sequence, the interrupt is called, increments the programNumber counter, and returns to the colour sequence code again! D'oh!
My friend who programs microchip PICs says I need to make my code return to the top of my loop() function after the interrupt has been called. Is this possible? Is there another way of doing this? My abbreviated code is below:
// declare variables
// PWM output pins 9,10,11
// input pins
// other variables
void setup()
{
pinMode(switchPin, INPUT); // input switch on pin 2
digitalWrite(switchPin, HIGH); // Set internal pullup
attachInterrupt(0, buttonPressed, FALLING); // call function buttonPressed, when interrupt switch pin is activated
}
void loop()
{
switch (programNumber)
{
case 0:
// initial state, turn LEDs off
solid(0, 0, 0, 1); // solid(rValue,gValue,bValue,time) function PWM's the RGB LED a solid colour specifed by RGB values, for specified amount of time.
break;
case 1:
// white
solid(255, 255, 255, 1);
break;
case 2:
// long (30 second) colour sequence
longColourSequence(); // function contains sequence of many calls to solid() function defining different colour sequences
break;
// add more cases as more programs are devised
}
}
// interrupt service routine
void buttonPressed()
{
debouncer.update();
val = debouncer.read();
if( val == HIGH)
{
program++;
}
if(program > 2)
{
program = 0;
}
}