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