Go Down

Topic: How can i measure pulse width? (2 interrupts) (Read 10083 times) previous topic - next topic

rukez

I need to measure pulse width (not time between two pulses, but length of one single pulse - from raising to falling) on fuel injector - it is driven by simple square pulse on ground wire (positive wire is allways attached to battery's +)
Can i assign TWO interrupts to ONE pin? i tried this:
Code: [Select]

int injPin = 3; //Pin connected to injector's ground wire
volatile unsigned long injTime1;  //Time of front raising
volatile unsigned long injTime2;  //Time of front falling
unsigned long injTime;

void setup()
{
 pinMode(injPin, INPUT);
 digitalWrite (injPin, HIGH);  //Pull-up pin - as our injector is driven by connecting it to ground, i suppose i need to pull-up pin to get it's value as 1 when injector is turned off
 attachInterrupt(1, up, RAISING);  //interrupt on raising front
 attachInterrupt(1, down, FALLING);  //interrupt on falling front
}

void loop()
{
//some code
}


void down()
{
injTime1 = micros(); //get time of pulse going down
}
void up()
{
 injTime2 = micros();  //get time of pulse going up
 injTime = injTime2 - injTime1;  //measure time between down and up (so it will be time between ground appearing ot pulled-up pin, and ground disappearing)
}


but my "down" (on falling) interrupt didn't fired even when i removed pull-up - can i use 2 interrupts on 1 pin, or i need to shortcut another interrupt-enabled pin (pin 2 to pin 3) and use two pins for two interrupts?

maybe someone will purpose some simple external logic to measure pulse width?

thanks!  :)

Coding Badly


As you've discovered, two interrupt conditions cannot be attached to one pin.  Instead, use a CHANGE interrupt and simply check the condition of the pin in your interrupt service routine.

rukez

Oups  :-[
Thanks a lot!
So looks like i need this:

Code: [Select]
int injPin = 3; //Pin connected to injector's ground wire
volatile unsigned long injTime1;  //Time of front raising
volatile unsigned long injTime2;  //Time of front falling
unsigned long injTime;

void setup()
{
 pinMode(injPin, INPUT);
 digitalWrite (injPin, HIGH);  //Pull-up pin - as our injector is driven by connecting it to ground, i suppose i need to pull-up pin to get it's value as 1 when injector is turned off
 attachInterrupt(1, pik, CHANGE);  //interrupt on front changing
}

void loop()
{
//some code
}

void pik()
{
if (injPin == LOW) //Or i need to digitalRead it first?
{
 injTime1 = micros(); //get time of pulse going down
}
else
{
 injTime2 = micros();  //get time of pulse going up
 injTime = injTime2 - injTime1;  //measure time between down and up
}
}


hope it will work, will try it in a couple of days  8-)

Coding Badly

Code: [Select]
int injPin = 3; //Pin connected to injector's ground wire
[glow]volatile [/glow]unsigned long injTime; [glow]// When accessing this in setup or loop (or anything called by either) interrupts must be disabled[/glow]

void setup()
{
 pinMode(injPin, INPUT);
 digitalWrite (injPin, HIGH);  //Pull-up pin - as our injector is driven by connecting it to ground, i suppose i need to pull-up pin to get it's value as 1 when injector is turned off
 attachInterrupt(1, pik, CHANGE);  //interrupt on front changing
}

void loop()
{
//some code
}

void pik()
{
[glow]unsigned long injTime1;  //Time of front raising[/glow]
[glow]unsigned long injTime2;  //Time of front falling[/glow]

[glow]if ( digitalRead( injPin ) == LOW ) //Or i need to digitalRead it first? YES.[/glow]
{
 injTime1 = micros(); //get time of pulse going down
}
else
{
 injTime2 = micros();  //get time of pulse going up
 injTime = injTime2 - injTime1;  //measure time between down and up
}
}

rukez


Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy