Interrupt has no effect

Hi,
… and sorry for bothering you with another beginner question…

My actual project is very simple.
I use one of those starter sets from amazon to make this.
I have modified “Lesson 5 Digital Inputs”

The Setup looks like this “Schaltplan LED.PNG”
Since Interrupts work only at Port 2 and 3 I conected the switches there.
Most of the sketch is the same as the one in the lesseion (where everything works fine)

I do need to get in touch with interrupts, because I want to integrate something like an emergency-stop button. (for a later more complicated sketch)

So what I want this sketch to do is wait … until the first button is pressed.
This set x to 3 and the while loop makes the LED blink 3 times
In case of an “emergency” the sceond button is pressed and the value x should be set to 0 which ends the loop and everything is stopped.
Until the first button is pressed again setting x to 3… and so on

Not too complicated, but it doesn’t work the way I did it.

Any ideas, wherer is missed the important part :wink: ?
Thanx

sketch_Button_Start-Stop_V2.ino (591 Bytes)

Schaltplan LED.PNG

Not too complicated, but it doesn't work the way I did it.

Please explain what it does instead.

Code, posted properly (see the "How to use this forum" post).

int ledPin = 5;
int buttonApin = 2;
int buttonBpin = 3;
volatile int x = 5;

void setup() 
{
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  pinMode(buttonApin, INPUT_PULLUP);  
  pinMode(buttonBpin, INPUT_PULLUP);  
  attachInterrupt(buttonApin, functionSTART, LOW);
  attachInterrupt(buttonBpin, functionSTOP, LOW);
}

void loop() 
{
  while (x > 0) {
  digitalWrite(ledPin, HIGH);
  delay(250);
  digitalWrite(ledPin, LOW);
  delay(250);
  x--;
  }
}

void functionSTART() {    
    x = 3;
   }
   
void functionSTOP() {    
    x = 0;
   }

If you are really using a breadboard in exactly the manner shown in the posted diagram, use your multimeter to verify that the switches are connected to Arduino GND when closed. Some breadboards have no connection across the horizontal gap.

Thanx for your quick response.

Actually it is doing nothing. It does not react to any button.

The Setup itself is working. With the original sketch, it does what it is suposed to do and I did not change something when using this sketch.

The switches are connected to GND, just measured with my multimeter to be sure.

From the attachInterrupt() documentation:
https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/

attachInterrupt(pin, ISR, mode) (Not recommended. Additionally, this syntax only works on Arduino SAMD Boards, Uno WiFi Rev2, Due, and 101.)

The syntax you're using in your attachInterrupt calls doesn't work on your Uno and is not recommended no matter which board you're using. You need to use this syntax:

attachInterrupt(digitalPinToInterrupt(pin), ISR, mode)

Do NOT use the LOW and HIGH interrupt modes! They will tie up the processor for the ENTIRE time the input pin is LOW or HIGH. Use CHANGE, FALLING or RISING modes only.

Regards,
Ray L.

 attachInterrupt(buttonApin, functionSTART, LOW);
  attachInterrupt(buttonBpin, functionSTOP, LOW);

If you are using a Uno as in your attachment then check the syntax for the attachInterrupt() function

What is the first parameter ?
HINT : it is not a pin number

And make 'x' (which could use a better name btw) to a byte. Or, if you want the option to make it >255, disable interrupts while reading or writing from/to it from the normal program.

Problem solved :wink:

UKHeliBob:

 attachInterrupt(buttonApin, functionSTART, LOW);

attachInterrupt(buttonBpin, functionSTOP, LOW);



If you are using a Uno as in your attachment then check the syntax for the attachInterrupt() function

What is the first parameter ?
HINT : it is not a pin number

Thanx!

Don't forget to fix reply #6 as well. Or you can have weird (random) surprises :wink: