Pages: [1]   Go Down
Author Topic: Change/Rotate string every N seconds  (Read 329 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 2
Posts: 85
(╯°□°)╯︵ ┻━┻
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 50
Posts: 2199
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Mark
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 84
Posts: 3400
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Offline Offline
Jr. Member
**
Karma: 2
Posts: 85
(╯°□°)╯︵ ┻━┻
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 50
Posts: 2199
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 84
Posts: 3400
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Pages: [1]   Go Up
Jump to: