Hello and good day for all. I try to read pulse width with PulseIn, which the signal have two difference pulse width. Not matter how fast the frequency change it always one pulse width is longer then other one.
I try to add the PulseIn value into array to separate bigger pulse width and smaller pulse width into to variable. But it have some delay.
So the idea is to digitalWrite pin 13 when it detect longer pulse width... i try with static frequency it work. But cannot if the frequency is changing.
First of all, you don't need while(1){ inside the loop() function because it's already an infinite loop.
Second, I wonder why you test for pulsewidth greater than 16000 and less than 17000: if the pulse is between those values both if() will be executed (and in fact the led will be on). Shouldn't it be tested with the same "trigger" value?
Third, pulseIn() function returns ad unsigned long, not a float.
Last, after pulseIn(5, HIGH) the input pin should always be LOW, why test it again? (and remember, before calling pulseIn() the input should be LOW to get the real pulse duration).
So, why not give this a try? I added some serial printing just to better debug things (but with higher speed to avoid delays), but if the pulse frequency does not allow serial print, just comment them out.
To create good, readable code, use comment signs and comments to tell what's the purpose of the lines. That will serve You some day in the future when You either want to update the code or use it as a start in new projects.
Basically I try to get only one pulse which represent 131' Degree TDC like above picture. It can be accomplish if the CAM signal is static like simulator signal.
I tested docdoc code, it work if the PulseIn signal is static width value.
digitalWrite(13, (pulsewidth < 17000));
Is there any method to separate two difference width time measure read by PulseIn?
Example code / idea:
unsigned long pulsewidth = 0;
unsigned long pulseArray[1];
int pos = 0;
void setup() {
Serial.begin(115200);
pinMode(5, INPUT);
pinMode(13, OUTPUT);
}
void loop() {
pulsewidth = pulseIn(5, HIGH);//Get Pulse width value when pin 5 is HIGH
pulseArray[pos] = pulsewidth; //Adding PulseIn value into array
pos++;
if (pos >= 1) //Set only two width value store in [0] and [1]
{
pos = 0; //Reset pos count to zero if counter bigger than
}
Serial.print("P1"); Serial.print(pulseArray[0]); Serial.print(" P2"); Serial.println(pulseArray[1]);
digitalWrite(13, (pulseArray[0] < pulseArray[1])); // Digitalwrite 13 HIGH if width value bigger than another width value.
}
It is not a good idea, because in real world two consecutive measurements will almost always give different results, at least by a couple of units - which means that one impulse will always be greater than the other.
It is not a good idea, because in real world two consecutive measurements will almost always give different results, at least by a couple of units - which means that one impulse will always be greater than the other.<
Yes I try these, It always changing place not constantly pulse as i want. The width value randomly insert into array.
With these pin13 Output, I can use to connect with AND gate with Ignition signal to get 1 Cylinder TDC signal. Later I can write a code to separate the ignition output to run individual coil
Real world question! How long after you discover the TDC pulse, will the real TDC appear?
I submit you are using the wrong approach for timing the spark ignition for you motor. The ignition for an internal combustion engine MUST occur before TDC because the flame in the combustion chamber takes time to fully produce the pressure pulse that drives the piston. And that time will vary based on the RPM of the engine.
Consider the real TDC occurs at the center of the long pulse, not after the long pulse.
The long pulse should tell your program to begin counting the short pulses and after counting all the short pulses, the BEGINNING of the next pulse, the long pulse, is where the ignition spark should occur, after a delay based on engine RPM.
These is only re-arrange sequential ignition signal from stock ecu. The pulse I need to tell arduino where the first cylinder signal arrange new output for individual ignition coil signal.
These already work, and running fine when I added one additional trigger plate at distributor. But now I just want to try modify the cam signal to replace additional trigger plate.