attachInterrupt() problem

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");}*/
}

please i'm lost here

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.

What is wired to that pin? And how?

The interrupt pin number is not the same as the first parameter of the attachInterrupt function.

The interrupts are numbered 0 and 1, although they are on pins 3 and 4. Or something like that. This has fooled many people. Look it up and check.

pin 3 is interrupt 0 on arduino ,and i'm wiring it to an other arduino that is sending a signal HIGH

michinyon:
The interrupt pin number is not the same as the first parameter of the attachInterrupt function.

The interrupts are numbered 0 and 1, although they are on pins 3 and 4. Or something like that. This has fooled many people. Look it up and check.

i didnt know that i'm gona try with 0 or 1

That's for an UNO. A comment in your code implies you are using a Leonardo in which case the numbering is different.

Look here: http://www.arduino.cc/en/Reference/AttachInterrupt

Delta_G:
That's for an UNO. A comment in your code implies you are using a Leonardo in which case the numbering is different.

Look here: http://www.arduino.cc/en/Reference/AttachInterrupt

for the arduino leonardo the INT0 is 3 and INT1 is 2 , i tried it and i didnt work either

hiiiro:
for the arduino leonardo the INT0 is 3 and INT1 is 2 , i tried it and i didnt work either

With which code? Neither of those pins would work with the code you posted earlier. Did you change pins or change code?

The code you had in the first post uses INT3, so that's pin 1.

Delta_G:
With which code? Neither of those pins would work with the code you posted earlier. Did you change pins or change code?

The code you had in the first post uses INT3, so that's pin 1.

i changed my code to use INT0

hiiiro:
i changed my code to use INT0

Post that. So the signal (you still haven't said what it is) is still connected to pin 3?

What is producing the signal? Might it need a pull-down resistor?

I can see why people have trouble using attachInterrupt()

This is the example from the link in reply #5

Example

int pin = 13;
volatile int state = LOW;

void setup()
{
  pinMode(pin, OUTPUT);
  attachInterrupt(0, blink, CHANGE);
}

void loop()
{
  digitalWrite(pin, state);
}

void blink()
{
  state = !state;
}

This example is more confusing than useful. What is it supposed to do ?

And what does !state mean, when state is an int variable ?

volatile int state = LOW;

[/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.

Not 0 depends on which "not" you use.

The bitwise not ~0 would give 0xFFFF.

The boolean not !0 would give 1.

If !someInt bothers you, then you'd really freak out over the oft used !!someInt which is used to force any non-zero integer to 1.

void setup(void){
  
  int x = 1234;
  
  int notx = !x;
  
  int notnotx = !!x;
  
  Serial.begin(9600);
  
  delay(20);
  
  Serial.print("x = ");
  Serial.println(x);
  
  Serial.print("notx = ");
  Serial.println(notx);
  
  Serial.print("notnotx = ");
  Serial.println(notnotx);
  
}

void loop(void){}

outputs:

x = 1234
notx = 0
notnotx = 1