Pages: [1]   Go Down
Author Topic: How can i measure pulse width? (2 interrupts)  (Read 5917 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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!  smiley
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 176
Posts: 12283
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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-)
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 176
Posts: 12283
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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
 }
}
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks a lot for your remarks!  smiley
Logged

Pages: [1]   Go Up
Jump to: