#define camshaft1 13
#define camshaft2 12
#define relay1 7
#define relay2 8
#define relay3 9
static int g1 = 0;
static int g2 = 0;
void setup() {
Serial.begin(115200);
pinMode(camshaft1, INPUT_PULLUP);
pinMode(camshaft2, INPUT_PULLUP);
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
}
void loop() {
static bool camshaft1_last_state = HIGH;
static bool camshaft2_last_state = HIGH;
bool camshaft1_current_state = digitalRead(camshaft1);
bool camshaft2_current_state = digitalRead(camshaft2);
if (camshaft1_last_state == HIGH && camshaft1_current_state == LOW) {
g1++;
}
if (camshaft2_last_state == HIGH && camshaft2_current_state == LOW) {
g2++;
}
camshaft1_last_state = camshaft1_current_state;
camshaft2_last_state = camshaft2_current_state;
int x2 = g1 + g2;
if (x2 > 4) {
g2 = 0;
g1 = 0;
x2 = 0;
}
if (x2 == 3) { // Copression
digitalWrite(relay2, LOW);
digitalWrite(relay3, LOW);
digitalWrite(relay1, LOW); }
else if (x2 == 1) { // Exhaust
digitalWrite(relay2, LOW);
digitalWrite(relay3, LOW);
digitalWrite(relay1, HIGH);
}
else if (x2 == 2) { // Intake
digitalWrite(relay2, HIGH);
digitalWrite(relay3, LOW);
digitalWrite(relay1, LOW);
}
else if (x2 == 4) { // Intake
digitalWrite(relay2, LOW);
digitalWrite(relay3, HIGH);
digitalWrite(relay1, LOW);
}
Serial.print("x2 :- ");
Serial.println(x2);
Serial.print("g2 :- ");
Serial.println(g2);
Serial.print("g1 :- ");
Serial.println(g1);
}