/*controlando Leds e botões com ESP32
Turma 641N1
Aluno: Marcelo P. Silveira
Material:
2 pushbutton
2 leds
2 resistores de 100ohms
1 ESP32
1 LCD i2C
CÓDIGO DO PROGRAMA:
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
int bt1 = 14; // Define buttonPin no pino digital 14
int led1 = 13; // Define ledPin no pino digital 13
int bt2 = 19; // Define buttonPin no pino digital 19
int led2 = 18; // Define ledPin no pino digital 18
int estadoButton1 = 0; // Variável responsável por armazenar o estado do botão 1 (ligado/desligado)
int estadoButton2 = 0; // Variável responsável por armazenar o estado do botão 2 (ligado/desligado)
void setup() {
lcd.init();
lcd.backlight();
lcd.print("ESP32 - Projeto");
lcd.setCursor(0, 1);
lcd.print("com display!");
delay(4000);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(bt1, INPUT_PULLUP);
pinMode(bt2, INPUT_PULLUP);
}
void loop() {
estadoButton1 = digitalRead(bt1); // Lê o valor de bt1 e armazena em estadoButton1
estadoButton2 = digitalRead(bt2); // Lê o valor de bt2 e armazena em estadoButton2
if (estadoButton1 == LOW) { // Se bt1 estiver pressionado
digitalWrite(led1 , HIGH); // Liga o LED1
lcd.setCursor(0, 1);
lcd.print("Led Vermelho ON ");
} else { // Senão
digitalWrite(led1, LOW); // Desliga o LED1
lcd.setCursor(0, 1);
lcd.print("Led Vermelho OFF");
}
if (estadoButton2 == LOW) { // Se bt2 estiver pressionado
digitalWrite(led2 , HIGH); // Liga o LED2
lcd.setCursor(0, 0);
lcd.print("Led Azul ON ");
} else { // Senão
digitalWrite(led2, LOW); // Desliga o LED2
lcd.setCursor(0, 0);
lcd.print("Led Azul OFF ");
}
delay(100); // Intervalo de 100 milissegundos para evitar leituras rápidas
}