Open nk25719 opened 4 months ago
Connect one terminal of the button to GPIO 36 (or any other available GPIO pin). Connect the other terminal of the button to GND.
Add the button functionality to your existing code. Toggle an LED connected to GPIO 2:
#define BUTTON_PIN 39 // GPIO 36 for the button
#define LED_PIN 2 // GPIO 2 for the blue LED
bool ledState = false; // Initial state of the LED
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50; // Debounce time in milliseconds
bool lastButtonState = HIGH;
bool currentButtonState;
bool buttonPressed = false;
void setup() {
Serial.begin(115200);
pinMode(BUTTON_PIN, INPUT); // Set button pin as input
pinMode(LED_PIN, OUTPUT); // Set LED pin as output
digitalWrite(LED_PIN, LOW); // Initialize LED to off
// Ensure internal pull-up is enabled on the button pin (for ESP32, INPUT_PULLUP may not work for GPIO 36)
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop() {
bool reading = digitalRead(BUTTON_PIN);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonPressed) {
buttonPressed = reading;
if (buttonPressed == LOW) { // Button pressed (active low)
ledState = !ledState; // Toggle LED state
digitalWrite(LED_PIN, ledState ? HIGH : LOW);
Serial.println(ledState ? "KRAKE ON" : "KRAKE OFF");
}
}
}
lastButtonState = reading;
}
The ON/OFF button is not correctly toggling KRAKE on and off as intended. Instead, it turns the LED on and off after being held down for approximately ten seconds.
Add the necessary firmware for the ON/OFF butom that us connected to GPIO pin#39