You are on page 1of 3

1 #include <Filters.

h> //Easy library to do the calculations

2 #include <LiquidCrystal_I2C.h>

3 LiquidCrystal_I2C lcd(0x27, 16, 2);

4  

5 float testFrequency = 50;  // test signal frequency (Hz)

6  

7 int Sensor = 0; //Sensor analog input, here it's A0

8 int relay = 9;  //Define output pin for relay

9  

10 float intercept = 0.7; // To be adjusted based on calibration testing

11 float slope = 0.04; // To be adjusted based on calibration testing

12 float current_Volts; // Voltage

13  

14 unsigned long printPeriod = 1000; //Refresh rate

15 unsigned long previousMillis = 0;

16  

17  

18 void setup()

19 {

20   lcd.init();

21   lcd.backlight();

22   pinMode(relay, OUTPUT);

23   lcd.print("Voltage:");

24   delay(1000);

25  

26 }

27  

28 void loop()

29 {

30   RunningStatistics inputStats;                // Easy life lines, actual calculation of the RMS requires a load of coding

31  

32   while ( true )

33   {

34     Sensor = analogRead(A0);  // Read the analog in value:

35     inputStats.input(Sensor);  // Log to stats function


36  

37     if ((unsigned long)(millis() - previousMillis) >= printPeriod)

38     {

39       previousMillis = millis();   // Update time every second

40  

41       current_Volts = intercept + slope * inputStats.sigma(); // Calibartions for offset and amplitude

42       current_Volts = current_Volts * (40.3231);             // Further calibrations for the amplitude

43  

44       lcd.setCursor(9, 0);

45       lcd.print(current_Volts);

46       

47       lcd.print("V");

48     }

49     // Case 1 Under Voltage

50     if ( (current_Volts > 0)  &&  (current_Volts < 150) )

51   {

52     lcd.setCursor(0, 1);

53     lcd.print("Under Voltage");

54     digitalWrite(relay, LOW);

55   }

56   // Case 2 Normal Rated Voltage

57   if ( (current_Volts >= 150)  &&  (current_Volts <= 260) )

58   {

59     lcd.setCursor(0, 1);

60     lcd.print("Normal Voltage");

61     digitalWrite(relay, HIGH);

62   }

63   // Case 3 Over Voltage

64   if ( current_Volts > 260 )

65   {

66     lcd.setCursor(0, 1);

67     lcd.print("Over Voltage");

68     digitalWrite(relay, LOW);

69   }

70   }
71 }

You might also like