Go Down

Topic: Change/Rotate string every N seconds (Read 409 times) previous topic - next topic

Abfahrt

I'm trying to find the best way to change a string message after some seconds. What I'm trying to achieve is show message A for 5 seconds, then message B for another 5 seconds. How can I approach this on the Arduino?

holmes4

Strange this may sound but blink without delay is the answer!.

Mark

HazardsMind

You can store the messages in an array and just cycle through it.

void loop() {
String message[5] = { "Hello, ", "how ", "are ", "you ", "today?"};
for(int data = 0; data < 5; data++) {
 Serial.print(message[data]);
 delay(5000); // 5 second delay
 }
Serial.println();
}

Simple and dirty, but it should work.
However, this will block everything until it is done, so look into the blink with delay example.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

Abfahrt

I should have been more accurate with my original description! I actually want to execute different functions at those intervals. Let's call them A(), B() and C(). How can I do this? One quick answer based on HazardsMine's answer might be using function pointers, but I have no past experience.

Based on the Blink (no delay) sketch, how can I do this? I understand that each method will be executed after N seconds but how do I cycle between them? I guess that they will overlap, which is something I don't want.

holmes4

Quote
Based on the Blink (no delay) sketch, how can I do this? I understand that each method will be executed after N seconds but how do I cycle between them? I guess that they will overlap, which is something I don't want.


Then look at FSM's (finite state machines) in the playground.

Mark

HazardsMind

If you have any code your already working on, please post it. And please put it in code tags. Look for the # symbol in the tool bar.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

Go Up