const int led1=6;
const int led2=7;
const int led3=8;
unsigned long previousMillis;
const unsigned long interval = 5000;
boolean enableDelay = false;
float sourceOvervoltage = 54.00; //Source Overvoltage
const int analogInput1 = A0; // voltage measurement pin
int value1 = 0;
float vout1 = 0.0;
float vSource = 0.0;
float R1a =100000;
float pot1 = 10000;
void setup()
{
Serial.begin(9600);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
}
void loop()
{
value1 = analogRead(analogInput1);
vout1 = (value1 * 5.0) / 1024;
vSource = vout1 / (pot1/(R1a+pot1));
unsigned long currentMillis = millis();
// Trigger the delay
if (vSource>=sourceOvervoltage){
enableDelay = true;
if (enableDelay){
//Serial.println("currentMillis - previousMillis");
//Serial.println(currentMillis - previousMillis);
if (currentMillis - previousMillis >= interval){
digitalWrite(led1, HIGH);
}
}
}
// Checking the millis delay should be done in the loop.
// This code is not inside the code to start the delay, but
// at the same level as other code in the loop.
//if (enableDelay){
if (vSource<=sourceOvervoltage){
enableDelay = false;
//previousMillis = currentMillis;
digitalWrite(led1, LOW);
}
//}
}