Using attachInterrupt to halt a function using a switch

I have made a simple LED array contained inside a SparkFun box. I have also created multiple functions that light each LED up in different patterns. All LEDs are connected to individual digital pins on my SparkFun Redboard (basically an Arduino Uno). I have also added a toggle switch to the top of the box for power. Currently, my code uses an if statement in the loop to determine whether the switch is turned to on or off. When it is turned to on, each pattern function is called three times, which takes about a minute or so to go through every pattern. What I can’t seem to figure out is how to code the switch so that when I turn it to the off position in the middle of a pattern, all of the lights turn off. Right now, if I toggle the switch to off in the middle of the pattern sequence, the program simply waits for the last pattern to complete and then turns off. Of course, this isn’t like a real power switch. I have been trying to use the attachInterrupt function, but have had no success. PLEASE HELP! :’( :’( :’(

The attached code has my failed attachInterrupt code commented out. The uncommented code runs properly besides the delayed ‘turn off’ from the switch.

LED_box.ino (6.34 KB)

That's not a job for an interrupt. The interrupt would just return and finish the sequence. What you have to do is write the sequences so they're non-blocking and loop can still read the switch. Don't go to a function and try to do the whole sequence at once. Let the function check to see if it is time for the next step, take one step if so, and return. Rely on the fact that loop is repeating to call your function over and over again so the sequence runs.

Pertaining to Delta_G's advice, look at the "Blink without delay" and "How to do several things at the same time" links:
forum.arduino.cc/index.php?topic=384198.0
best,
Michael

Edit: BTW, some of the links on that page will lead you to Nick Gammon's site. It's worth spending some time there.
--M

int REDpins[] = {2, 3, 4};
int GREENpins[] = {5, 6, 7};
int BLUEpins[] = {8, 9, 10};
int YELLOWpins[] = {11, 12, 13};
int switchPin = 0;
//volatile boolean power = false;

void setup() {
  // put your setup code here, to run once:
  int index;

  for (index = 0; index <= 2; index++) {
    pinMode(REDpins[index], OUTPUT);
    pinMode(GREENpins[index], OUTPUT);
    pinMode(BLUEpins[index], OUTPUT);
    pinMode(YELLOWpins[index], OUTPUT);
  }

  pinMode(switchPin, INPUT);
  //attachInterrupt(switchPin, powercontrol, CHANGE);

}

void loop() {
  // put your main code here, to run repeatedly:
  
  if (digitalRead(switchPin) == LOW) {
    wave();
    wave();
    steppingstone();
    steppingstone();
    colors();
    colors();
  }

  else if (digitalRead(switchPin) == HIGH) {
    delay(5);
  }

//  if (power == true) {
//    wave();
//    wave();
//    steppingstone();
//    steppingstone();
//    colors();
//    colors();
//  }
//
//  else if (power == false) {
//    delayMicroseconds(5);
//  }

}

//void powercontrol() {
//  if (digitalRead(switchPin) == LOW) {
//    power = true;
//  }
//
//  else if (digitalRead(switchPin) == HIGH) {
//    power = false;
//  }
//}

void wave() {
  int index;
  int delayTime = 100;

  //turn RED pins on
  for (index = 0; index <= 2; index++) {
    digitalWrite(REDpins[index], HIGH);
    delay(delayTime);
  }

  //turn GREEN pins on
  for (index = 0; index <= 2; index++) {
    digitalWrite(GREENpins[index], HIGH);
    delay(delayTime);
  }

  //turn BLUE pins on
  for (index = 0; index <= 2; index++) {
    digitalWrite(BLUEpins[index], HIGH);
    delay(delayTime);
  }

  //turn YELLOW pins on
  for (index = 0; index <= 2; index++) {
    digitalWrite(YELLOWpins[index], HIGH);
    delay(delayTime);
  }

  //turn RED pins off
  for (index = 0; index <= 2; index++) {
    digitalWrite(REDpins[index], LOW);
    delay(delayTime);
  }

  //turn GREEN pins off
  for (index = 0; index <= 2; index++) {
    digitalWrite(GREENpins[index], LOW);
    delay(delayTime);
  }

  //turn BLUE pins off
  for (index = 0; index <= 2; index++) {
    digitalWrite(BLUEpins[index], LOW);
    delay(delayTime);
  }

  //turn YELLOW pins off
  for (index = 0; index <= 2; index++) {
    digitalWrite(YELLOWpins[index], LOW);
    delay(delayTime);
  }

  //turn YELLOW pins on
  for (index = 2; index >= 0; index--) {
    digitalWrite(YELLOWpins[index], HIGH);
    delay(delayTime);
  }

  //turn BLUE pins on
  for (index = 2; index >= 0; index--) {
    digitalWrite(BLUEpins[index], HIGH);
    delay(delayTime);
  }

  //turn GREEN pins on
  for (index = 2; index >= 0; index--) {
    digitalWrite(GREENpins[index], HIGH);
    delay(delayTime);
  }

  //turn RED pins on
  for (index = 2; index >= 0; index--) {
    digitalWrite(REDpins[index], HIGH);
    delay(delayTime);
  }

  //turn YELLOW pins off
  for (index = 2; index >= 0; index--) {
    digitalWrite(YELLOWpins[index], LOW);
    delay(delayTime);
  }

  //turn BLUE pins off
  for (index = 2; index >= 0; index--) {
    digitalWrite(BLUEpins[index], LOW);
    delay(delayTime);
  }

  //turn GREEN pins off
  for (index = 2; index >= 0; index--) {
    digitalWrite(GREENpins[index], LOW);
    delay(delayTime);
  }

  //turn RED pins off
  for (index = 2; index >= 0; index--) {
    digitalWrite(REDpins[index], LOW);
    delay(delayTime);
  }
}

void steppingstone() {
  int index;
  int delayTime(100);

  //turn RED pins on then off
  for (index = 0; index <= 2; index++) {
    digitalWrite(REDpins[index], HIGH);
    delay(delayTime);
    digitalWrite(REDpins[index], LOW);
  }

  //turn GREEN pins on then off
  for (index = 0; index <= 2; index++) {
    digitalWrite(GREENpins[index], HIGH);
    delay(delayTime);
    digitalWrite(GREENpins[index], LOW);
  }

  //turn BLUE pins on then off
  for (index = 0; index <= 2; index++) {
    digitalWrite(BLUEpins[index], HIGH);
    delay(delayTime);
    digitalWrite(BLUEpins[index], LOW);
  }

  //turn YELLOW pins on then off
  for (index = 0; index <= 2; index++) {
    digitalWrite(YELLOWpins[index], HIGH);
    delay(delayTime);
    digitalWrite(YELLOWpins[index], LOW);
  }

  //turn YELLOW pins on then off
  for (index = 2; index >= 0; index--) {
    digitalWrite(YELLOWpins[index], HIGH);
    delay(delayTime);
    digitalWrite(YELLOWpins[index], LOW);
  }

  //turn BLUE pins on then off
  for (index = 2; index >= 0; index--) {
    digitalWrite(BLUEpins[index], HIGH);
    delay(delayTime);
    digitalWrite(BLUEpins[index], LOW);
  }

  //turn GREEN pins on then off
  for (index = 2; index >= 0; index--) {
    digitalWrite(GREENpins[index], HIGH);
    delay(delayTime);
    digitalWrite(GREENpins[index], LOW);
  }

  //turn RED pins on then off
  for (index = 2; index >= 0; index--) {
    digitalWrite(REDpins[index], HIGH);
    delay(delayTime);
    digitalWrite(REDpins[index], LOW);
  }
}

void colors() {
  int index;
  int delayTime(500);

  for (index = 0; index <= 2; index++) {
    digitalWrite(REDpins[index], HIGH);
  }

  delay(delayTime / 2);

  for (index = 0; index <= 2; index++) {
    digitalWrite(REDpins[index], LOW);
  }

  for (index = 0; index <= 2; index++) {
    digitalWrite(GREENpins[index], HIGH);
  }

  delay(delayTime);

  for (index = 0; index <= 2; index++) {
    digitalWrite(GREENpins[index], LOW);
  }

  for (index = 0; index <= 2; index++) {
    digitalWrite(BLUEpins[index], HIGH);
  }

  delay(delayTime);

  for (index = 0; index <= 2; index++) {
    digitalWrite(BLUEpins[index], LOW);
  }

  for (index = 0; index <= 2; index++) {
    digitalWrite(YELLOWpins[index], HIGH);
  }

  delay(delayTime / 2);

  for (index = 0; index <= 2; index++) {
    digitalWrite(YELLOWpins[index], LOW);
  }

  for (index = 0; index <= 2; index++) {
    digitalWrite(YELLOWpins[index], HIGH);
  }

  delay(delayTime / 2);

  for (index = 0; index <= 2; index++) {
    digitalWrite(YELLOWpins[index], LOW);
  }

  for (index = 0; index <= 2; index++) {
    digitalWrite(BLUEpins[index], HIGH);
  }

  delay(delayTime);

  for (index = 0; index <= 2; index++) {
    digitalWrite(BLUEpins[index], LOW);
  }

  for (index = 0; index <= 2; index++) {
    digitalWrite(GREENpins[index], HIGH);
  }

  delay(delayTime);

  for (index = 0; index <= 2; index++) {
    digitalWrite(GREENpins[index], LOW);

  }

  for (index = 0; index <= 2; index++) {
    digitalWrite(REDpins[index], HIGH);

  }

  delay(delayTime);

  for (index = 0; index <= 2; index++) {
    digitalWrite(REDpins[index], LOW);
  }


}

Hi,
Maybe this will help you.
If you want to use an interrupt In Arduino UNO you most use the pins below.

Board Digital Pins Usable For Interrupts
Uno, Nano, Mini, other 328-based 2, 3

Better still would be to show how not to use an interrupt where an interrupt is not appropriate,

Delta_G, I am confused how I make the pattern functions non-blocking. After each LED change, is that when I check the status of the button?

Absolutely right.

rzouk:
Delta_G, I am confused how I make the pattern functions non-blocking. After each LED change, is that when I check the status of the button?

Yes. But that does NOT mean adding dozens of pieces of code into your program to check the button status.

Your code should be designed so it just moves one step through the sequence everytime loop() repeats. Look at how the servo operates in Several Things at a Time

…R