#define potenPin 32
#define buttonPin 14
int leds[] = {15, 2, 0, 4, 16, 17};
int buttonState = HIGH;
int lastButtonState = HIGH;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 10;
int buttonCounter = 0;
bool ledsOn = false;
void setup() {
pinMode(potenPin, INPUT);
pinMode(buttonPin, INPUT_PULLUP);
for (int i = 0; i < sizeof(leds) / sizeof(leds[0]); i++)
pinMode(leds[i], OUTPUT);
Serial.begin(115200);
}
void loop() {
int potenValue = analogRead(potenPin);
int numLeds = map(potenValue, 0, 4095, 0, sizeof(leds) / sizeof(leds[0]));
for (int i = 0; i < sizeof(leds) / sizeof(leds[0]); i++) {
if (i < numLeds) {
digitalWrite(leds[i], ledsOn ? HIGH : LOW);
} else {
digitalWrite(leds[i], LOW);
}
}
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == LOW) {
buttonCounter++;
Serial.print("PUNPITCHA เลขที่ 26 กดครั้งที่ ");
Serial.println(buttonCounter);
ledsOn = !ledsOn;
}
}
}
lastButtonState = reading;
}