attachInterrupt() NOT WORKING ?

can someone help with my interrupt?
I AM sending pulses, but the interrupt is not doing anything ? i’m using duemilanove

THE CODE:

int encPin = 2; //pulses sent to this pin

volatile int pulses=0;
long cm;
float speedMS, maxSpeed = 0;
float newTime, distance = 0.018786;
volatile float endTime,startTime;

void setup()
{
Serial.begin(9600); // initialize serial communication:
pinMode(encPin, INPUT); // sets the digital pin 2 as output

}

void loop() //START OF VOID LOOP
{
cm = 160;

if (cm > 150 || cm ==0)
{
attachInterrupt(0, blink, RISING); //grap the time to pulse at each low-to-high
}

if (pulses>1) //calculate speed each 2 pulses
{
newTime = (endTime-startTime)/1000000; //calculate time for 2 pulses (in seconds)
speedMS = distance /newTime; //calculate the speed (m/s) of the car
pulses=0; //reset the pulse counter
}

} //END OF LOOP

void blink() //keep track of pulse count AND the Time form pulse 1 to pulse 2
{
pulses++;
if (pulses==1)
{
startTime = micros(); //Time when pulse 1 is detected
}
if (pulses==2)
{
endTime = micros(); //Time when pulse 2 is detected
}

Confusing comment:

pinMode(encPin, INPUT); // sets the digital pin 2 as output

Don't do this in the main loop:

if (cm > 150 || cm ==0)          
{
attachInterrupt(0, blink, RISING);   //grap the time to pulse at each low-to-high
}

You haven't detached the interrupt, so there isn't much point doing it every time through, and it may have some side-effect.

Where does cm change?

How do you know it isn't working? I don't see any debugging display.

What is the nature of the input? A light sensor? A switch? How is it wired?

What is generating the pulses? Does it need debouncing?

micros() stops working in an ISR, it will I think still return a value but if you are interested in timing to that degree it would be worth moving the if()... code from the ISR.

if (cm > 150 || cm ==0)          
{
attachInterrupt(0, blink, RISING);   //grap the time to pulse at each low-to-high
}

This will be true every loop, no need to constantly attachInterrupt.


Rob

I posted part of my code.

Where does cm change? changes in the loops

What is the nature of the input? A light sensor? A switch? How is it wired?

the input is i digital encoder with infrared light transmitted all the time.
every time the infrared light is CUT, it send a pulse.

I want to read the number of pulses all the time and CAPTURE the time between pulses. that is why I put the attachInterrupt() in the LOOP.

i'm feeding the pulse to digital input 2 (interrupt Pin in the duemilanove)
(cm) does change in my code, but for the sake of this forum i set it to 160 to capture time. I'm not capturing anything. HELP?

i am a biggner i don't even know what ISR stand for

Bassam:
I posted part of my code.

The part you didn't post might be the problem.

Bassam:
I want to read the number of pulses all the time and CAPTURE the time between pulses. that is why I put the attachInterrupt() in the LOOP.

Once the interrupt is attached it stays attached. You don't need to do it every time in the loop.

Bassam:
i am a biggner i don't even know what ISR stand for

ISR = Interrupt Service Routine - the thing you called "blink".

Once again, how do you know it isn't working? And are you sure you are getting the pulses?

Let me put it another way. attachInterrupt works. I was using it earlier today to capture pulses. So that isn’t the problem. The problem could be the code, possibly the parts we can’t see. Possibly doing attachInterrupt thousands of times. Possibly in your code cm is 120 - you said “for the sake of this forum i set it to 160” so if it was not in the range 150+, or zero, the attachInterrupt isn’t done. Possibly your wiring is incorrect. But attachInterrupt works.