Millis problem

Hello, I am working on a project and I did this simple trial to verify if the combination between Array and millis could work for my project. Everything looked good except for the fact that Serial.print is printing each word +- 4 times instead of print it only one, in my sketch, when currentTime == timeArray[indes number] I'm using an arduino UNO R3. Any idea is more than welcome. Thank you very much

unsigned long currentTime;
unsigned long timeArray[] = {1000UL, 2000UL, 15000UL, 30000UL, 400000UL,};

void print_time(unsigned long currentTime);
void setup() {
void loop() {
  currentTime = millis();
  if (currentTime == timeArray[0]) {
  if (currentTime == timeArray[1]) {
  if (currentTime == timeArray[2]) {
  if (currentTime == timeArray[3]) {
  if (currentTime == timeArray[4]) {
void print_time(unsigned long currentTime) {
  Serial.print("Time: ");
  currentTime = millis();
  Serial.print(currentTime / 1000);
  Serial.print("s - ");

Arduino fast, serial slow.
The condition persists for multiple iterations of loop.

Things happen very, very fast in Arduino land.

For example when: if (currentTime == timeArray[0]) { evaluates, it will continue to do so until ‘currentTime’ is no longer equal to ‘timeArray[0]’.

What exactly do you want to happen?

The Arduino can repeat the loop function many many times during the millisecond that the if statement is true.

It’s also not good to look for an exactly equal time. Once the program gets larger you may not make it to that statement on exactly the right millisecond.

Your method is flawed The loop() function repeats so quickly that the test of the current millis() value will be satisfied many times, hence the multiple printout. Even if the method worked the messages will only be printed once then, when millis() increases beyond 40 seconds no more printing will occur. Is that what you want ?


Your younger look isn’t quite right.

Liked your old face better :wink: .

Thank you all for your swift answers. Really I didn't think about the fact that all of you remark concerning the speed and constant evaluation. Answering to larryd, "What exactly do you want to happen?" I want that at each index time something happens but can't work for me with this repetition issue. The final project are 3 or 4 motors that change direction, start and stop synchronized with a music track. Arduino control the motors and trigger the track. It's not necessary a super precise time code synchronicity, that's why I go confidently into millis, simple and with enough accuracy for my project. UKheliBob I know that in this sketch beyond 40 seconds no more printing will occur. It short time list only for a trial. A failed trial.

Thank you a lot for your comments.

Take a look at Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE.