Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.83 KB | None | 0 0
  1. #include "EmonLib.h"
  2. #include <Arduino.h>
  3. #include <U8g2lib.h>
  4. #include <SPI.h>
  5. #include <Wire.h>
  6.  
  7. #define RELE1_PIN 8
  8. #define RELE2_PIN 9
  9. #define RELE3_PIN 10
  10. #define RELE4_PIN 11
  11.  
  12. // display
  13. U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0);
  14.  
  15. float proudy[4] = {0,0,0,0};
  16.  
  17. // LED warning light parameters setting
  18. float nocurrent = 0.1;    
  19. float lowcurrent = 0.12;  
  20. float highcurrent = 0.20;
  21.  
  22. // Include Emon Library
  23. EnergyMonitor emonitors[4];
  24.  
  25. void setup()
  26. {
  27.   Serial.begin(9600);  
  28.   while (!Serial){  
  29.   }
  30.  
  31.   Serial.println("SW VERZE 1.0");
  32.  
  33.   u8g2.begin();
  34.  
  35.   pinMode(RELE1_PIN, OUTPUT);       // red
  36.   pinMode(RELE2_PIN, OUTPUT);       //orange
  37.   pinMode(RELE3_PIN, OUTPUT);       //green
  38.   pinMode(RELE4_PIN, OUTPUT);       //buzzer
  39.  
  40.   for(int n=0; n<4; n++){
  41.     emonitors[n].current(n+A0,30);             // Current: input pin, calibration.
  42.   }
  43. }
  44.  
  45. void loop(){
  46.   for(int n=0; n<4; n++){
  47.     proudy[n] = emonitors[n].calcIrms(1674);  // Calculate Irms only (1674... n/50*5580.. orig 1480)
  48.   }
  49.  
  50.   String jsonData = "{\"I1\":";
  51.   jsonData += proudy[0];
  52.   jsonData += ",\"I2\":";
  53.   jsonData += proudy[1];
  54.   jsonData += ",\"I3\":";
  55.   jsonData += proudy[2];
  56.   jsonData += ",\"I4\":";
  57.   jsonData += proudy[3];
  58.   jsonData +=  "}";
  59.   Serial.println(jsonData);
  60.  
  61.   // u8g2.clearBuffer();          // clear the internal memory
  62.   // u8g2.setFont(u8g2_font_logisoso28_tr);  // choose a suitable font at https://github.com/olikraus/u8g2/wiki/fntlistall
  63.   // u8g2.drawStr(8,29,"Irms=");  // write something to the internal memory
  64.   // u8g2.sendBuffer();         // transfer internal memory to the display
  65.   //delay(1000);
  66.  
  67.   for(int n=0; n<4; n++){
  68.      u8g2.clearBuffer();          // clear the internal memory
  69.      u8g2.setFont(u8g2_font_logisoso22_tr);  // choose a suitable font at https://github.com/olikraus/u8g2/wiki/fntlistall
  70.      u8g2.setCursor(8, 29);
  71.      u8g2.print("K");
  72.      u8g2.print(n+1);
  73.      u8g2.print(":");
  74.      u8g2.print(proudy[n]);
  75.      u8g2.print(" A");
  76.      u8g2.sendBuffer();
  77.      delay (1000);    
  78.   }
  79.  
  80.   // LED warning light control
  81.   if ((proudy[0] < nocurrent) || (proudy[1] < nocurrent) || (proudy[2] < nocurrent) || (proudy[3] < nocurrent)){
  82.     NastavitVystupy(0,1,0,0);
  83.   }
  84.  
  85.   if ((lowcurrent > proudy[0] < highcurrent) || (lowcurrent > proudy[1] < highcurrent) || (lowcurrent > proudy[2] < highcurrent) || (lowcurrent > proudy[3] < highcurrent)){
  86.     NastavitVystupy(0,0,1,1);
  87.   }
  88.  
  89.   if ((proudy[0] > highcurrent) || (proudy[1] > highcurrent) || (proudy[2] > highcurrent) || (proudy[3] > highcurrent)){
  90.     NastavitVystupy(1,0,0,0);
  91.   }
  92.  
  93.   //delay(100);
  94. }
  95.  
  96. void NastavitVystupy(bool Out1, bool Out2, bool Out3, bool Out4){
  97.   digitalWrite(RELE1_PIN, Out1);
  98.   digitalWrite(RELE2_PIN, Out2);
  99.   digitalWrite(RELE3_PIN, Out3);
  100.   digitalWrite(RELE4_PIN, Out4);
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement