Advertisement
safwan092

Untitled

Oct 3rd, 2023
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. #include <Filters.h> //Easy library to do the calculations
  2. #include <LiquidCrystal_I2C.h>
  3. LiquidCrystal_I2C lcd(0x27, 16, 2);
  4. float testFrequency = 50; // test signal frequency (Hz)
  5. int Sensor = 0; //Sensor analog input, here it's A0
  6. int relay = 9; //Define output pin for relay
  7. float intercept = 0.7; // To be adjusted based on calibration testing
  8. float slope = 0.04; // To be adjusted based on calibration testing
  9. float current_Volts; // Voltage
  10. unsigned long printPeriod = 1000; //Refresh rate
  11. unsigned long previousMillis = 0;
  12. void setup()
  13. {
  14. lcd.init();
  15. lcd.backlight();
  16. pinMode(relay, OUTPUT);
  17. lcd.print("Voltage:");
  18. delay(1000);
  19. }
  20. void loop()
  21. {
  22. RunningStatistics inputStats; // Easy life lines, actual calculation of the RMS requires a load of coding
  23. while ( true )
  24. {
  25. Sensor = analogRead(A0); // Read the analog in value:
  26. inputStats.input(Sensor); // Log to stats function
  27. if ((unsigned long)(millis() - previousMillis) >= printPeriod)
  28. {
  29. previousMillis = millis(); // Update time every second
  30. current_Volts = intercept + slope * inputStats.sigma(); // Calibartions for offset and amplitude
  31. current_Volts = current_Volts * (40.3231); // Further calibrations for the amplitude
  32. lcd.setCursor(9, 0);
  33. lcd.print(current_Volts);
  34.  
  35. lcd.print("V");
  36. }
  37. // Case 1 Under Voltage
  38. if ( (current_Volts > 0) && (current_Volts < 150) )
  39. {
  40. lcd.setCursor(0, 1);
  41. lcd.print("Under Voltage");
  42. digitalWrite(relay, LOW);
  43. }
  44. // Case 2 Normal Rated Voltage
  45. if ( (current_Volts >= 150) && (current_Volts <= 180) )
  46. {
  47. lcd.setCursor(0, 1);
  48. lcd.print("Normal Voltage");
  49. digitalWrite(relay, HIGH);
  50. }
  51. // Case 3 Over Voltage
  52. if ( current_Volts > 180 )
  53. {
  54. lcd.setCursor(0, 1);
  55. lcd.print("Over Voltage");
  56. digitalWrite(relay, LOW);
  57. }
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement