hello, i want to generate an interrupt when the digital pin reads a rising signal but what i got nothing so i tested with analog read and it works perfectly
int interup=3;// pin 3 sur arduino leonardo
volatile int flag=0;
void setup() {
Serial.begin(9600);
attachInterrupt(interup, TEST, RISING);
}
void TEST ()
{
flag= 1;
}
void loop() {
//Serial.println(digitalRead(3));
if ( flag == 1){
Serial.println("interuption");
flag= 0;
}
else{ Serial.println(" NO interuption");}
/*if ( digitalRead(3)== HIGH){
Serial.println("interuption");
}
else{ Serial.println(" NO interuption");}*/
}
What pin is your signal wired to? Interrupt 3 on Leonardo uses pin 1. The commented code makes it look like you are using pin 3, which would be interrupt 0.
[/code]
volatile lets variable state be used across functions, sets it to LOW (0) to start.
attachInterrupt(0, blink, CHANGE);
Interrupt 0 is "called" when the pin CHANGEs from 0 to 1, or 1 to 0. Code at function blink() is run.
Function blink:
void blink()
state = !state;
[/code]
changes value of state from 0 to Not 0, or Not 0 to 0.
Not 0 is probably -1 (0xFFFF) for data type int.
I use type byte myself, and use
state = 1 - state;
1 - 0 = 1,
1 - 1 = 0,
1 - 0 = 1,
1 - 1 - 0,
etc, to toggle a variable.