Pages: [1]   Go Down
Author Topic: measuring engine rpm using induction coil technique.  (Read 1591 times)
0 Members and 1 Guest are viewing this topic.
New Delhi,India
Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hey all,
i want to calculate the speed of my engine using the coil method,and i need to calculate the frequency of the ignition pulses through the arduino...
i have tested a code which gives me the on and the off time for a pulse graph ( used a hall effect sensor and a magnet)..and i need to calculate the frequency of the total number of "on's"...how should i go about it?..should i just divide the time by 1..(1/time)..for the on time,but the on time is slightly different for every time i pass the magnet around the hall effect sensor.

code for pulses:

int pulsePin = 3;
unsigned long highCounter = 0;
unsigned long duration = 0;
int pulse = 0;
int lastPulse = LOW;
unsigned long timeNow = 0;
unsigned long lastTime = 0;


void setup() {
   pinMode(pulsePin, INPUT);
   digitalWrite(pulsePin, HIGH);   
   Serial.begin(9600);
   Serial.println("start");
}

void loop() {
  pulse = digitalRead(pulsePin);
  if (pulse != lastPulse)
  {
    timeNow = millis();
    duration = timeNow - lastTime;
    Serial.print(pulse);
    Serial.print(" ,");
    Serial.println(duration);
    lastPulse = pulse;
    lastTime = timeNow;
    if (pulse == HIGH)
    highCounter++;
   
  }
 
 
}

i have tried using the attach interrupt function and written another code..but something ( or manythings) are wrong here...because the output never stops and keeps switching between 1,6 and 8.. smiley-eek
code:
int pulse = 3;
unsigned int timenow=0;
unsigned int timelast=0;
 int input=0;
 int counter=0;
unsigned int duration =0;


void setup()
{
  pinMode(pulse,INPUT);
  digitalWrite(pulse,HIGH);
  Serial.begin(9600);
  Serial.println("start");
  attachInterrupt (0, freq , CHANGE);
}
 
  void loop ()
  {
    timenow = millis();
    input = digitalRead(pulse);
    duration = timenow-timelast;
   
    Serial.println(input);
    Serial.println(duration);
    timelast=timenow;
 
  }
  void freq()
  {
    if (duration == 500)
  { counter =0;
  }
  else
    {
       counter ++;
  }
  }

   
 
i have to calculate the frequency,so that i can calculate the rpm by multiplying it with 60.
cheers.
Logged

United Kingdom
Offline Offline
Tesla Member
***
Karma: 224
Posts: 6614
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The accurate way is to have the Hall sensor trigger an interrupt, and measure the time interval between interrupts. Something like this:

Code:
volatile unsigned long lastInterruptTime;
volatile unsigned long interval;

void isr()
{
  unsigned long now = micros();
  interval = now - lastInterruptTime;
  lastInterruptTime =now;
}

The in loop() you calculate the rpm from 'interval'.

Please use the # tool when posting code.
Logged

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

New Delhi,India
Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thank you.will try doing with that.
will remember about the # next time.
Logged

Pages: [1]   Go Up
Jump to: