Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include <MenuBackend.h>
- #include <LiquidTWI2.h>
- #include "EmonLib.h"
- // Pin analogico sensore corrente
- const int currentSensorPin = A1;
- // Calibrazion1 sensore di corrente
- const int volt = 230;
- const float ct_calibration = 3.33;
- float Irms = 0;
- // Create an Emon instance
- EnergyMonitor emon1;
- LiquidTWI2 lcd(0);
- // valori utilizzati dai pulsanti
- #define btnNONE 0
- #define btnRIGHT 1
- #define btnUP 2
- #define btnDOWN 3
- #define btnLEFT 4
- #define btnSELECT 5
- #define buttonPin A0
- // timeout per lo spegnimento dello schermo ("cablaggio" da completare su millefori)
- unsigned long timeoutTime = 0; // this is set and compared to millis to see when the user last did something.
- const int menuTimeout = 5 * 1000; // time to timeout in a menu when user doesn't do anything.
- byte myBackLight; // set because user is still pushing buttons.
- // LCD constants
- #define ON 0x4 // lcd backlight (ovvero LCD GND + POT GND) collegata al BLU della adafruit shield (MCP23017 pin 1 - GPB0)
- #define OFF 0x0
- #define FIRST_LINE 0 //text position for first line
- #define SECOND_LINE 1 //text position for second line
- #define THIRD_LINE 2 //text position for third line
- #define FOURTH_LINE 3 //text position for fourth line
- // A global flag. true = root (top) menu and display clock. false = don't display clock
- byte menuRoot = true;
- /****************************************************************************
- * Definizioni dei menu
- ****************************************************************************/
- MenuBackend menu = MenuBackend( menuUsed, menuChanged);
- MenuItem menu1 = MenuItem("menu1"); //
- MenuItem menu2 = MenuItem("menu2");
- MenuItem menu3 = MenuItem("menu3");
- void setup() {
- // put your setup code here, to run once:
- Wire.begin();
- lcd.setMCPType(LTI_TYPE_MCP23017);
- lcd.begin(20, 4);
- lcd.clear();
- menu.getRoot().add(menu1);
- menu1.add(menu2);
- menu2.add(menu3);
- menu3.addAfter(menu1);
- //initialize Emon instance
- emon1.current(currentSensorPin, ct_calibration);
- }
- void loop() {
- // put your main code here, to run repeatedly:
- // leggo i pulsanti
- uint8_t buttons = read_LCD_buttons();
- navigateMenus(); // funzione di navigazione nei menu
- }
- // navigazione nei menu tramite pulsanti
- //-------------------------------------------------------------------------------------------------------------
- void navigateMenus() {
- MenuItem currentMenu = menu.getCurrent();
- uint8_t buttons = read_LCD_buttons();
- switch (buttons) {
- case btnLEFT:
- delay(500);
- menu.moveLeft();
- timeoutTime = millis() + menuTimeout; // reset timeout timer
- break;
- case btnRIGHT:
- delay(500);
- menu.moveRight();
- timeoutTime = millis() + menuTimeout; // reset timeout timer
- break;
- case btnDOWN:
- delay(500);
- menu.moveDown();
- timeoutTime = millis() + menuTimeout; // reset timeout timer
- break;
- case btnUP:
- delay(500);
- menu.moveUp();
- timeoutTime = millis() + menuTimeout; // reset timeout timer
- break;
- case btnSELECT:
- //delay(500);
- //menu.use();
- switch (myBackLight) {
- case 0:
- lcd.setBacklight(ON);
- break;
- case 1:
- delay(500);
- menu.use();
- break;
- }
- myBackLight = 1;
- timeoutTime = millis() + menuTimeout; // reset timeout timer
- break;
- }
- if (timeoutTime < millis()) { // user hasn't done anything in awhile
- myBackLight = 0; // tell loop to bail out.
- lcd.setBacklight(OFF);
- menu.toRoot();
- defaultScreen();
- }
- }
- //-------------------------------------------------------------------------------------------------------------
- // navigazione nei menu tramite pulsanti
- void menuChanged(MenuChangeEvent changed) {
- MenuItem newMenuItem = changed.to; // ottiene il menu di destinazione
- lcd.setCursor(0, FIRST_LINE);
- if (newMenuItem.getName() == "menu1") {
- menuRoot = false;
- lcd.clear();
- lcd.print(F("sei in menu1"));
- }
- else if (newMenuItem.getName() == "menu2") {
- menuRoot = false;
- lcd.clear();
- lcd.print(F("sei in menu2"));
- current_sensor();
- }
- else if (newMenuItem.getName() == "menu3") {
- menuRoot = false;
- lcd.clear();
- lcd.print(F("sei in menu3"));
- lcd.setCursor(0, SECOND_LINE);
- lcd.print(millis() / 1000);
- }
- button_input();
- }
- void menuUsed(MenuUseEvent used) {
- // funzione vuota, non bisogna selezionare nessun menu
- }
- void defaultScreen() { //
- menuRoot = true;
- //lcd.clear();
- lcd.setCursor(0, FIRST_LINE);
- lcd.print("MENU LCD CON");
- lcd.setCursor(0, SECOND_LINE);
- lcd.print("SENSORE DI CORRENTE");
- delay(1000);
- }
- //////////////////////////////////////////////////////////////////////////
- // button input
- //////////////////////////////////////////////////////////////////////////
- void button_input() {
- byte lcd_key = key_press(); // read the buttons
- /*
- switch (lcd_key) {
- case btnRIGHT:
- lcd.clear();
- lcd.print("Set Date");
- break;
- case btnLEFT:
- lcd.clear();
- lcd.print("Set Time");
- break;
- }
- */
- }
- //////////////////////////////////////////////////////////////////////////
- // multiple buttons on 1 analog pin
- //////////////////////////////////////////////////////////////////////////
- byte read_LCD_buttons() { // read the buttons
- int adc_key_in = analogRead(buttonPin); // read the value from the pin
- if (adc_key_in == 0) return btnNONE;
- if (adc_key_in > 290 && adc_key_in < 330) return btnUP;
- if (adc_key_in > 130 && adc_key_in < 180) return btnDOWN;
- if (adc_key_in > 50 && adc_key_in < 100) return btnLEFT;
- if (adc_key_in > 820 && adc_key_in < 950) return btnRIGHT;
- if (adc_key_in > 440 && adc_key_in < 590) return btnSELECT;
- return btnNONE;
- }
- //////////////////////////////////////////////////////////////////////////
- // debounce a button
- //////////////////////////////////////////////////////////////////////////
- int counter = 0; // how many times we have seen new value
- long previous_time = 0; // the last time the output pin was sampled
- byte debounce_count = 10; // number of millis/samples to consider before declaring a debounced input
- byte current_state = 0; // the debounced input value
- byte key_press() {
- // If we have gone on to the next millisecond
- if (millis() != previous_time)
- {
- byte this_button = read_LCD_buttons();
- if (this_button == current_state && counter > 0) counter--;
- if (this_button != current_state) counter++;
- // If the Input has shown the same value for long enough let's switch it
- if (counter >= debounce_count) {
- counter = 0;
- current_state = this_button;
- return this_button;
- }
- previous_time = millis();
- }
- return 0;
- }
- unsigned long previousMillisTest = 0;
- unsigned long intervalTest = 1000; //intervallo operazione da eseguire
- // FUNZIONE PER VISUALIZZARE IL CONSUMO DI CORRENTE NEL MENU3
- void current_sensor() {
- Irms = emon1.calcIrms(1480);
- if ((Irms * volt) < 5) {
- lcd.clear();
- lcd.print("NESSUN CARICO");
- }
- else {
- //lcd.clear();
- float watt;
- float ampere;
- // leggo il valore per 10 volte, poi faccio la media
- for (int ciclo = 0; ciclo < 14; ciclo++) {
- watt += (Irms * volt);
- ampere += (Irms * volt) / volt;
- }
- watt /= 14;
- ampere /= 14;
- lcd.setCursor(0, SECOND_LINE);
- lcd.print("Watt: ");
- lcd.print(watt);
- //lcd.print((Irms*volt));
- lcd.setCursor(0, THIRD_LINE);
- lcd.print("Ampere: ");
- //lcd.print((Irms*volt)/volt);
- lcd.print(ampere);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement