Interrupt help

Hi everyone,

I am having trouble figuring out how to get 2 interrupts working. This is a traffic light system. And pushbutton1 needs to turn all traffic lights red (PORTB=B100100 would mean all red lights in this case), and then if pushbutton2 is pushed it resumes the normal traffic pattern from the void loop ().

Any help would be appreciated!

int i;
const int pb1 = 2;
const int pb2 = 3;
volatile int buttonState = 0;

void setup() {
  // put your setup code here, to run once:
 for (i=8; i<=13; i++)
  pinMode(i, OUTPUT);

  
attachInterrupt(pb1, stopTraffic, CHANGE);  
attachInterrupt(pb2, restartTraffic, CHANGE);
    

}

void loop() {
  // put your main code here, to run repeatedly:
PORTB=B100001;
delay(5000);
PORTB=B010001;
delay(5000);
PORTB=B001100;
delay(5000);
PORTB=B001010;
delay(5000);
}
void stopTraffic() {      //ISR function for push button 1
  PORTB=B100100; 
  delay(5000); 
}

void restartTraffic() {      //ISR function for push button 2
 delay(10);
}

Get rid of the delays, get rid of the interrupts.

Why is that?

Neither is necessary, or desirable

Ok, I'm looking for help with the current code. I'll keep your advice in mind for the future.

If anyone else can help I'd appreciate it.

OK.
Don’t put delay() in interrupt context.

josrya89:
Why is that?

TheMemberFormerlyKnownAsAWOL:
Don't put delay() in interrupt context.

The MCU disables the 'interrupt logic' before going to ISR so that the MCU is not again interrupted until the current ISR is finished. In the ISR, you are executing delay() function which essentially needs 'interrupt logic' at enabled state to get executed (in fact delay() function works on Timer-0 interrupt). As the interrupt logic is already disabled, the delay() function is never finished and the MCU gets stuck at the ISR; it never returns to main line program (the loop() function).

josrya89:
Ok, I'm looking for help with the current code. I'll keep your advice in mind for the future.

If anyone else can help I'd appreciate it.

So, it's mandatory for your homework assignment?

Correct, homework.

So delay cannot be used inside an interrupt?

josrya89:
So delay cannot be used inside an interrupt?

What does the documentation say?

What documentation are you referring to?

The documentation for Arduino interrupts.

Ok, so delays won’t work in interupts.

So that leaves:

int i;
const int pb1 = 2;
const int pb2 = 3;
volatile int buttonState = 0;

void setup() {
  // put your setup code here, to run once:
 for (i=8; i<=13; i++)
  pinMode(i, OUTPUT);

  
attachInterrupt(pb1, stopTraffic, CHANGE);  
attachInterrupt(pb2, restartTraffic, CHANGE);
    

}

void loop() {
  // put your main code here, to run repeatedly:
PORTB=B100001;
delay(5000);
PORTB=B010001;
delay(5000);
PORTB=B001100;
delay(5000);
PORTB=B001010;
delay(5000);
}
void stopTraffic() {      //ISR function for push button 1
  PORTB=B100100; 

}

void restartTraffic() {      //ISR function for push button 2

}

See reply #1.

See reply #4

Dead-end.
Out.

But, you want to see that the traffic remains OFF for 5 seconds when you interrupt the MCU over INT0-pin and then resume the traffic from the point of the loop() function where it was interrupted. Where and how are you going to insert this time delay?

Similarly, when you interrupt the MCU at INT1-pin, the traffic must restart from the 1st line of loop() function. How are going to implement it?

Note that the external interrupts are asynchronous events meaning that the times of their arrivals are not known to the MCU; moreover, the loop() function is also free running.