Code for high voltage fluctuation protection with on/off relay switch

I wrote my code to use my arduino uno as a voltmeter to measure a high voltage circuit. I want my code to monitor the voltage fluctuations in the circuit, trigger a 5V relay shut off switch and send an email alert. The problem I am having is getting the code to actually monitor both over voltage and under voltage and shut off the circuit. I can only can get it to monitor one or the other and neither send the email alert. Any help with this would be greatly appreciated. note I am using 3 colored LED’s to show the voltage range when it is high(50v),low(30v),normal(40v). The working code I have so far is attached below.

betatest1.ino (1.83 KB)

Welcome to the forum!

Your logic is not correct. Your very last if() statement is this

  if (analogvalue >= 1023) { // 5V
    digitalWrite(rled1, HIGH);
    digitalWrite(rled2, HIGH);
    delay(150);
  }
  else {       // 0V
    digitalWrite(gled1, LOW);
    digitalWrite(gled2, LOW);
    digitalWrite(yled1, LOW);
    digitalWrite(yled2, LOW);
    digitalWrite(rled1, LOW);
    digitalWrite(rled2, LOW);
  }

which will either set the red led (if analogvalue is 1023) or turn everything off.

You want to test for ranges of the voltage since your currently only test if the analogvalue is greater than some number. Something like this:

if ( analogvalue < 205 ) {
  // do whatever for 1V
} else if ( analogvalue < 410 ) {
  // do whatever for 2V
} else if ( analogvalue < 615 ) {
  // do whatever for 3V
} else if ....

I think you get the idea

I’d think like this

//float r1=1000000.0;    //r1 value    1M Ohm
//float r2=100000.0;      //r2 value  100 k Ohm  result: 1/11 =0.909091
/* 
 find the correct faktors by testing with a DMM ! adjust numbers
 50V to give read value og 930 
 40V -> 744
 30v -> 558
 20V -> 372
 
*/

float read_value,voltage,temp; 
int gled1 = 8;   //all LEDs are connected with 330Ω resistor
int gled2 = 7;  
int yled1 = 10; 
int yled2 = 9; 
int rled1 = 12; 
int rled2 = 11;

void setup()
{
   Serial.begin(9600);     //  opens serial port, sets data rate to 9600 bps
  // pinMode(analogvalue, INPUT);
   pinMode(gled1, OUTPUT);
   pinMode(gled2, OUTPUT);
   pinMode(yled2, OUTPUT);
   pinMode(yled2, OUTPUT);
   pinMode(rled1, OUTPUT);
   pinMode(rled2, OUTPUT);
}
void loop()
{
 	read_value = analogRead(A0);
    voltage=map(read_value,0,1023,0.0,55.0);
if (voltage <=2)   voltage=0.0; 
Serial.print("v= ");                 // prints the voltage value in the serial monitor
Serial.println(voltage);
if(voltage < 10){    // <<10V    digitalWrite(gled1, LOW);
     digitalWrite(gled2, LOW);
     digitalWrite(yled1, LOW);
     digitalWrite(yled2, LOW);
     digitalWrite(rled1, LOW);
     digitalWrite(rled2, LOW);
   }
else if(voltage < 20){    // <<20V
     digitalWrite(yled1, HIGH);
     digitalWrite(yled2, HIGH);
     delay(4);
   }
else if(voltage < 30){    // <<30V
     digitalWrite(yled1, HIGH);
     digitalWrite(yled2, HIGH);
     delay(4);
   }
 else  if(voltage < 40){    // <<40V 
     digitalWrite(yled1, HIGH);
     digitalWrite(yled2, HIGH);
     delay(4);
   }
 else  if(voltage < 50){    // 5<0V
     digitalWrite(gled1, HIGH);
     digitalWrite(gled2, HIGH);
     delay(4);
   }
 else  {   // >50V
     digitalWrite(rled1, HIGH);
     digitalWrite(rled2, HIGH);
     delay(150);
 
   }
}