const int analogInputPin = A0;
// Define voltage thresholds
const float voltageThreshold1 = 2.5;
const float voltageThreshold2 = 4;
// Define LED output pins
const int ledPin1 = 2; // green led
const int ledPin2 = 3; // red led
const int ledPin3 = 4; // relay
const int ledPin4 = 5; // error
boolean status_count = false; // detect the switching
int count = 0; // just a counter
void turnon(){
digitalWrite(ledPin1, HIGH); // Turn on green led
digitalWrite(ledPin2, LOW); // Turn off red led
digitalWrite(ledPin3, LOW); // Turn off relay
}
void turnoff(){
digitalWrite(ledPin1, LOW); // Turn off green led
digitalWrite(ledPin2, HIGH); // Turn on red led
digitalWrite(ledPin3, HIGH); // Turn on relay
}
void setup() {
Serial.begin(9600); // Initialize serial communication
pinMode(analogInputPin, INPUT); // Set the analog pin as input
pinMode(ledPin1, OUTPUT); // Set the green led pin as output
pinMode(ledPin2, OUTPUT); // Set the red led
pinMode(ledPin3, OUTPUT); // Set the relay
pinMode(ledPin4, OUTPUT); // error
turnon();
}
void loop() {
for (int i = 0 ; i < 180 ; i++){
delay(1000); // Wait for a second before the next reading
int sensorValue = analogRead(analogInputPin); // Read the analog input value
float voltage = sensorValue * (5.0 / 1023.0); // Convert the value to voltage
Serial.print(voltage);Serial.print(" / ");Serial.println(count);
if (voltage < voltageThreshold1) {
if (status_count == false) { count = count + 1; status_count =true ;}
turnoff();}
if (voltage > voltageThreshold2) {
status_count =false ;
turnon(); }
if (count >2){
digitalWrite(ledPin4, HIGH);
Serial.println("waiting");
while(1){}
}
} if (count <3){ count = 0;} // if nothing happen after 3 mins
}