Hello, I'm having some unexpected behaviour with the millis() function with the Adafruit 32u4 basic feather.
I'm using the following code based on the tutorials, as a test of this function:
const unsigned long interval = 5000;
unsigned long startMillis;
unsigned long currentMillis;
void setup() {
while (!Serial){;}
Serial.begin(9600);
Serial.println("Millis Test");
startMillis = millis();
}
void loop() {
currentMillis = millis();
if (currentMillis - startMillis >= interval){
Serial.print("Time ");
Serial.println(currentMillis);
startMillis = currentMillis;
}
}
If my interval is less than 2000, I get the expected behaviour, i.e. the time gets printed out to the serial monitor. For anything greater than 2700 it prints out maybe once and then nothing else.
If I add a Serial print with a delay inside the loop() section, it works fine, but only if the print is there:
const unsigned long interval = 5000;
unsigned long startMillis;
unsigned long currentMillis;
void setup() {
while (!Serial){;}
Serial.begin(9600);
Serial.println("Millis Test");
startMillis = millis();
}
void loop() {
currentMillis = millis();
if (currentMillis - startMillis >= interval){
Serial.print("Time ");
Serial.println(currentMillis);
startMillis = currentMillis;
}
Serial.println("Pass");
delay(1000);
}
I want to be able to have a long delay period (60sec) using millis() rather then using delay() as I plan to connect the 32u4 Feather to a GPS breakout.
I have also verified that the first code above works fine with an Uno, so its in some way specific to the 32u4 Feather.
Does that mean its a hardware issue with my board? I had thought it was a programming thing related to the Serial.print as the second code gives sort-of the expected behaviour...
I ordered an Arduino Micro (which uses the same 32u4 chip as the feather) and tried my millis() code again.
I get the same behaviour as before - for an interval value >= 2700, nothing gets printed out to the Serial port.
However, as the Micro has a RX and TX LEDs, I can see some different behaviour that I couldn't before:
When the interval is <=2000, TX flashes, and output appears
When the interval is >2000 and <2700, RX randomly flashes, TX flashes, and sometimes there is output to serial, but not at the correct interval. Sometimes an interval will get skipped, or a few microseconds get added. This gets worse as the interval is increased closer to 2700.
When the interval is =>2700, RX turns on, TX LED flashes, then both turn off, but no serial output
I tried your first code as well on a Micro (actually a What's Next Pink) and it works as expected. Modified the interval (1500, 2200, 5000 (original)) and all behave correctly (expected times printed).
Which OS
Which IDE version
Which board manager version
//Edit
Using Windows 10, IDE 1.8.5 and Boards 1.6.21
Yes, the board manager versions 1.8.2 and 1.6.21 mentioned are for the Arduino AVR for the Arduino Micro I tested earlier. My Adafruit AVR version is 1.4.13.
I have now tested my code on Windows 10 using the online IDE and it works as expected.
There must be something wrong with my IDE install on Ubuntu, so I'll try and reinstall and see if that works.
Have a look at this: https://www.arduino.cc/en/Serial.IfSerial.
You might be the only one that has the test for !Serial and the Serial.begin() in a different order. I don't understand that it worked with others, but at least fix it !
Sometimes a bad quality USB cable can cause a lot of trouble. For example when the voltage for the Arduino board is too low. Try another USB port on the computer with an other USB cable.
The problem might that the board is a "Adafruit" version. Adafruit and Sparkfun have sometimes a different bootloader and you should add the specific boards for those to the Arduino IDE.
You could also burn the Arduino bootloader on it, and use it as a Arduino board.
Did you add the additional boards as told here: Arduino IDE Setup | Adafruit Feather 32u4 Basic Proto | Adafruit Learning System.