Hello,
I have just picked up an Arduino nano for a project. So i am a complete newbie to arduino, and even programming.
I want to interpret the signal pattern attached in this image.
As you can see the sensor has two signals, the "ident" signal is occuring once in a revolution and the "cyl" signal occurs 6 times in a revolution.
"ident" is the TDC of #1 cylinder.
"cyl" is the TDC of all 6 cylinders.

The goal of the project would be to use the arduino to drive a wasted spark coil pack instead of the distributor. (which is generating theese signals via hall sensors and vanes)
The distributor would remain connected to the ECU as is, thus the ECU can function as usual, and output an ignition trigger signal. ( timing the spark event and coil dwell)
The engine needs 3 wasted spark coil packs, each firing two cylinders. (1-4, 5-2,, 3-6)
I could drive the coil packs with the original trigger signal from the ECU and a cylinder pairs signal from the arduino.
For example:
each coil pack has a CMOS AND gate driving it. A input of all the gates is the ECU trigger signal.
B input is an output from arduino.
The ECU knows which cylinder needs to be ignited, outputs the ignition trigger signal.
The arduino knows also which cylinder is at TDC, for example its cylinder 5 at the moment. Arduino outputs a digital signal drivng the AND gate for 5-2 cylinders coil.
I have written this code and tested it. It works, sort of.
It needs a synchnorization bit to avoid sending the wrong output if its not certain which cylinder is next. I have tried several ideas that didnt work(commented in the program) , then came up with the last one which did seem to work.
Then i have modified something in the program, now the sync variable never goes to 1, only if i reset the board, then it goes to 1 for 3 pulses.
I hve tried debugging this for hours with no avail.
Please advise. If you have another ideas for implementing this "camshaft decoder", i am open for it.
code:
const int ident = 6; //pin 6 = cylinder identification input
const int cyl= 7; //pin 7 = cylinder strobe input
const int cyl14 = 2; //pin 2 = 1-4 cylinder output
const int cyl52 = 3; //pin 3 = 5-2 cylinder output
const int cyl36= 4; //pin 4 = 6-3 cylinder output
int identState = 0;
int cylState = 0;
int LastIdentState =0;
int LastCylState = 0;
int sync = 0;
int count = 0;
bool identhigh;
bool cylhigh;
int counterCyl = 0;
int counterIdent = 0;
int counterIdentHigh = 0;
int counterIdentLow = 0;
void setup() {
//define IO pins
pinMode(cyl14, OUTPUT);
pinMode(cyl52, OUTPUT);
pinMode(cyl36, OUTPUT);
pinMode(ident, INPUT_PULLUP);
pinMode(cyl, INPUT_PULLUP);
Serial.begin(2000000); //9600 baud made the program run slow, lost sync at about 1500rpm
}
void loop() {
//read inputs
identState = digitalRead(ident);
cylState = digitalRead(cyl);
//detect falling and raising edges
if (identState != LastIdentState) {
// if the state has changed, increment the counter
if (identState == HIGH) {
// if the current state is HIGH then the vane is in front of the hall sensor
counterIdent++;
identhigh=1;
counterCyl=0;
/*Serial.print("number of revolutions: "); //the ident input generates 1 pulse/rpm
Serial.println(counterIdent);
*/
} else {
// if the current state is LOW then the vane is NOT in front of the hall sensor
identhigh=0;
counterCyl=3;
}
}
LastIdentState = identState; //save last known state
if (cylState != LastCylState) {
// if the state has changed, increment the counter
if (cylState == HIGH) {
// if the current state is HIGH then the vane is in front of the hall sensor
counterCyl++;
cylhigh = 1;
Serial.print("no of current cylinder: "); ///serial printing is only used for debugging prposes
Serial.println(counterCyl);
Serial.print("sync: ");
Serial.println(sync);
Serial.print("identhighcount: ");
Serial.println(counterIdentHigh);
Serial.print("identlowcount: ");
Serial.println(counterIdentLow);
Serial.println(counterCyl - counterIdentHigh);
Serial.println(counterCyl - counterIdentLow);
if (LastIdentState == HIGH) {
counterIdentHigh++;
}else {
counterIdentHigh = 1;
}
if (LastIdentState == LOW) {
counterIdentLow++;
}else {
counterIdentLow = 4;
}
}
else {
// if the current state is LOW then the vane is NOT in front of the hall sensor
cylhigh = 0;
}
}
LastCylState = cylState; //save last known state
//sync
if ((counterCyl - counterIdentHigh == 0) || (counterCyl - counterIdentLow == 0)){
sync = 1;
}
else {
sync = 0;
}
/*if ((counterCyl >= 1 && counterCyl <= 3 && identhigh == 1) | (counterCyl <= 6 && counterCyl >= 3 && identhigh == 0 ) ){
sync = 1;
} else {
sync = 0;
}
*/
/*if (counterCyl >6){
counterCyl = 0;
}*/
/*if (counterCyl <= 6 && counterCyl >= 4 && identhigh == 0 ){
sync = 1;
} else{
sync = 0;
} */
/*if (counterCyl > 5 && identhigh == 1 && cylhigh == 0){
counterCyl = 0;
}
*/
//writing outputs depending on where the counter sits and sync is ok
if ((counterCyl == 1 || counterCyl == 4) && sync == 1 ){
digitalWrite(cyl14, HIGH);
}
else {
digitalWrite(cyl14, LOW);
}
if ((counterCyl == 5 || counterCyl == 2) && sync == 1 ){
digitalWrite(cyl52, HIGH);
}
else {
digitalWrite(cyl52, LOW);
}
if ((counterCyl == 3 || counterCyl == 6) && sync == 1 ){
digitalWrite(cyl36, HIGH);
}
else {
digitalWrite(cyl36, LOW);
}
//end of loop
}
