I am planning to power the WIO from the connector on the rear (Pin 2 + 5v. Pin 6 - Gnd). I have prototyped the connection and it seems to work OK.
UPDATE --------------------
If I use a USB power supply connected to the WIO or provide 5 volts via the 40 pin adaptor the RTC clock slows down by a factor of 2.
After further investigation I have found that the issue it is NOT to do with the power supply as I earlier thought. It is to do with the Serial connection.
long waitTime = millis() + SERIAL_WAIT;
Serial.begin(SERIAL_BAUD);
while (!Serial) {
if (millis() > waitTime) {
break;
}
}
I added a time out because it waits forever if nothing reads the serial buffer.
If the above code times out then the issue is there. If it does not time out then it is OK.
If it is connected to my PC and The Arduino Serial monitor is active it counts time normally.
There are absolutely NO Serial.print (or any other) lines of code in my main loop. All output is to the display. I do have a Single Serial.println() in my setup code.
UPDATE --------------------
I have a loop that displays the time from the RTC adjusted from the NTP time server. The loop displays the time every second.
Can you please explain what is going on and how I can correct this.
Code Snippet (setup()):
waitTime = millis() + NTP_WAIT;
devicetime = getNTPtime();
while (devicetime == 0) {
delay(500);
devicetime = getNTPtime();
if (millis() == 0) {
fail("NTP returned ZERO");
}
}
DateTime now = DateTime(F(__DATE__), F(__TIME__));
rtc.adjust(now);
now = rtc.now();
rtc.adjust(DateTime(devicetime));
Code Snippet (loop()):
delay(1000);
now = rtc.now();
String s = (String(now.hour()) + ":" + String(now.minute()) + ":" + String(now.second()));
tft.drawString(s, 0, 0);
Please help
Stuart
Hi @stuartdd
I think it might be the while(!Serial)
, this is the mechanism of Arduino where you have to manually open the serial monitor for the code to be continued.
You can use something like Serial.available()
to check incoming buffer.
Hi.
This happens if I don’t have any Serial output. In fact it happens if I don’t use Serial at all,
I wrote a small program to demonstrate.
Plug it in to your PC to upload it.
Unplug the WIO and plug it into a wall socket phone charger and watch time slow down!
#include "TFT_eSPI.h"
#include "RTC_SAMD51.h"
#include "DateTime.h"
#define SCREEN_W 320
#define SCREEN_H 240
#define TIMER_DISPLAY 1000
const int xBlock = (SCREEN_W - 20) / 8;
const int yBlock = (SCREEN_W - 20) / 6;
const int xOfs = 10;
const int yOfs = 10;
RTC_SAMD51 rtc;
DateTime now;
TFT_eSPI tft;
const int xOffsets[] = {xOfs, xOfs + xBlock, xOfs + (xBlock * 2), xOfs + (xBlock * 3), xOfs + (xBlock * 4), xOfs + (xBlock * 5), xOfs + (xBlock * 6), xOfs + (xBlock * 7)};
char prev[] = {'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a'};
char next[10];
int bgColor = TFT_BLUE;
bool colonIsOn = false;
long millisNow = 0;
long timerDisplay = 0;
void setup() {
tft.begin();
rtc.begin();
DateTime now = DateTime(F(__DATE__), F(__TIME__));
rtc.adjust(now);
now = rtc.now();
initScreen(TFT_BLUE, TFT_WHITE);
}
void loop() {
millisNow = millis();
if (millisNow > timerDisplay) {
timerDisplay = millisNow + TIMER_DISPLAY;
now = rtc.now();
(pad(now.hour()) + (colonIsOn ? ":" : " ") + pad(now.minute()) + (colonIsOn ? ":" : " ") + pad(now.second())).toCharArray(next, 9);
for (int i = 0; i < 8; i++) {
if (next[i] != prev[i]) {
tft.fillRect(xOffsets[i], yOfs, xBlock - 1, yBlock, bgColor);
tft.drawChar(next[i], xOffsets[i], yOfs);
prev[i] = next[i];
}
}
colonIsOn = !colonIsOn;
}
}
String pad(int n) {
if (n > 9) {
return String(n);
}
return "0" + String(n);
}
void initScreen(int bg, int fg) {
bgColor = bg;
tft.setRotation(3);
tft.fillScreen(bgColor);
tft.setTextColor(fg);
tft.setTextSize(7);
}