#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 totalCnt;
int CIState=B00000000;
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(PWus);
PORTD=PORTD^B01000000;
delayMicroseconds(FDCSoffset);
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 trying to generate 2 signals using the code above and this output is
From the image, the lime green signal is called FDCS and the other is called CI. Note that the scope shows the inverted form of the signal. I want the falling edge of CI to match with the Falling edge of the 5th FDCS signal (right now rising edge is matching with the falling edge of the CI signal). But the Rising edge of the CI signal should match with the Rising edge of the first FDCS signal (which it's already doing). How do I change my code to do this?