[Newbie] Button seems to be acting flakey but I think it is wired correctly what am I missing?

partyk1d24
Posts: 1
Joined: Wed Jan 17, 2024 7:30 pm

[Newbie] Button seems to be acting flakey but I think it is wired correctly what am I missing?

Postby partyk1d24 » Wed Jan 17, 2024 11:47 pm

I have a switch from this set https://spot.pcc.edu/~dgoldman/labs/37SENSORKIT.pdf
switchDoc.png
switchDoc.png (93.5 KiB) Viewed 584 times
I hook it up to port 32 and use this code

Code: Select all

gpio_set_direction(GPIO_NUM_25, GPIO_MODE_OUTPUT);
    gpio_set_direction(GPIO_NUM_32, GPIO_MODE_INPUT);
    gpio_set_level(GPIO_NUM_25, 1);
    int button_state = 1;
    while (1) {
        const int current_state = gpio_get_level(GPIO_NUM_32);
        if (current_state == 0 && button_state == 1) {
            //         // If button is pressed
            printf("Pressed");
            button_state = 0;
            // Turn the LED on
        } else if(current_state == 1 && button_state == 0) {
            button_state = 1;
            // printf("Button Released");
        }

        vTaskDelay(1);
        // Add 1 tick delay (10 ms) so that current task does not starve idle task and trigger watchdog timer
}
The light works fine but the switch just keeps printing "pressed" and it seems a bit jittery like the wires aren't in well but I triple checked them. I have it wired to the ground as well as the pin and the 5v first pin in my dev board.

Also since this is my first time posting feel free to let me know if this is the wrong room.
board.jpg
board.jpg (101.45 KiB) Viewed 584 times

Who is online

Users browsing this forum: axellin and 96 guests