Hi guys, I'm reading off a signal from an ignition coil primary winding. I don't have a scope so i can tell you the correct voltage it produces but my MM says 4.5-5.8v see attached.
This is the code:
int pulsePin = 10;
unsigned long RPS;
unsigned long total;
unsigned long average;
void setup() {
Serial.begin(9600);
}
void loop() {
total = 0;
for (int i = 0; i <10; i++){
unsigned long interval = pulseIn(pulsePin, HIGH) + pulseIn(pulsePin, LOW); // Pulse interval in microseconds
RPS = (1000000UL / interval);
Serial.print("RPS: ");Serial.println(RPS);
total = total + RPS;
delay(1000);
}
average = total / 10;
Serial.print(average); Serial.println("RPS");
delay(2000);
}
it seems to work well except for (usually) a single stray reading tha'ts way way off what it should be, sometimes i get all ten readings the same but usually its just a single stray reading upsetting the average.
So one cycle i usually get:
Could it be that the pulse is being read mid point of the signal and hence calculating a greater RPS. But as i understand it PulseIn waits for a rising edge before it reads??
Another anomaly is the count of 72, this implies that its counting at 4320 RPM - could this be the rise and fall time of the opto? which is stated as 18uS Max (3-4Typical) which seems negligible.
so the high signal appears to be solid, and looks to me that the Low is probably getting interrupted by noise or erratic spikes, even so looking at the results even the correct readings are being interpreted incorrectly.
I'll move the circuit off the breadboard onto a proto board to see if it helps.
just a quick note i've gone thru the readings and on the high side there is only a difference of 13uS in the print out 1258 lowest and 1271 highest,
disregarding the spurious readings on the low side its just 328uS so the program works.
Danois90:
Please use code tags, it looks horrible without. You are reading the "same" values twice and the printouts are not separated by a line break..
pHigh = pulseIn(pulsePin, HIGH); //High first
pLow = pulseIn(pulsePin, LOW); //Low first
unsigned long interval = pulseIn(pulsePin, HIGH) + pulseIn(pulsePin, LOW); //High & Low second
Serial.print(pHigh);Serial.print(" : ");Serial.print(pLow); //No line ending
You don't need to use pulseIn() 4 times. After reading in the high pulse and the low pulse, just add those two values together and store in interval. If you look at your data, you will notice that they don't "add up" if you add pHigh and pLow and then convert to RPS.
i turned down the idle speed slightly and it makes sense from the last readings, so it looks definitely like a bounce or noise, being that its a very (mostly) short period after the high time i suspect a bounce even more. (or is a 161 uS bounce a bit long after the high drops out?)
johnwasser i think you misunderstood the readings (probably bit clearer now), but on the left is the magnet passing over the coil (the HIGH period) and on the right the low period and then the calculated RPS. Any chance you could advise me on how to disregard the readings, as you said?