//headerfile is used to manage delays like vtask delay
#include "freertos/FreeRTOS.h"
//headerfile is used to manage delays like vtask delay
#include "freertos/task.h"
//GPIO driver header for controlling input/output pins on the ESP32-S3.
#include "driver/gpio.h"
// Define GPIO pins for LEDs and buttons
int leds[4] = {45, 48, 47, 21};
//making the code understand A is num 4
#define BUTTON_A GPIO_NUM_4
//making the code understand B is num 5
#define BUTTON_B GPIO_NUM_5
// Function declarations
//blinks the LEDS in a sequence
void blink_leds_in_sequence(int LEDs[], int num_leds, int delay_ms);
//calling the function for the LEDS to blink ina forward motion
void forward();
//calling the function to blink the LEDS backward
void backward();
//calling the function to blink the LEDS alltogether
void altogether();
//calling the function to flash even and odd number of the LEDS
void alternate();
//MAIN
void app_main() {
// Set the LED pins as outputs
for (int i = 0; i < 4; i++) {
gpio_set_direction(leds[i], GPIO_MODE_OUTPUT);
}
// Set the button pins as inputs with pull-down resistors
gpio_set_direction(BUTTON_A, GPIO_MODE_INPUT);
gpio_set_direction(BUTTON_B, GPIO_MODE_INPUT);
//infinite loop
while (1) {
// Read button states HIGH AND LOW
int button_a_state = gpio_get_level(BUTTON_A);
int button_b_state = gpio_get_level(BUTTON_B);
// Control LEDs based on button states
//when A is off and B is off call the forward function
if (button_a_state == 0 && button_b_state == 0) {
forward();
}
//When A is pressed/high and B is Off/low call the backward function
else if (button_a_state == 1 && button_b_state == 0) {
backward();
}
//When A is off/low and B is on/high call the altogether function
else if (button_a_state == 0 && button_b_state == 1) {
altogether();
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
//when both A and B are on/high call the alternate funtion
else{
alternate();
}
//adding a delay
}
}
//turning the LEDS on and off in sequence
void blink_leds_in_sequence(int LEDs[], int num_leds, int delay_ms) {
for (int i = 0; i < num_leds; i++) {
gpio_set_level(LEDs[i], 1);
vTaskDelay(500 / portTICK_PERIOD_MS);
gpio_set_level(LEDs[i], 0);
}
}
void forward(){
//loops through the array incremtning by one everytime and looping
for(int i = 0; i <4; i++) {
//turn the LEDS on in the array
gpio_set_level(leds[i], 1);
//adding a delay
vTaskDelay(500 / portTICK_PERIOD_MS);
//turns off the LED at index 0
gpio_set_level(leds[i], 0);
}
}
void backward(){
//loops through the array
for(int i = 3; i >= 0; i--){
gpio_set_level(leds[i], 1);
vTaskDelay(500 / portTICK_PERIOD_MS);
gpio_set_level(leds[i], 0);
}
}
void alternate() {
for (int i = 0; i < 4; i++) {
if (i % 2 == 0) { // For even-indexed LEDs
gpio_set_level(leds[i], 1); // Turn on LED
}
}
vTaskDelay(1000 / portTICK_PERIOD_MS); // Wait
for (int i = 0; i < 4; i++) {
if (i % 2 == 0) { // For even-indexed LEDs
gpio_set_level(leds[i], 0); // Turn off LED
}
}
for (int i = 0; i < 4; i++) {
if (i % 2 != 0) { // For odd-indexed LEDs
gpio_set_level(leds[i], 1); // Turn on LED
}
}
vTaskDelay(1000 / portTICK_PERIOD_MS); // Wait
for (int i = 0; i < 4; i++) {
if (i % 2 != 0) { // For odd-indexed LEDs
gpio_set_level(leds[i], 0); // Turn off LED
}
}
}
void altogether(){
for(int i = 0; i < 4; i++){
gpio_set_level(leds[i], 1);
}
vTaskDelay(1000 / portTICK_PERIOD_MS);
for (int i = 0; i < 4; i++){
gpio_set_level(leds[i], 0);
}
}