Go Down

Topic: Use 2 different interrupt input.. (Read 1 time) previous topic - next topic

portalsole

May 04, 2015, 02:29 pm Last Edit: May 04, 2015, 03:28 pm by portalsole
I do this sketch to measure the time between two digital pulses arriving on pin 2.
I have handled an interrupt for this purpose:


Code: [Select]

byte sensorInterrupt = 0;
volatile unsigned long delta_pulse = 300001;// start up value
extern unsigned long timer0_millis;

void setup () {

attachInterrupt (sensorInterrupt, pulse, FALLING); // Interrupt pulse
}

void loop () {

Serial.println (delta_pulse);

void pulse () {


   if ((timer0_millis)> (1000)) {// ignore if  receive impulses lower than 1 second..
   delta_pulse = timer0_millis;
   timer0_millis = 0; // change the value of the register
 
   
 
     }
}
}




Now I need to implement an additional measure using the other fisical interrupt input ( pin3) ,  in order to have two distinct measures of time ...

I can not understand how I can use 2 different timer i 2 different interrupt.

Any idea ?


stoutfiles

Move the attachInterrupt out of setup().
Use detachInterrupt when you have your value.

There are a lot of good examples here.

http://www.gammon.com.au/forum/?id=11504

Adding a second interrupt would just be duplicating the functions,  and changing some values to use Pin 3 instead of Pin 2 for the second interrupt.

tmd3

Please put your code inside [ code ][ /code ] tags.

It took three fixes to get this code to compile:  
  • "bytes" isn't a type, changed to "byte";
  • "Pulse" isn't defined, changed to "pulse"; and
  • It looks like the closing bracket of loop() is missing.

That makes me think you've never used this code, and that your actual sketch is something different.  I'm happy to see that you're posting a minimal sketch to the forum, rather than a huge sketch.  It's important, though, that the code you post compiles, and demonstrates the problem.  I can't verify that the changes I made result in what you actually intended.

Can you test this code, get it to compile, and verify for us that it demonstrates the problem?

aarg

Move the attachInterrupt out of setup().
Really? Move it to where? It's quite normal for it to be in setup().
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

stoutfiles

Really? Move it to where? It's quite normal for it to be in setup().
In the loop of course, preferably inside a function call in loop().  If he's using two interrupts it makes no sense to attach in setup since he's going to have to detach it for the second interrupt.

aarg

#5
May 04, 2015, 03:15 pm Last Edit: May 04, 2015, 03:15 pm by aarg
In the loop of course, preferably inside a function call in loop().  If he's using two interrupts it makes no sense to attach in setup since he's going to have to detach it for the second interrupt.
Why would you need two interrupt handlers just to measure consecutive pulses? ISRs can hold state information in static variables.
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

portalsole

thanks.. :)

My code is working since 2 years.

The timer0_millis autoincrement every millisec and i reset this register when an interrupt occurs.

If i use another interrupt i cannot use this register because this is reserved in first interrupt routine.

What i can use instead of    "timer0_millis".

thans,
Fabrizio.

aarg

That's the advantage of a free running timer. Any routine can use it.
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

portalsole

..
It took three fixes to get this code to compile:  
  • "bytes" isn't a type, changed to "byte";
  • "Pulse" isn't defined, changed to "pulse"; and
  • It looks like the closing bracket of loop() is missing.

That makes me think you've never used this code, and that your actual sketch is something different.

..


Ho.. yes.. this is a google traslator auto- aggiustament :)
Sorry.. now i have correct


portalsole

That's the advantage of a free running timer. Any routine can use it.
Yes but i reset it every interrupt... a cannot use it for second interrupt event..


timer0_millis = 0; //

aarg

#10
May 04, 2015, 03:56 pm Last Edit: May 04, 2015, 04:02 pm by aarg
Yes but i reset it every interrupt... a cannot use it for second interrupt event..


timer0_millis = 0; //
Exactly. Don't reset it. Use millis(), save timestamps instead of resetting.
millis() can be safely called once from inside an ISR, but not repeatedly because it is not incrementing.
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

dlloyd

#11
May 04, 2015, 04:02 pm Last Edit: May 04, 2015, 04:05 pm by dlloyd
Now I need to implement an additional measure using the other fisical interrupt input ( pin3) ,  in order to have two distinct measures of time ...
Your first measurement of time is from falling edge to falling edge of the signal (period). Is your second time measurement using the same signal to measure duty cycle, HIGH or LOW time? If so, then its possible to use just one interrupt that detects logic change to trigger the various measurements.

EXTERNAL INTERRUPTS ON THE ATmega168/328

aarg

... or are you measuring two separate signals?
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

stoutfiles

Why would you need two interrupt handlers just to measure consecutive pulses? ISRs can hold state information in static variables.
He just said he wants to measure a signal on Pin 2, and a signal on Pin 3.  He needs two interrupt handlers, 0 and 1.

portalsole

... or are you measuring two separate signals?
Yes.. two separate signal.. the first is count pulse related to a water flow.. the new related to a future power measurament.

Exactly. Don't reset it. Use millis(), save timestamps instead of resetting.

I try to understand.. sorry but i'm newbie in arduino enviroment..

 ...something like this is correct ? :

Code: [Select]


void pulse () {

   old_pulse = last_pulse;
   last_pulse = timer0_millis;

   delta_pulse = (last_pulse - old_pulse);

  
     }



In this manner i can use two different interrupt routine with different variables but the same timer0_millis register...

But the question is..:
with this approach I have to handle the timer0_millis overflow  ?.

thank in advance,
Fabrizio.



Go Up