Advertisement
subnet

Arduino, MenuBackend ed EmonLib

Mar 15th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.79 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <MenuBackend.h>
  3. #include <LiquidTWI2.h>
  4. #include "EmonLib.h"
  5.  
  6. // Pin analogico sensore corrente
  7. const int currentSensorPin = A1;
  8.  
  9. // Calibrazion1 sensore di corrente
  10. const int volt = 230;
  11. const float ct_calibration = 3.33;
  12. float Irms = 0;
  13.  
  14. // Create an Emon instance
  15. EnergyMonitor emon1;
  16.  
  17. LiquidTWI2 lcd(0);
  18.  
  19. // valori utilizzati dai pulsanti
  20. #define btnNONE   0
  21. #define btnRIGHT  1
  22. #define btnUP     2
  23. #define btnDOWN   3
  24. #define btnLEFT   4
  25. #define btnSELECT 5
  26. #define buttonPin A0
  27.  
  28. // timeout per lo spegnimento dello schermo ("cablaggio" da completare su millefori)
  29. unsigned long timeoutTime = 0;  // this is set and compared to millis to see when the user last did something.
  30. const int menuTimeout = 5 * 1000; // time to timeout in a menu when user doesn't do anything.
  31. byte myBackLight;  // set because user is still pushing buttons.
  32.  
  33.  
  34. // LCD constants
  35. #define ON 0x4 // lcd backlight (ovvero LCD GND + POT GND) collegata al BLU della adafruit shield (MCP23017 pin 1 - GPB0)
  36. #define OFF 0x0
  37. #define FIRST_LINE 0 //text position for first line
  38. #define SECOND_LINE 1 //text position for second line
  39. #define THIRD_LINE 2 //text position for third line
  40. #define FOURTH_LINE 3 //text position for fourth line
  41.  
  42. // A global flag. true = root (top) menu and display clock. false = don't display clock
  43. byte menuRoot = true;
  44.  
  45. /****************************************************************************
  46.  * Definizioni dei menu
  47.  ****************************************************************************/
  48. MenuBackend menu = MenuBackend( menuUsed, menuChanged);
  49.  
  50. MenuItem menu1 = MenuItem("menu1"); //
  51. MenuItem menu2 = MenuItem("menu2");
  52. MenuItem menu3 = MenuItem("menu3");
  53.  
  54. void setup() {
  55.   // put your setup code here, to run once:
  56.   Wire.begin();
  57.   lcd.setMCPType(LTI_TYPE_MCP23017);
  58.   lcd.begin(20, 4);
  59.   lcd.clear();
  60.   menu.getRoot().add(menu1);
  61.   menu1.add(menu2);
  62.   menu2.add(menu3);
  63.   menu3.addAfter(menu1);
  64.  
  65.   //initialize Emon instance
  66.   emon1.current(currentSensorPin, ct_calibration);
  67.  
  68. }
  69.  
  70. void loop() {
  71.   // put your main code here, to run repeatedly:
  72.  
  73.   // leggo i pulsanti
  74.   uint8_t buttons = read_LCD_buttons();
  75.   navigateMenus();  // funzione di navigazione nei menu
  76. }
  77.  
  78. // navigazione nei menu tramite pulsanti
  79. //-------------------------------------------------------------------------------------------------------------
  80.  
  81. void navigateMenus() {
  82.   MenuItem currentMenu = menu.getCurrent();
  83.  
  84.   uint8_t buttons = read_LCD_buttons();
  85.  
  86.   switch (buttons) {
  87.  
  88.     case btnLEFT:
  89.       delay(500);
  90.       menu.moveLeft();
  91.       timeoutTime = millis() + menuTimeout; // reset timeout timer
  92.       break;
  93.  
  94.     case btnRIGHT:
  95.       delay(500);
  96.       menu.moveRight();
  97.       timeoutTime = millis() + menuTimeout; // reset timeout timer
  98.       break;
  99.  
  100.     case btnDOWN:
  101.       delay(500);
  102.       menu.moveDown();
  103.       timeoutTime = millis() + menuTimeout; // reset timeout timer
  104.       break;
  105.  
  106.     case btnUP:
  107.       delay(500);
  108.       menu.moveUp();
  109.       timeoutTime = millis() + menuTimeout; // reset timeout timer
  110.       break;
  111.  
  112.     case btnSELECT:
  113.       //delay(500);
  114.       //menu.use();
  115.       switch (myBackLight) {
  116.  
  117.         case 0:
  118.           lcd.setBacklight(ON);
  119.           break;
  120.  
  121.         case 1:
  122.           delay(500);  
  123.           menu.use();
  124.           break;
  125.  
  126.       }
  127.       myBackLight = 1;
  128.       timeoutTime = millis() + menuTimeout; // reset timeout timer
  129.       break;
  130.   }
  131.  
  132.   if (timeoutTime < millis()) { // user hasn't done anything in awhile
  133.     myBackLight = 0;  // tell loop to bail out.
  134.     lcd.setBacklight(OFF);
  135.     menu.toRoot();
  136.     defaultScreen();    
  137.   }
  138. }
  139. //-------------------------------------------------------------------------------------------------------------
  140. // navigazione nei menu tramite pulsanti
  141.  
  142.  
  143. void menuChanged(MenuChangeEvent changed) {
  144.  
  145.   MenuItem newMenuItem = changed.to; // ottiene il menu di destinazione
  146.  
  147.   lcd.setCursor(0, FIRST_LINE);
  148.  
  149.   if (newMenuItem.getName() == "menu1") {
  150.     menuRoot = false;
  151.     lcd.clear();
  152.     lcd.print(F("sei in menu1"));
  153.   }
  154.  
  155.   else if (newMenuItem.getName() == "menu2") {
  156.     menuRoot = false;
  157.     lcd.clear();
  158.     lcd.print(F("sei in menu2"));
  159.     current_sensor();
  160.  
  161.   }
  162.  
  163.   else if (newMenuItem.getName() == "menu3") {
  164.     menuRoot = false;
  165.     lcd.clear();
  166.     lcd.print(F("sei in menu3"));
  167.     lcd.setCursor(0, SECOND_LINE);
  168.     lcd.print(millis() / 1000);
  169.   }
  170.   button_input();
  171. }
  172.  
  173. void menuUsed(MenuUseEvent used) {
  174. // funzione vuota, non bisogna selezionare nessun menu
  175. }
  176.  
  177.  
  178. void defaultScreen() { //
  179.   menuRoot = true;
  180.   //lcd.clear();
  181.   lcd.setCursor(0, FIRST_LINE);
  182.   lcd.print("MENU LCD CON");
  183.   lcd.setCursor(0, SECOND_LINE);
  184.   lcd.print("SENSORE DI CORRENTE");
  185.   delay(1000);
  186. }
  187.  
  188.  
  189. //////////////////////////////////////////////////////////////////////////
  190. // button input
  191. //////////////////////////////////////////////////////////////////////////
  192. void button_input() {
  193.   byte lcd_key = key_press();   // read the buttons
  194.   /*
  195.   switch (lcd_key) {
  196.     case btnRIGHT:
  197.  
  198.       lcd.clear();
  199.       lcd.print("Set Date");
  200.  
  201.       break;
  202.     case btnLEFT:
  203.  
  204.       lcd.clear();
  205.       lcd.print("Set Time");
  206.  
  207.       break;
  208.   }
  209.   */
  210. }
  211.  
  212. //////////////////////////////////////////////////////////////////////////
  213. //  multiple buttons on 1 analog pin
  214. //////////////////////////////////////////////////////////////////////////
  215. byte read_LCD_buttons() {              // read the buttons
  216.   int adc_key_in = analogRead(buttonPin);       // read the value from the pin
  217.  
  218.   if (adc_key_in == 0) return btnNONE;
  219.   if (adc_key_in > 290 && adc_key_in < 330)  return btnUP;
  220.   if (adc_key_in > 130 && adc_key_in < 180)  return btnDOWN;
  221.   if (adc_key_in > 50 && adc_key_in < 100)  return btnLEFT;
  222.   if (adc_key_in > 820 && adc_key_in < 950)   return btnRIGHT;
  223.   if (adc_key_in > 440 && adc_key_in < 590)  return btnSELECT;
  224.   return btnNONE;
  225. }
  226.  
  227. //////////////////////////////////////////////////////////////////////////
  228. // debounce a button
  229. //////////////////////////////////////////////////////////////////////////
  230. int counter = 0;       // how many times we have seen new value
  231. long previous_time = 0;    // the last time the output pin was sampled
  232. byte debounce_count = 10; // number of millis/samples to consider before declaring a debounced input
  233. byte current_state = 0;   // the debounced input value
  234.  
  235. byte  key_press() {
  236.   // If we have gone on to the next millisecond
  237.   if (millis() != previous_time)
  238.   {
  239.     byte this_button = read_LCD_buttons();
  240.  
  241.     if (this_button == current_state && counter > 0) counter--;
  242.  
  243.     if (this_button != current_state) counter++;
  244.  
  245.     // If the Input has shown the same value for long enough let's switch it
  246.     if (counter >= debounce_count) {
  247.       counter = 0;
  248.       current_state = this_button;
  249.       return this_button;
  250.     }
  251.     previous_time = millis();
  252.   }
  253.   return 0;
  254. }
  255. unsigned long previousMillisTest = 0;
  256. unsigned long intervalTest = 1000;  //intervallo operazione da eseguire
  257.  
  258. // FUNZIONE PER VISUALIZZARE IL CONSUMO DI CORRENTE NEL MENU3
  259. void current_sensor() {
  260.  
  261.  
  262.     Irms = emon1.calcIrms(1480);
  263.  
  264.     if ((Irms * volt) < 5) {
  265.       lcd.clear();
  266.       lcd.print("NESSUN CARICO");
  267.  
  268.     }
  269.     else {
  270.       //lcd.clear();
  271.       float watt;
  272.       float ampere;
  273.  
  274.       // leggo il valore per 10 volte, poi faccio la media
  275.       for (int ciclo = 0; ciclo < 14; ciclo++) {
  276.         watt += (Irms * volt);
  277.         ampere += (Irms * volt) / volt;
  278.       }
  279.  
  280.       watt /= 14;
  281.       ampere /= 14;
  282.  
  283.       lcd.setCursor(0, SECOND_LINE);
  284.       lcd.print("Watt: ");
  285.       lcd.print(watt);
  286.       //lcd.print((Irms*volt));
  287.       lcd.setCursor(0, THIRD_LINE);
  288.       lcd.print("Ampere: ");
  289.       //lcd.print((Irms*volt)/volt);
  290.       lcd.print(ampere);
  291.      
  292.  
  293.     }
  294.  
  295. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement