#include <Encoder.h>
#define ENCODER_PINA 1
#define ENCODER_PINB 2
#define BUTTON_PIN 3
#define LED_PIN_OK 13
#define LED_PIN_ER 12
Encoder encoder(ENCODER_PINA, ENCODER_PINB);
volatile boolean buttonPressed = false;
char password[6] = {'0', '0', '0', '0', '0', '0'};
int index = 0;
int antPos;
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_PIN_OK, OUTPUT);
pinMode(LED_PIN_ER, OUTPUT);
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), buttonPressedISR, LOW);
antPos = encoder.read();
Serial.begin(9600);
}
int pass = 0;
void loop() {
int encoderPos = encoder.read() / 4;
Serial.print("encPos");
Serial.println(encoderPos);
if (encoderPos != antPos){
if (encoderPos > antPos)
pass++;
else
pass--;
antPos = encoderPos;
}
if (pass < 0) pass = 9;
if (pass > 9) pass = 0;
if (buttonPressed) {
// Incrementa o índice da senha
index++;
if (index >= 6) {
index = 0; // Retorna ao primeiro dígito após 6 dígitos inserido
boolean correctPassword = true;
for (int i = 0; i < 6; i++) {
if (password[i] != '0') {
correctPassword = false;
break;
}
}
if (correctPassword) {
digitalWrite(LED_PIN_OK, HIGH); // LED indicador acende
delay(1500);
digitalWrite(LED_PIN_OK, LOW); // LED indicador acende
} else {
digitalWrite(LED_PIN_ER, HIGH); // LED indicador acende
delay(1500);
digitalWrite(LED_PIN_ER, LOW); // LED indicador acende
}
}
buttonPressed = false;
antPos = 0;
encoder.write(0);
}
password[index] = pass + '0';
Serial.print("Senha: ");
for (int i = 0; i<=index; i++)
Serial.print(password[i]);
Serial.println();
delay(100);
}
void buttonPressedISR() {
buttonPressed = true;
}