#define LED_PIN 3
#define POT_PIN A0
#define BTN_PIN 7
bool ledState = false;
bool lastBtn = LOW;
unsigned long lastDebounceTime = 0;
const int debounceDelay = 200; // ms
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
pinMode(BTN_PIN, INPUT); // pull-down ngoài
}
void loop() {
bool btn = digitalRead(BTN_PIN);
Serial.println(btn);
// phát hiện cạnh lên + debounce chuẩn
if (btn == HIGH && lastBtn == LOW && millis() - lastDebounceTime > debounceDelay) {
ledState = !ledState;
lastDebounceTime = millis();
}
lastBtn = btn;
if (ledState) {
int val = analogRead(POT_PIN);
int pwm = map(val, 0, 1023, 0, 255);
analogWrite(LED_PIN, pwm);
} else {
analogWrite(LED_PIN, 0);
}
}