#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
//Variable to Read the pin state
int readPinStateWithDebounce(int pin);
// Pin definitions
#define BUTTON_PIN 4 // Button connected to GPIO 4
uint8_t led_pins[] = {38, 37, 36, 35, 0, 45, 48, 47};
void app_main() {
// setting the GPIO to input mode
gpio_set_direction(BUTTON_PIN, GPIO_MODE_INPUT);
//looping the LEDS and setting the LEDS to output
for (int i = 0; i < 8; i++) {
gpio_set_direction(led_pins[i], GPIO_MODE_OUTPUT);
}
while (1) {
//setting the button state to the debounce state
int btnState = readPinStateWithDebounce(BUTTON_PIN);
// if button state is 1 then loop LED pins
if (btnState == 1) {
for (int i = 0; i < 8; i++) {
gpio_set_level(led_pins[i], 0);
}
} else {
for (int i = 0; i < 8; i++) {
gpio_set_level(led_pins[i], 1);
}
}
vTaskDelay(pdMS_TO_TICKS(200)); // Small delay to prevent excessive looping
}
}
/* Function Name - readPinStateWithDebounce
* Description - reads pin with a debounce
* Return type - int
* Parameters -integer -pin
*/
//reading the pin state
int readPinStateWithDebounce(int pin) {
//button state is the same as the pin Level
int btnstate = gpio_get_level(pin);
//when button is not presses the display state which is 1
if (btnstate == 0) {
vTaskDelay(pdMS_TO_TICKS(50));
int btnState = gpio_get_level(pin);
printf("Button state 1 = 0 \n");
// if presses state pin = 0
if (btnstate == 0) {
printf("Button state 2 = 0 \n");
return 0;
}
return 1;
}
else{
printf("Button not pressed. ");
printf("Button state = 1 \n");
return 1;
}
}