#define relay1 2
#define relay2 3
int openTime = 0;
int closeTime = 0;
int pulseCount = 0;
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
int f = 0;
int pulse = 4;
int rst = 5;
int pb = 0;
int reset = 0;
signed long countera = 0;
signed long counterb = 0;
signed long Open_Current_Millis = 0;
signed long Close_Current_Millis = 0;
void setup() {
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(pulse, INPUT_PULLUP);
pinMode(rst, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
pb = digitalRead(pulse);
reset = digitalRead(rst);
if (pb == LOW and a == 0){
countera = millis();
a = 1;
pulseCount = countera - counterb;
}
if (a == 1 and pb == HIGH and b == 0) b = 1;
if (pb == LOW and a == 1 and b == 1 and c == 0){
counterb = millis();
c = 1;
pulseCount = counterb - countera;
}
if (pb == HIGH and c ==1 and b == 1 and a == 1) a = 0, b = 0, c = 0;
if(pulseCount < 0) pulseCount = 0;
openTime = pulseCount / 2.5;
closeTime = pulseCount / 2;
Serial.print("Open Time = ");
Serial.print(openTime);
Serial.print(" Close Time = ");
Serial.print(closeTime);
Serial.print(" Pulsecount = ");
Serial.print(pulseCount);
Serial.print(" D = ");
Serial.println(d);
// Activating the Relays
if (pb == LOW) digitalWrite(relay1, HIGH), Open_Current_Millis = millis(), d = 1;
if (millis() - Open_Current_Millis > openTime and d == 1){
digitalWrite(relay1, LOW);
Close_Current_Millis = millis();
digitalWrite(relay2, HIGH);
d = 0;
}
if (millis() - Close_Current_Millis > closeTime) digitalWrite(relay2, LOW);
}