I am trying to use interrupts
The board is Teensy 3.2, and I have a button connected to pin 0 and GND (normally open).
I am trying to catch only when the button is pressed and then when released.
During setup I have the following
And the routine is:
When I push (and hold) the button down I want to get one interrupt (and the routine prints "Button Pressed" at the end)
What I actually get is the following repeating itself until I let go of the button:
[repeats same message block]
What is the proper way to code this?
Thanks
Ric
The board is Teensy 3.2, and I have a button connected to pin 0 and GND (normally open).
I am trying to catch only when the button is pressed and then when released.
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/interrupt.h>
During setup I have the following
pinMode(0, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(0), IServiceChange, CHANGE);
attachInterrupt(digitalPinToInterrupt(0), IServiceChange, CHANGE);
And the routine is:
void IServiceChange()
{
cli();
Serial.println("IServiceChange");
uint8_t r1 = digitalRead(0);
delay(500);
uint8_t r2 = digitalRead(0);
Serial.print("r1 = "); Serial.print(r1);
Serial.print(", r2 = "); Serial.println(r2);
if (r1 == r2)
{
bool b = (r1 == HIGH ? false : true);
if (b)
{
Serial.println("Button Pressed");
}
else
{
Serial.println("Button up");
Serial.println("");
}
sei();
}
}
{
cli();
Serial.println("IServiceChange");
uint8_t r1 = digitalRead(0);
delay(500);
uint8_t r2 = digitalRead(0);
Serial.print("r1 = "); Serial.print(r1);
Serial.print(", r2 = "); Serial.println(r2);
if (r1 == r2)
{
bool b = (r1 == HIGH ? false : true);
if (b)
{
Serial.println("Button Pressed");
}
else
{
Serial.println("Button up");
Serial.println("");
}
sei();
}
}
When I push (and hold) the button down I want to get one interrupt (and the routine prints "Button Pressed" at the end)
What I actually get is the following repeating itself until I let go of the button:
IServiceChange
r1 = 0, r2 = 0
Button Pressed
IServiceChange
r1 = 1, r2 = 1
Button up
r1 = 0, r2 = 0
Button Pressed
IServiceChange
r1 = 1, r2 = 1
Button up
[repeats same message block]
What is the proper way to code this?
Thanks
Ric