Using Interrupt to stop program with button #1 and resume program with button #2

Hello,

I am currently designing a traffic controller for a two-way intersection. The program will loop through a series of light switches seen in changeLights(). I would like two add an additonal feature that has two push buttons involved as seen in the Hardware photo. When the push button connected to pin #2 is pressed, I would like it to enter the interrupt and turn the Red LED's on only and all other off. I would like it to remain this way until the push button connected to pin #3 is pressed, which then it will return normal operation in changeLights(). I have placed my current code below, I cannot seem to find the correct coding that will allow my program to function properly.. Any advice is appreciated!

volatile int button2 =3;
void setup() {
DDRB = 0b00111111;
attachInterrupt(digitalPinToInterrupt(2),RED, RISING);
}
void loop() {
  changeLights();

}
void changeLights()
{
PORTB=0b00100001;
delay(25000);
PORTB=0b00100010;
delay(5000);
PORTB=0b00001100;
delay(20000);
PORTB=0b00010100;
delay(4000);
PORTB=0b00000000; 
}

void RED()
  {
  PORTB=0b00100100;
  
  }

Hardware.PNG

IF you found the correct code, what would it look like?

Paul

I am still on the search for the code Paul! I will get it posted when I have it working.

As it is, the computer is delaying 99.9999% of the time, so of course it cannot read buttons.

A much better idea is to get rid of all the delays, then the computer will have plenty of time to do useful things.

To learn how, start with this tutorial https://www.baldengineer.com/blink-without-delay-explained.html

Much, much easier than learning how interrupts work, and using them won't fix the problem anyway.

jremington:
As it is, the computer is delaying 99.9999% of the time, so of course it cannot read buttons.

Thank you jremington! I will read up on it and remove the delays.

if it is of any help, I have a simple traffic light sketch without delays:

// Traffic Lights
// for https://forum.arduino.cc/index.php?topic=649027.0
// by noiasca
// introduce the taffic manager

/* State                               NorthWest        WestEast
  NorthGo                               green             red
  NorthAwaitRed                         yellow            red
  NorthRed                               red              red
  WestAwaitGreen                         red            red yellow
  WestGo                                 red              green
  WestAwaitRed                           red              yellow
  WestRed                                red               red
  NorthAwaitGreen                      red yellow          red
*/
enum trafficLightColor {red, redyellow, green, yellow};  // available colors/colorcombinations on a traffic lights

// row, colum
const uint8_t sequenceA [8][3]
{
  { 8, green, red},         //interval, north, west
  { 2, yellow, red},
  { 2, red, red},
  { 2, red, redyellow},
  { 8, red, green},
  { 2, red, yellow},
  { 2, red, red},
  { 2, redyellow, red}
};

byte trafficStateCurrent = 0;                          // the current state of the traffic
const uint16_t factorIntervall = 500;                 // a factor for the intervall

class TrafficLight {
    trafficLightColor state = red;
    unsigned long previousMillis;
    const byte ledPinRed;
    const byte ledPinYellow;
    const byte ledPinGreen;

  public:
    TrafficLight(byte red, byte yellow, byte green):
      ledPinRed(red),
      ledPinYellow(yellow),
      ledPinGreen(green)
    {}

    void begin()
    {
      pinMode(ledPinRed, OUTPUT);
      pinMode(ledPinYellow, OUTPUT);
      pinMode(ledPinGreen, OUTPUT);
    }

    void setState(trafficLightColor _state)
    {
      state = _state;
      switch (state)
      {
        case red :
          digitalWrite(ledPinRed, HIGH);
          digitalWrite(ledPinYellow, LOW);
          digitalWrite(ledPinGreen, LOW);
          break;
        case redyellow :
          digitalWrite(ledPinRed, HIGH);
          digitalWrite(ledPinYellow, HIGH);
          digitalWrite(ledPinGreen, LOW);
          break;
        case green :
          digitalWrite(ledPinRed, LOW);
          digitalWrite(ledPinYellow, LOW);
          digitalWrite(ledPinGreen, HIGH);
          break;
        case yellow :
          digitalWrite(ledPinRed, LOW);
          digitalWrite(ledPinYellow, HIGH);
          digitalWrite(ledPinGreen, LOW);
          break;
      }
    }
};

TrafficLight trafficLightNorth(2, 3, 4);   // the traffic light North-South and the LED pins for red, yellow, green
TrafficLight trafficLightWest(5, 6, 7);    // the traffic light West-East and the LED pins for red, yellow,

void setup() {
  Serial.begin(115200);
  Serial.println(F("traffic lights"));
  trafficLightNorth.begin();
  trafficLightWest.begin();
}

void runTrafficController() {
  static uint32_t lastMillis =  -10000;                                                       // last update
  static uint8_t trafficStateIntervall = 0;

  if (millis() - lastMillis >= trafficStateIntervall * factorIntervall)  // do something by time
  {
    lastMillis = millis();
    trafficStateCurrent++;                                                           // --> simple switch to next state
    if (trafficStateCurrent >= 8) trafficStateCurrent = 0;                           //MISSING magic number ... abhängig von der jeweiligen Sequence
    
    Serial.print(F("sequenceA:")); Serial.println(trafficStateCurrent);
    trafficStateIntervall = sequenceA[trafficStateCurrent][0];
    trafficLightNorth.setState(sequenceA[trafficStateCurrent][1]);
    trafficLightWest.setState(sequenceA[trafficStateCurrent][2]);
  }
}

void loop()
{
  runTrafficController();
  // do what ever you want unblocked here

}

adding a digitalRead and force the state to a yellow/red should be easy.

this was the old thread: LED Sequence Setup - LEDs and Multiplexing - Arduino Forum

noiasca:
adding a digitalRead and force the state to a yellow/red should be easy.

Thank you, noiasca! I will try implement this into my program and see what I can do. Stay posted!