I've got a question regarding the use of Millis() on a Leonardo Arduino. I'm currently using it to time 4 seconds elapsed while sending a serial message every second. These messages play a sound. On the fourth message the sound is different to signify that the Arduino has started an event.
I'm running into an issue where the sounds are occasionally being played sporadically without a consistent 1 second between them. I'm wondering if there is a better way of doing what I'm doing? During the timing the Arduino is doing nothing else, and the receiving computer is also generally not doing anything taxing. Is this even an issue from the Arduino side? My receiving program is written in C# using System.IO.Ports.SerialPort.
The reason this is done through Arduino is because the event started by the Arduino needs to correspond exactly to the sounds, though if this remains unresolved I will need to rework this.
Edit: Code as requested.
void setup()
{
//Setup Code
}
void loop()
{
//Code that handles other serial messages and various states
if (isSpecificState == true)
onSpecificState();
}
void onSpecificState()
{
//Setup to handle state
long holdStart = millis();
long holdDuration = 0;
boolean print1 = false;
boolean print2 = false;
boolean print3 = false;
boolean print4 = false;
while(holdDuration <= 4000)
{
if (holdDuration >= 4000 && !print4)
{
Serial.println("tt_sound:2");
print4 = true;
}
else if (holdDuration >= 3000 && !print3)
{
Serial.println("tt_sound:1");
print3 = true;
}
else if (holdDuration >= 2000 && !print2)
{
Serial.println("tt_sound:1");
print2 = true;
}
else if (holdDuration >= 1000 && !print1)
{
Serial.println("tt_sound:1");
print1 = true;
}
holdDuration = millis() - holdStart;
}
//Handle state
}
As I mentioned in the comments, the Arduino is doing nothing else while the timing is happening.
2 Answers 2
There is only a very slight chance that you meet the holdDuration >= 4000
condition in the while loop. Only when holdDuration will be exactly 4000 the while loop will be executed AND the subsequent test in the if statement will pass.
The serial print commands can easily take long enough to have millis()
jump from a number less than 4000 to a number higher than 4000 in a single pass of the while loop.
To prove this assumption, increase the while test to holdDuration <= 4100
and see if the intermittent effect goes away.
Thoughs: Unfortunately I have been unable to get to the bottom of this issue. I noticed the problem tends to happen the first time the interaction starts after the computer and Arduino are idle.
Without knowing too much about how the serial communication is built I'm guessing that without traffic the connection is put to sleep, or generally inactive, and the sound delay is a result of the connection needing to re-initialize or something of that nature. I cant recall a time when the issue occurred and I had a recent correct previous interaction. Once it works any immediate retrial will also behavior correctly.
Solution: I have since moved my timing/sound playing component within my C# app. The Arduino now signals the start of the timer, and then end of the timer with an immediate start to my event.
if
, typically that firstif
should be located after thewhile
loop. Important question: what are the types ofholdDuration
andholdStart
?millis()
is actuallyunsigned long
but that is probably not the issue unless you are running for more than 24 days.