Facebook
From rezaul alam rafi, 4 Months ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 213
  1. #include <RunningStats.h>
  2.  
  3. #include <Filters.h> //Easy library to do the calculations
  4. #include <LiquidCrystal_I2C.h>
  5. LiquidCrystal_I2C lcd(0x27, 16, 2);
  6. float testFrequency = 50;  // test signal frequency (Hz)
  7. int Sensor = 0; //Sensor analog input, here it's A0
  8. int relay = 9;  //Define output pin for relay
  9. float intercept = 0.7; // To be adjusted based on calibration testing
  10. float slope = 0.04; // To be adjusted based on calibration testing
  11. float current_Volts; // Voltage
  12. unsigned long printPeriod = 1000; //Refresh rate
  13. unsigned long previousMillis = 0;
  14. void setup()
  15. {
  16.   lcd.init();
  17.   lcd.backlight();
  18.   pinMode(relay, OUTPUT);
  19.   lcd.print("Voltage:");
  20.   delay(1000);
  21. }
  22. void loop()
  23. {
  24.   RunningStats stats;                // Easy life lines, actual calculation of the RMS requires a load of coding
  25.   while ( true )
  26.   {
  27.     Sensor = analogRead(A0);  // Read the analog in value:
  28.    stats.update(Sensor);  // Log to stats function
  29.     if ((unsigned long)(millis() - previousMillis) >= printPeriod)
  30.     {
  31.       previousMillis = millis();   // Update time every second
  32.       current_Volts = intercept + slope * stats.std(); // Calibartions for offset and amplitude
  33.       current_Volts = current_Volts * (40.3231);             // Further calibrations for the amplitude
  34.       lcd.setCursor(9, 0);
  35.       lcd.print(current_Volts);
  36.      
  37.       lcd.print("V");
  38.     }
  39.     // Case 1 Under Voltage
  40.     if ( (current_Volts > 0)  &&  (current_Volts < 150) )
  41.   {
  42.     lcd.setCursor(0, 1);
  43.     lcd.print("Under Voltage");
  44.     digitalWrite(relay, LOW);
  45.   }
  46.   // Case 2 Normal Rated Voltage
  47.   if ( (current_Volts >= 150)  &&  (current_Volts <= 260) )
  48.   {
  49.     lcd.setCursor(0, 1);
  50.     lcd.print("Normal Voltage");
  51.     digitalWrite(relay, HIGH);
  52.   }
  53.   // Case 3 Over Voltage
  54.   if ( current_Volts > 260 )
  55.   {
  56.     lcd.setCursor(0, 1);
  57.     lcd.print("Over Voltage");
  58.     digitalWrite(relay, LOW);
  59.   }
  60.   }
  61. }