I'm working on my project of blinking 8 LEDs multiple cycles using esp32-s3, which has this weird problem. In the first cycle, it has no problem. For example, all cycle inputs are the same (1 2 3 4 5 6 7 8). At first cycle, it's fine. In the second cycle, the LEDs start to bink like the input is ( 0 1 2 3 4 5 6 7). And in the third cycle, the output becomes (8 0 1 2 3 4 5 6). I think this is the problem when getting input from the serial monitor but I don't know how to fix this. Can someone help me find the solution for this?
(https://youtube.com/shorts/8EAeeey00Cg?si=fXKury9qSdx3suFj) Here is the video I made when trying with (1 1 1 1 1 1 1 1) input for all three cycles.
// Array of LED pins
int ledPins[] = {2, 4, 5, 12, 13, 14, 15, 16};
// Button pin
const int buttonPin = 17;
// Button state variables
int buttonState = 0;
int lastButtonState = 0;
// Button press counter to track which cycle to run
int cycleCounter = 0;
// Arrays to store the blink counts for each LED across 3 cycles
int blinkCountsCycle1[8];
int blinkCountsCycle2[8];
int blinkCountsCycle3[8];
// Variables for button debouncing
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50; // 50ms debounce delay
// Function to safely read input and avoid missing values
int readBlinkCount() {
while (Serial.available() == 0) {
// Wait for user input
}
return Serial.parseInt();
}
void setup() {
// Initialize the LED pins as outputs
for (int i = 0; i < 8; i++) {
pinMode(ledPins[i], OUTPUT);
}
// Initialize the button pin as input
pinMode(buttonPin, INPUT);
// Start serial communication
Serial.begin(115200);
// Wait for serial monitor to connect
while (!Serial) {
delay(10);
}
// Prompt the user to input blink counts for Cycle 1
Serial.println("Enter the blink count for each LED (8 values) for Cycle 1:");
for (int i = 0; i < 8; i++) {
blinkCountsCycle1[i] = readBlinkCount();
}
// Prompt the user to input blink counts for Cycle 2
Serial.println("Enter the blink count for each LED (8 values) for Cycle 2:");
for (int i = 0; i < 8; i++) {
blinkCountsCycle2[i] = readBlinkCount();
}
// Prompt the user to input blink counts for Cycle 3
Serial.println("Enter the blink count for each LED (8 values) for Cycle 3:");
for (int i = 0; i < 8; i++) {
blinkCountsCycle3[i] = readBlinkCount();
}
// Confirm that blink values are set for all 3 cycles
Serial.println("Blink settings updated for all 3 cycles.");
}
void loop() {
// Read the current button state
int reading = digitalRead(buttonPin);
// Debounce the button: If the button state has changed and it has been long enough
if (reading != lastButtonState) {
lastDebounceTime = millis(); // Reset the debounce timer
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// Only register the button press when the state has stabilized
if (reading != buttonState) {
buttonState = reading;
// Only act if the button is pressed (HIGH state)
if (buttonState == HIGH) {
cycleCounter++; // Increment cycle counter on valid button press
// Pointer to hold the address of the current cycle's blink counts
int* blinkCounts;
if (cycleCounter == 1) {
Serial.println("Starting Cycle 1:");
blinkCounts = blinkCountsCycle1; // Point to Cycle 1's blink counts
}
else if (cycleCounter == 2) {
Serial.println("Starting Cycle 2:");
blinkCounts = blinkCountsCycle2; // Point to Cycle 2's blink counts
}
else if (cycleCounter == 3) {
Serial.println("Starting Cycle 3:");
blinkCounts = blinkCountsCycle3; // Point to Cycle 3's blink counts
cycleCounter = 0; // Reset to 0 after the third cycle is complete
}
// If we have valid blink counts for this cycle, blink the LEDs
if (blinkCounts != nullptr) {
printBlinkCounts(blinkCounts); // Show blink counts for current cycle
blinkLEDs(blinkCounts); // Use pointer to blink counts for current cycle
}
}
}
}
// Save the current reading as the last button state for the next loop iteration
lastButtonState = reading;
delay(50); // Short delay for button debounce
}
// Function to blink LEDs based on the provided blink counts array (using pointer)
void blinkLEDs(int* blinkCounts) {
for (int i = 0; i < 8; i++) {
// Debug output to ensure each LED is handled correctly
Serial.print("Blinking LED ");
Serial.print(i + 1);
Serial.print(" for ");
Serial.print(*(blinkCounts + i)); // Use pointer arithmetic to get blink count
Serial.println(" times.");
// Blink the current LED the specified number of times
for (int j = 0; j < *(blinkCounts + i); j++) {
digitalWrite(ledPins[i], HIGH); // Turn the LED on
delay(500); // Wait for 500 milliseconds
digitalWrite(ledPins[i], LOW); // Turn the LED off
delay(500); // Wait for 500 milliseconds
}
}
}
// Function to print how many times each LED will blink per cycle (using pointer)
void printBlinkCounts(int* blinkCounts) {
for (int i = 0; i < 8; i++) {
Serial.print("LED ");
Serial.print(i + 1);
Serial.print(" will blink ");
Serial.print(*(blinkCounts + i)); // Use pointer arithmetic to get blink count
Serial.println(" times.");
}
}
Here is the serial monitor when I did the (1 2 3 4 5 6 7 8) input for all three cycle.
16:52:14.537 -> Enter the blink count for each LED (8 values) for Cycle 1:
16:52:34.373 -> Enter the blink count for each LED (8 values) for Cycle 2:
16:52:38.776 -> Enter the blink count for each LED (8 values) for Cycle 3:
16:52:42.831 -> Blink settings updated for all 3 cycles.
16:52:47.845 -> Starting Cycle 1:
16:52:47.845 -> LED 1 will blink 1 times.
16:52:47.845 -> LED 2 will blink 2 times.
16:52:47.887 -> LED 3 will blink 3 times.
16:52:47.887 -> LED 4 will blink 4 times.
16:52:47.887 -> LED 5 will blink 5 times.
16:52:47.887 -> LED 6 will blink 6 times.
16:52:47.887 -> LED 7 will blink 7 times.
16:52:47.887 -> LED 8 will blink 8 times.
16:52:47.887 -> Blinking LED 1 for 1 times.
16:52:48.873 -> Blinking LED 2 for 2 times.
16:52:50.889 -> Blinking LED 3 for 3 times.
16:52:53.892 -> Blinking LED 4 for 4 times.
16:52:57.891 -> Blinking LED 5 for 5 times.
16:53:02.883 -> Blinking LED 6 for 6 times.
16:53:08.883 -> Blinking LED 7 for 7 times.
16:53:15.892 -> Blinking LED 8 for 8 times.
16:53:26.072 -> Starting Cycle 2:
16:53:26.072 -> LED 1 will blink 0 times.
16:53:26.072 -> LED 2 will blink 1 times.
16:53:26.072 -> LED 3 will blink 2 times.
16:53:26.072 -> LED 4 will blink 3 times.
16:53:26.072 -> LED 5 will blink 4 times.
16:53:26.117 -> LED 6 will blink 5 times.
16:53:26.117 -> LED 7 will blink 6 times.
16:53:26.117 -> LED 8 will blink 7 times.
16:53:26.117 -> Blinking LED 1 for 0 times.
16:53:26.117 -> Blinking LED 2 for 1 times.
16:53:27.085 -> Blinking LED 3 for 2 times.
16:53:29.085 -> Blinking LED 4 for 3 times.
16:53:32.106 -> Blinking LED 5 for 4 times.
16:53:36.085 -> Blinking LED 6 for 5 times.
16:53:41.085 -> Blinking LED 7 for 6 times.
16:53:47.103 -> Blinking LED 8 for 7 times.
16:54:02.648 -> Starting Cycle 3:
16:54:02.648 -> LED 1 will blink 8 times.
16:54:02.648 -> LED 2 will blink 0 times.
16:54:02.648 -> LED 3 will blink 1 times.
16:54:02.648 -> LED 4 will blink 2 times.
16:54:02.648 -> LED 5 will blink 3 times.
16:54:02.648 -> LED 6 will blink 4 times.
16:54:02.648 -> LED 7 will blink 5 times.
16:54:02.690 -> LED 8 will blink 6 times.
16:54:02.690 -> Blinking LED 1 for 8 times.
16:54:10.704 -> Blinking LED 2 for 0 times.
16:54:10.704 -> Blinking LED 3 for 1 times.
16:54:11.695 -> Blinking LED 4 for 2 times.
16:54:13.689 -> Blinking LED 5 for 3 times.
16:54:16.656 -> Blinking LED 6 for 4 times.
16:54:20.695 -> Blinking LED 7 for 5 times.
16:54:25.672 -> Blinking LED 8 for 6 times.```