#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "digitalWriteFast.h"
#define BUTTONPIN 2
#define FDCSPIN 6
#define CIPIN 5
#define SPEAKERPIN 4
float RPM=1000.0;
int PWus=1.5*1000.0;
int FDCSoffset=(1000.0/RPM/60.0/4.0) - (PWus/1000.0);
int CiCnt;
int totalCnt;
int CIState=B01100000;
bool Buttonstate=0;
LiquidCrystal_I2C lcd(0x27, 20, 4);
void setup()
{
Serial.begin(9600);
lcd.begin();
lcd.backlight();
lcd.clear();
lcd.print("READY TO TEST"); // Waiting for button press
pinMode(BUTTONPIN, INPUT);// Setting button as Input
pinMode(FDCSPIN, OUTPUT); // Fuel Delivery Control Signal
pinMode(CIPIN, OUTPUT); // Cylinder Identification
digitalWriteFast(FDCSPIN, LOW);
digitalWriteFast(CIPIN, LOW);
}
void loop()
{
Buttonstate=digitalReadFast(BUTTONPIN);
if(Buttonstate==1)
{
delay(5);
if(Buttonstate==1)
{
runInjectorTest();
}
}
}
void runInjectorTest()
{
lcd.print("Test Run");
playtesttone();
for(totalCnt=0;totalCnt<8000;totalCnt++)
{
PORTD=CIState;
delayMicroseconds(FDCSoffset);
PORTD=PORTD^B01000000;
delayMicroseconds(PWus);
if(totalCnt%4==0)
{
CIState=CIState^B00100000;
}
}
totalCnt=0;
playfinishtone();
}
void playtesttone()
{
tone(SPEAKERPIN, 500, 500);
delay(700);
tone(SPEAKERPIN, 500, 500);
delay(700);
tone(SPEAKERPIN, 500, 500);
delay(700);
tone(SPEAKERPIN, 2000, 500);
}
void playfinishtone()
{
tone(SPEAKERPIN, 2000, 500);
delay(700);
tone(SPEAKERPIN, 2000, 500);
delay(700);
tone(SPEAKERPIN, 2000, 500);
delay(700);
tone(SPEAKERPIN, 500, 500);
}
I am using this code to generate 2 signals, called CI and FDCS. CI stays on until the 5th FDCS pulse. I checked the output which is connected to a driver using the scope and I got this.
The rising edge and falling edge of the longer signal (CI, dark green) are supposed to sync with the rising edge of the FDCS signal (Lime green),but they are inline with the falling edge. Note that the output is inverted (when compared to the code) hence the signal generated by the Arduino pins is according to the code.