Codigo principal

codigo que mostra o menu seletivo
mail@pastecode.io avatar
unknown
c_cpp
a year ago
4.4 kB
2
Indexable
Never
#include <Wire.h>
#include<LiquidCrystal_I2C_Hangul.h>
#define BUTTON_PIN_1 16  // ESP32 pin GIOP16, which connected to button
#define BUTTON_PIN_2 15
#define BUTTON_PIN_3 18
#define BUTTON_PIN_4 17

LiquidCrystal_I2C_Hangul lcd(0x3F,16,2); //LCD 클래스 초기화

int button_state1;       // the current state of button
int last_button_state1;  // the previous state of button
int button_state2;       // the current state of button
int last_button_state2;
int button_state3;       // the current state of button
int last_button_state3;
int button_state4;       // the current state of button
int last_button_state4;
int dia = 1;
int mes = 1;
int ano = 2023;


int selectedOption = 1; // Opção inicial selecionada (pode ser 1, 2 ou 3)

void setup() {
  Serial.begin(9600);                // initialize serial
  pinMode(BUTTON_PIN_1, INPUT_PULLUP); // set ESP32 pin to input pull-up mode
  button_state1 = digitalRead(BUTTON_PIN_1);
  pinMode(BUTTON_PIN_2, INPUT_PULLUP); // set ESP32 pin to input pull-up mode
  button_state2 = digitalRead(BUTTON_PIN_2);
  pinMode(BUTTON_PIN_3, INPUT_PULLUP); // set ESP32 pin to input pull-up mode
  button_state3 = digitalRead(BUTTON_PIN_3);
  pinMode(BUTTON_PIN_4, INPUT_PULLUP); // set ESP32 pin to input pull-up mode
  button_state4 = digitalRead(BUTTON_PIN_4);
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("Fluxo");
  lcd.setCursor(0,1);
  lcd.print("Abastecimento");
}

void loop() {
  last_button_state1 = button_state1;      // save the last state
  button_state1 = digitalRead(BUTTON_PIN_1); // read new state
  last_button_state2 = button_state2;      // save the last state
  button_state2 = digitalRead(BUTTON_PIN_2); 
  last_button_state3 = button_state3;      // save the last state
  button_state3 = digitalRead(BUTTON_PIN_3);
  last_button_state4 = button_state4;      // save the last state
  button_state4 = digitalRead(BUTTON_PIN_4);
  
  // Atualiza a opção selecionada de acordo com os botões
  
  if(last_button_state1 == HIGH && button_state1 == LOW){
    selectedOption = 1;
    drawMenu(selectedOption);
  }
  else if (last_button_state2 == HIGH && button_state2 == LOW){
    selectedOption = 2;
    drawMenu(selectedOption);
  }
  else if (last_button_state3 == HIGH && button_state3 == LOW) {
    selectedOption = 3;
    drawMenu(selectedOption);
  }
  else if (last_button_state4 == HIGH && button_state4 == LOW){
    if(selectedOption == 1) {
      lcd.setCursor(0,0);
      lcd.print("menu 1         ");
      lcd.setCursor(0,1);
      lcd.print("                ");
      }
    else if(selectedOption == 2){
      lcd.setCursor(0,0);
      lcd.print("menu 2         ");
      lcd.setCursor(0,1);
      lcd.print("                ");
    }
    else if(selectedOption == 3){
      lcd.setCursor(0,0);
      lcd.print("menu 3         ");
      lcd.setCursor(0,1);
      lcd.print("                ");
    }
  }
}

void drawMenu(int selectedOption) {
  if (selectedOption == 3) {
    lcd.setCursor(0, 0);
    lcd.print(">Reabastecer    ");
    lcd.setCursor(0,1);
    lcd.print("                ");
  }
  else if(selectedOption == 1) {
    lcd.setCursor(0, 0);
    lcd.print(">Fluxo             ");
    lcd.setCursor(0, 1);
    lcd.print("Abastecimento ");
  }
  else if(selectedOption == 2) {
    lcd.setCursor(0, 0);
    lcd.print("Fluxo              ");
    lcd.setCursor(0, 1);
    lcd.print(">Abastecimento ");
  }
}

void atualizarDisplay(){
  lcd.setCursor(0,1);
  
  if(dia<10){
    lcd.print("0");
  }
  lcd.print(dia);
  lcd.print("/");
  
  if(mes<10){
    lcd.print("0");
  }
  lcd.print(mes);
  lcd.print("/");
  lcd.print(ano);
}

void menu(){
  lcd.setCursor(0,0);
  lcd.print("Data:");
  atualizarDisplay();

  if(last_button_state1 == HIGH && button_state1 == LOW){
    Serial.println("The button 1 is pressed");
    dia++;
      if(dia>31){
        dia = 1;
      }
    atualizarDisplay();
    delay(200);
  }
    if(last_button_state2 == HIGH && button_state2 == LOW){
      Serial.println("The button 2 is pressed");
    mes++;
    if(mes>12){
      mes=1;
    }
    atualizarDisplay();
    delay(200);
  }
   if(last_button_state3 == HIGH && button_state3 == LOW){
    Serial.println("The button 3 is pressed");
    ano++;
    atualizarDisplay();
    delay(200);
  }
}