I'm new to this so I apologize if something doesn't make sense. I am using a DUE to generate signals for an Engine Control Unit. I have a couple of problems that I can't find answers for. A digital out put signal is used to generate a signal simulating a camshaft signal and a second signal simulates a signal from a crankshaft position sensor. The crank shaft has 3 sets of 20 teeth and each set is separated from the others with a "missing tooth.
Two major problems I'm dealing with; 1st, the duration of the tooth signals changes, maybe 3 different pulse width going from long to shorter with shorter maybe being 1/3 to 1/2 of long. The high state is high about 1/4 the time vice 1/2. The second problem is that I don't see a missing tooth. I have code to keep the tooth signal low at the end of the 20 teeth but it doesn't seem to be effective.
A copy of the sketch is below. Thanks in advance.
const int RPM_c = A0; // Coarse RPM input from JimStim to A0
const int RPM_f = A1; // Fine RPM input from JimStim to A1
const int Tooth_Out = 22; // Tooth output signal to pin 22
const int Cam_Out = 23; //Cam output signal to pin 23
int n; //counter
int t; //timer value
void setup() {
pinMode(Tooth_Out, OUTPUT); // enable output for tooth
pinMode(Cam_Out, OUTPUT); // enable output for cam
analogReadResolution(12);
Serial.begin(9600); // Open serial port and sets baud rate
Serial.flush(); // flush residual characters
}
void loop() {
int RPM_C=analogRead(RPM_c); // Read coarse RPM
int RPS_C = map(RPM_C, 0, 4095, 0, 100); // Convert digital counts to coarse Rev/Sec
int RPM_F=analogRead(RPM_f); // Read fine RPM
int RPS_F = map(RPM_F, 0, 4095, 0, 5); // Convert digital counts to fine Rev/Sec
int RPS_T = RPS_C + RPS_F; // Add fine and coarse Rev/Sec' for a total Rev/Sec
int Tooth_Time = 1000000/((RPS_T*63)*2); // Convert total Rev/Sec to approximate tooth time in uSec (3175-79 uSec)
for (n = 0; n < 20; n++) // tooth signal for for 1st 120 degrees of crank rotation
{digitalWrite(Tooth_Out, HIGH);
delayMicroseconds(Tooth_Time); // delay for tooth
digitalWrite(Tooth_Out, LOW);
delayMicroseconds(Tooth_Time); // delay for gap between teeth
if (n == 8) {digitalWrite(Cam_Out, HIGH); t=micros(); Serial.println(t); Serial.println(Cam_Out);} // cam signal
delayMicroseconds(2*Tooth_Time); // delay for missing tooth
}
for (n = 0; n < 20; n++) // tooth signal for for 2nd 120 degrees of crank rotation
{digitalWrite(Tooth_Out, HIGH);
delayMicroseconds(Tooth_Time); // delay for tooth
digitalWrite(Tooth_Out, LOW);
delayMicroseconds(Tooth_Time); // delay for gap between teeth
delayMicroseconds(2*Tooth_Time); // delay for missing tooth
}
for (int n = 0; n < 20; n++) // tooth signal for for 3rd 120 degrees of crank rotation
{digitalWrite(Tooth_Out, HIGH);
delayMicroseconds(Tooth_Time); // delay for tooth
digitalWrite(Tooth_Out, LOW);
delayMicroseconds(Tooth_Time); // delay for gap between teeth
delayMicroseconds(2*Tooth_Time); // delay for missing tooth
}
for (int n = 0; n < 20; n++) // tooth signal for 4th 120 degrees of crank rotation
{digitalWrite(Tooth_Out, HIGH);
delayMicroseconds(Tooth_Time); // delay for tooth
digitalWrite(Tooth_Out, LOW);
delayMicroseconds(Tooth_Time); // delay for gap between teeth
if (n == 8) {digitalWrite(Cam_Out, LOW); t=micros() - t; Serial.println(t); Serial.println(Cam_Out); } // cam signal
delayMicroseconds(2*Tooth_Time); // delay for missing tooth
}
for (int n = 0; n < 20; n++); // tooth signal for for 5th 120 degrees of crank rotation
{digitalWrite(Tooth_Out, HIGH);
delayMicroseconds(Tooth_Time); // delay for tooth
digitalWrite(Tooth_Out, LOW);
delayMicroseconds(Tooth_Time); // delay for gap between teeth
delayMicroseconds(2*Tooth_Time); // delay for missing tooth
}
for (int n = 0; n < 20; n++) // tooth signal for for 6th 120 degrees of crank rotation
{digitalWrite(Tooth_Out, HIGH);
delayMicroseconds(Tooth_Time); // delay for tooth
digitalWrite(Tooth_Out, LOW);
delayMicroseconds(Tooth_Time); // delay for gap between teeth
delayMicroseconds(2*Tooth_Time); // delay for missing tooth
}
}