#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Configuração do LCD (endereço 0x27 pode variar)
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Pinos
const int button1 = 2; // Botão para motor
const int button2 = 3; // Botão para LCD
const int button3 = 4; // Botão para buzzer
const int buzzer = 5;
const int motorPins[] = {8, 9, 10, 11};
// Variáveis
bool motorState = false;
unsigned long motorStartTime = 0;
unsigned long motorRunTime = 5000; // Tempo de execução (5 segundos)
// Função para inicializar o motor
void setupMotor() {
for (int i = 0; i < 4; i++) {
pinMode(motorPins[i], OUTPUT);
digitalWrite(motorPins[i], LOW);
}
}
// Sequência do motor de passo
void stepMotor(int step) {
const int sequence[4][4] = {
{1, 0, 0, 1},
{1, 1, 0, 0},
{0, 1, 1, 0},
{0, 0, 1, 1}
};
for (int i = 0; i < 4; i++) {
digitalWrite(motorPins[i], sequence[step][i]);
}
}
void setup() {
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
pinMode(buzzer, OUTPUT);
setupMotor();
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Painel de Controle");
}
void loop() {
// Botão do motor
if (digitalRead(button1) == LOW) {
motorState = !motorState;
motorStartTime = millis();
delay(200); // Debounce
}
if (motorState && millis() - motorStartTime < motorRunTime) {
for (int i = 0; i < 4; i++) {
stepMotor(i);
delay(5); // Ajuste para velocidade
}
} else {
motorState = false;
}
// Botão do LCD
if (digitalRead(button2) == LOW) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Texto Alterado!");
delay(200); // Debounce
}
// Botão do buzzer
if (digitalRead(button3) == LOW) {
digitalWrite(buzzer, HIGH);
delay(2000); // Buzina por 2 segundos
digitalWrite(buzzer, LOW);
delay(200); // Debounce
}
}