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() {
Serial.begin(9600);
}
void loop() {
currentTime = millis();
if (currentTime == timeArray[0]) {
print_time(currentTime);
Serial.println("Primera");
};
if (currentTime == timeArray[1]) {
print_time(currentTime);
Serial.println("Segunda");
};
if (currentTime == timeArray[2]) {
print_time(currentTime);
Serial.println("Tercera");
};
if (currentTime == timeArray[3]) {
print_time(currentTime);
Serial.println("Cuarta");
};
if (currentTime == timeArray[4]) {
print_time(currentTime);
Serial.println("Quinta");
};
}
void print_time(unsigned long currentTime) {
Serial.print("Time: ");
currentTime = millis();
Serial.print(currentTime / 1000);
Serial.print("s - ");
}
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 ?
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.