#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
//Using an array to define all of the LEDs. Keeps code short. (JI)
int ledArray[8] = {47, 48, 45, 0, 35, 36, 37, 38};
//Function Declarations
void initialize_gpio(); //When called, all LEDs will be set as outputs. (JI)
void printbin8(uint8_t valu); //When called, the value of the condition passed will be stored as an 8-bit unsigned integer. (JI)
void app_main() {
initialize_gpio(); //Initliazing LEDs as outputs. This is only done once. (JI)
//initializing the unsigned integer ‘count’ to 0.
uint8_t count = 0;
//While-loop is required in order to iterate through each binary number on the counter, as well as have its associated LED turn on. (JI)
while (1) {
printbin8(count); //Function is called for counter. Value of function's condition is passed as 'count'. The 'count' is then stored in a variable which is an 8-bit unsigned integer called 'valu'. (JI)
for (int i = 0; i < 8; i++) { //For-loop will iterate through its entire sequence before proceeding to next lines of code outside the loop. (JI)
gpio_set_level(ledArray[i], (count >> (7-i)) & 1);
/*Each LED in the array is selected one at a time through 'i'.
After the LED is selected, the current count is right shifted by 7 minus 'i'.
Starting at seven is vital for printing the binary digits from MSB to LSB.
After the right shift occurs, the result is ANDed with 1.
If the value ANDed with 1 is 1, then the selected LED turns on. If the value ANDed with 1 is 0, the LED stays off. (JI)
*/
}
count++; //Increments infinitely, but will repeat from 0 after reaching 255. The count is increased for the next iteration of the function call. (JI)
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
} //End of app-main (JI)
//Function Definitions (JI)
//Function defintion to intialize all LEDs as outputs. (JI)
void initialize_gpio() {
for (int i = 7; i >= 0; i--) {
gpio_set_direction(ledArray[i], GPIO_MODE_OUTPUT); //Setting all LEDs as outputs. (JI)
}
}
//Function definition for the binary counter. (JI)
void printbin8(uint8_t valu) { //This is a function which will print the value of an 8-bit unsigned integer when called. (JI)
printf("The value of %u in binary is: 0b", valu); // Use %u for uint8_t
for (int i = 7; i >= 0; i--) { //For-loop structure ensures we are printing from MSB to LSB and not exceeding 8 bits. (JI)
printf("%u", (valu >> i) & 1); //The value of 'valu' is right-shifted by 'i' from the for-loop. (JI)
}
printf("\n"); //Ensures each binary entry is recorded on a new line. (JI)
}Loading
esp32-s3-devkitc-1
esp32-s3-devkitc-1