const int led = 9;
const int button = 2;
const int pot = A0;
bool lampOn = false;
int buttonState = HIGH;
int lastButtonRead = HIGH;
unsigned long lastDebounceTime = millis();
const unsigned long debounceDelay = 50; // [ms]
void setup() {
pinMode(led, OUTPUT);
pinMode(button, INPUT_PULLUP);
}
void loop() {
int reading = digitalRead(button);
if (reading != lastButtonRead) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == LOW) {
lampOn = !lampOn;
}
}
}
lastButtonRead = reading;
if (lampOn) {
int potValue = analogRead(pot);
int pwmValue = map(potValue, 0, 1023, 0, 255);
analogWrite(led, pwmValue);
} else {
analogWrite(led, 0);
}
}