#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
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 button as an input
gpio_set_direction(BUTTON_PIN, GPIO_MODE_INPUT);
//creating a loop to define all the LED pins as outputs
for (int i = 0; i < 8; i++) {
gpio_set_direction(led_pins[i], GPIO_MODE_OUTPUT);
}
//while loop to infinitely read the value of btnState
while (1) {
//setting the function we made to equal btnState and set the pass by value to the button input
int btnState = readPinStateWithDebounce(BUTTON_PIN);
//Readng current pinstate
if (btnState == 1) {
//loop to set all LED pins to 0
for (int i = 0; i < 8; i++) {
gpio_set_level(led_pins[i], 0);
}
}
//reading current pinstate
else {
//loop to set all LED pins to 1
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 - checks the state of the Button repeatedly while printing the variables
* Return type - int
* Parameters - pin
*/
int readPinStateWithDebounce(int pin) {
int istate = gpio_get_level(pin);
if (istate == 0) {
vTaskDelay(pdMS_TO_TICKS(50));
printf("The 1st state is %d\n", istate);
int cstate = gpio_get_level(pin);
if (cstate == 0) {
printf("the 2nd state is %d\n", cstate);
return 0;
}
else {
printf("button not pressed. Pinstate high = %d\n", istate);
return 1;
}
}
else {
printf("button not pressed. Pinstate high = %d\n", istate);
return 1;
}
}