I am trying to display a countdown on one 8x8 LED matrix (while motor is working for 30 seconds), but I can't get it to work. I mean the LED goes blank for 30 secs, but nothing is being scrolled.
Could someone give me a hint?
The code is following (I have been trying to use the assistance of ChatGDP):
void startMotorAndCountdown() {
motorRunning = true;
digitalWrite(relayPin, HIGH); // Activate the relay to start the motor
unsigned long countdownStartTime = millis();
unsigned long countdownDuration = 30 * 1000; // 30 seconds in milliseconds
unsigned long lastUpdateTime = 0;
const int updateInterval = 500; // Update display every 500 milliseconds
while (millis() - countdownStartTime <= countdownDuration) {
// Calculate the remaining time
unsigned long remainingTime = (countdownDuration - (millis() - countdownStartTime)) / 1000;
// Update the display at a regular interval
if (millis() - lastUpdateTime >= updateInterval) {
myDisplay.displayClear();
String buffer = String(remainingTime); // Use String instead of char array
myDisplay.displayText(buffer.c_str(), PA_LEFT, 50, 0, PA_SCROLL_LEFT);
myDisplay.displayAnimate(); // Update the display
lastUpdateTime = millis(); // Update last update time
}
// Add a small delay to avoid rapid updates
delay(50);
}
digitalWrite(relayPin, LOW); // Deactivate the relay to stop the motor
motorRunning = false;
// Clear the display after the countdown is complete
myDisplay.displayClear();
myDisplay.displayText("AQQASHLARI TEMiZLE, CEMi 20 QEPiYE", PA_LEFT, 50, 0, PA_SCROLL_LEFT);
myDisplay.displayAnimate(); // Update the display
}
Your comment near displayAnimate() seems to indicate that you expect the animation to happen when the function call is executed, somehow, in the background.
displayAnimate() will check to see if an animation frame is due and do something to animate if the time has come. Nothing will happen if there is no animation in progress.
The first displayAnimate() should be where you have the delay() and get rid of the delay().
The second displayAnimate() should be in your loop(). The way you have it structured at the end of your function it will do the first frame of the animation and then nothing else will happen.
Please read the documentation for the library (in docs folder) or look at how the examples have structured the animation.
Thanks for help!
I've found a useful code piece that solved the problem.
void startMotorAndCountdown() {
motorRunning = true;
digitalWrite(relayPin, HIGH); // Activate the relay to start the motor
int counter = 30; // the counter will start from 30, you can set any value
char Buffer[3] = " "; // create a buffer to hold the numbers
unsigned long previousTime = 0;
const unsigned long interval = 1000; // interval at which to update the display (milliseconds)
while (counter > 0) {
unsigned long currentTime = millis();
if (currentTime - previousTime >= interval) {
previousTime = currentTime;
myDisplay.displayClear(); // Clear the display before updating
sprintf(Buffer, "%02d", counter);
myDisplay.displayText(Buffer, PA_RIGHT, 0, 0, PA_PRINT);
myDisplay.displayAnimate(); // Update the display
counter--;
}
}
digitalWrite(relayPin, LOW); // Deactivate the relay to stop the motor
motorRunning = false;
// Clear the display after the countdown is complete
myDisplay.displayClear();
myDisplay.displayText("AQQASHLARI TEMiZLE, CEMi 20 QEPiYE ", PA_LEFT, 65, 0, PA_SCROLL_LEFT);
myDisplay.displayAnimate(); // Update the display
}