hi, sorry for my bad english,
i trying to test interrupt button,
this is the code :
const byte interruptPin = 19;
volatile byte state = LOW;
byte H = 0, L = 0;
void setup() {
Serial.begin (9600);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
}
void loop() {
digitalWrite(LED_BUILTIN, state);
}
void blink() {
if (digitalRead(interruptPin) == HIGH)
{
//turn off
state = HIGH;
H++;
Serial.print("H :");
Serial.println(H);
}
else
{
state = LOW;
L++;
Serial.print("L :");
Serial.println(L);
}
}
and the result is
H :1
L :1
L :2
L :3
H :2
L :4
L :5
L :6
H :3
H :4
L :7
H :5
L :8
L :9
L :10
H :6
H :7
L :11
H :8
L :12
H :9
as you can see, the LOW detected 3 times, and HIGH detected 1 time in the first 4 output,
my question is: can the interrupt button combine with debounce?
and thanks for reading,
The difference between L and H could not exceed 1 if the code would work correctly.
Don't Serial.print from an ISR.
Declare all variables shared between ISR and main code volatile.
Don't use interrupts for buttons.
I use Bounce2 to handle debouncing of buttons.
Whandall:
The difference between L and H could not exceed 1 if the code would work correctly.
Don't Serial.print from an ISR.
Declare all variables shared between ISR and main code volatile.
Don't use interrupts for buttons.
I use Bounce2 to handle debouncing of buttons.
seems my code wrong in many ways,
i just want to test "interrupts for button", but it seems bad idea,
thanks for the reply,
Using an interrupt to monitor a switch as an experiment is OK, but is not really good practice in a "real" program. Interrupts are to detect events that happen quickly, a button pushed by a human is very slow in microcontroller terms. You can use software debounce as suggested by Whandall or hardware debounce. Hardware debounce is simply a 0.1uf (or so) capacitor across the switch (1 leg to ground and the other to the switch pin).