#include <Arduino.h>
static const int LED_PIN = 2;
static const int BTN_PIN = 4;
// Estado do “modo†(muda a velocidade da tarefa do LED)
static bool fastMode = false;
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
pinMode(BTN_PIN, INPUT_PULLUP);
}
// “Task†A: LED periódico (200ms ou 80ms)
void led_timing(bool mode, unsigned long now) {
static unsigned long lastLed = 0;
unsigned long ledperiod = mode ? 100 : 500;
if (now - lastLed >= ledPeriod){
lastled = now;
digitalWrite(LED_PIN,!digitalRead(LED_PIN));
}
}
// “Task†B: log a cada 1000ms
void log_alive(unsigned long now) {
static unsigned long lastLog = 0;
if (now - lastLog >=1000)
{
lastLog =now;
Serial.printf("[loop] modo =%s\n", fastMode ? "fastMode" : "NORMAL");
}
}
// “Task†C: polling do botão (com debounce)
void button_press() {
if (digitalRead(BTN_PIN) == LOW) { // apertou
delay(50);
if(digitalRead(BTN_PIN) == LOW){
fastMode = !fastMode;
Serial.printf("[btn] modo =%s\n]", fastMode ? "fastMode" : "NORMAL");
while(!digitalRead(btn_pin)==LOW){}
}
delay(50);
}
}
void loop() {
unsigned long now = millis();
button_press();
led_timing(fastMode, now);
log_alive(now);
}