External Interrupt question

Hello.

I am trying playing with external interrupts as i am new to this.

The sketch i wrote is atached.

I choosed to call the “react” function with atachinterrupt at a falling edge, so when i press the button and pin 2 goes from 5 volt to 0 volt
and println “tttttttttttttttttttttttt”.

But when i depress the button the function “react” is called again, now when pin 2 goes from 0 volt to 5 volt.

Why?

Thanks.

interrupt_experience.ino (641 Bytes)

The button is probably bouncing a few times on release: open,closed,open,closed. You have to release the button within 1/2 second of pressing it or the bounce will look like another press.

volatile long  val;
volatile long  val2;

int botao =2;

void setup()
{

  Serial.begin(9600);
while(!Serial){;};
  //LED output
  pinMode(13,OUTPUT);
  //switch input
  pinMode(2,INPUT);

  //set the pull-up on the switch
  digitalWrite(2,HIGH);
  
  attachInterrupt(1,react,FALLING);  
}

void loop()
{
Serial.println("Your reaction time: ");
delay (3000);
digitalWrite(13,LOW);

}

void react()
{
  val2=millis();
  
 if (val2-val>500)
  {
  digitalWrite(13,HIGH);
  //millis();
  //long fin = millis();

  //detachInterrupt(0);

  Serial.println("Led 13 turned on ");
  //Serial.println(millis());
//detachInterrupt(1);
}
val=val2;
}

I dont think you can use millis inside interrupts

from reference:

Inside the attached function, delay() won't work and the value returned by millis() will not increment. Serial data received while in the function may be lost. You should declare as volatile any variables that you modify within the attached funct

I dont think you can use millis inside interrupts

You can, you just have to recall that it’s value won’t change inside the interrupt handler. But storing the value of millis(), so keeping the time when the interrupt occurred is very feasible. But you shouldn’t write to the serial interface within the handler because the interface is handled by interrupts too and interrupts are blocked during an interrupt handler. So if you fill up your serial buffer the call to Serial.println() will block till the buffer gets at least partly cleared and this will never happen because the necessary interrupt is blocked. So you produced an endless loop.

Thanks for replys guys.

I want to be familiar with interrupts because my goal is to read rpm from a tooth ring wheel atached to a motor.

I am using a inductive pick up sensor, and converting the sine to square wave with a LM 393 and a pull up resistor.

Actually i already made the sketch myself and it reads good values but my problem now is optimization…

One of the things i cant figure out is:

1.What can i do to observe the values of rpm in the serial monitor quietly. Because the values change a lot fast since the pick up routine is inside the interrupt, and the values change very fast.

  1. Do not overflow the serial port, since every time i open the sketch and spin the motor, 10 seconds later or so the sketch freeze or blocks…

Do you have suggestions?

Thanks agains.

interrupt_experience.ino (1.43 KB)

Don't do the writes to the serial interface in the interrupt handler! If you fill up the serial buffer the write will block until the buffer empties again and because the interrupts are blocked within an interrupt handler (and the buffer clearing is done using interrupts) it will block forever.

In the interrupt handler just alter the variables (must be declared volatile) and react on that in the main loop. There you can wait for some time using the millis() value and write it to the serial interface in volumes not so problematic.

Thanks.

Allready did that alteration and it works fine. The sketch is doing what i want, and you can find it in this thread:

http://arduino.cc/forum/index.php/topic,127323.0.html

Thanks everyone.