#define BUTTON_1_PIN 6
#define BUTTON_2_PIN 5
#define BUTTON_3_PIN 4
#define LED_1 10
#define LED_2 3
#define LED_3 8
byte Last_State_button_1 = LOW;
byte Last_State_button_2 = LOW;
byte Last_State_button_3 = LOW;
unsigned long press_interval_1 = 50;
unsigned long press_interval_2 = 50;
unsigned long press_interval_3 = 50;
unsigned long tempo_limitePermitido = 5000;
unsigned long int tempo_B_1 = 0;
unsigned long int tempo_B_2 = 0;
unsigned long int tempo_B_3 = 0;
unsigned long debounceDuration = 50; // millis
unsigned long int lastTimeButton_1_StateChanged = 0;
unsigned long int lastTimeButton_2_StateChanged = 0;
unsigned long int lastTimeButton_3_StateChanged = 0;
void setup() {
Serial.begin(115200);
pinMode(BUTTON_1_PIN, INPUT_PULLUP);
pinMode(BUTTON_2_PIN, INPUT_PULLUP);
pinMode(BUTTON_3_PIN, INPUT_PULLUP);
pinMode(LED_1, OUTPUT); // define LED como saida
pinMode(LED_2, OUTPUT); // define LED como saida
pinMode(LED_3, OUTPUT); // define LED como saida
digitalWrite(LED_1, LOW);
digitalWrite(LED_2, LOW);
digitalWrite(LED_3, LOW);
}
void loop() {
// BOTAO 1 -----------------------------------------------------------------------------------------
if (millis() - lastTimeButton_1_StateChanged >= debounceDuration) {
byte button_1_State = digitalRead(BUTTON_1_PIN);
if(millis() - tempo_B_1 >= press_interval_1){
digitalWrite(LED_1, LOW);
}
// Se o BOTÃO 1 foi pressionado
if (button_1_State != Last_State_button_1) {
lastTimeButton_1_StateChanged = millis();
Last_State_button_1 = button_1_State;
// Se o botão foi pressionado e o intervalo de 5 segundos já passou
if (button_1_State == LOW && millis() - tempo_B_1 >= press_interval_1) {
Serial.println("Permissão CONCEDIDA ao Button 1");
digitalWrite(LED_1, HIGH);
press_interval_1 = tempo_limitePermitido;
tempo_B_1 = millis(); // atualiza o tempo do último pressionamento do botão 1
}
}
}
// BOTAO 2 -----------------------------------------------------------------------------------------
if (millis() - lastTimeButton_2_StateChanged >= debounceDuration) {
byte button_2_State = digitalRead(BUTTON_2_PIN);
if(millis() - tempo_B_2 >= press_interval_2){
digitalWrite(LED_2, LOW);
}
// Se o BOTÃO 2 foi pressionado
if (button_2_State != Last_State_button_2) {
lastTimeButton_2_StateChanged = millis();
Last_State_button_2 = button_2_State;
// Se o botão foi pressionado e o intervalo de 5 segundos já passou
if (button_2_State == LOW && millis() - tempo_B_2 >= press_interval_2) {
Serial.println("Permissão CONCEDIDA ao Button 2");
digitalWrite(LED_2, HIGH);
press_interval_2 = tempo_limitePermitido;
tempo_B_2 = millis(); // atualiza o tempo do último pressionamento do botão 2
}
}
}
// BOTAO 3 -----------------------------------------------------------------------------------------
if (millis() - lastTimeButton_3_StateChanged >= debounceDuration) {
byte button_3_State = digitalRead(BUTTON_3_PIN);
if(millis() - tempo_B_3 >= press_interval_3){
digitalWrite(LED_3, LOW);
}
// Se o BOTÃO 3 foi pressionado
if (button_3_State != Last_State_button_1) {
lastTimeButton_3_StateChanged = millis();
Last_State_button_3 = button_3_State;
// Se o botão foi pressionado e o intervalo de 5 segundos já passou
if (button_3_State == LOW && millis() - tempo_B_3 >= press_interval_3) {
Serial.println("Permissão CONCEDIDA ao Button 3");
digitalWrite(LED_3, HIGH);
press_interval_3 = tempo_limitePermitido;
tempo_B_3 = millis(); // atualiza o tempo do último pressionamento do botão 3
}
}
}
}