// Paul Brace Feb 2021
// Script to accept millis() from Arduino
// and compare it to internal millis() to
// assess inaccuracy of the Arduino clock.
// Assumes that the computer clock is accurate
// -ve = Arduino is running slow so enter as a +ve adjustment in the clock program
// +ve = Arduino is running fast so enter as a -ve adjustment to slow the clock
down
import [Link].*;
Serial theSerialPort; // create the serial port object
int[] serialBytesArray = new int[15]; // array to store incoming bytes
int bytesCount = 0; // current number of bytes received
boolean init = false; // false until handshake completed by
receiving the character Z
int fillColor = 255; // defining the initial fill colour
long mills = 0; // last reading received
long first = 0; // time of first mills received so we can
calculate the difference over an hour
long now; // number of millis elapsed since first
mills received
long firstReading = 100000; // millis() in processing of first message
received from Arduino
long DiffPerHour = 0; // the difference after the first hour has
passed
int inByte; // last byte read
void setup() {
// define some canvas and drawing parameters
size(500, 500);
background(70);
noStroke();
// print the list of all serial devices so you know which one to set for the
Arduino
// will need to run program and edit if the correct port is not set below
printArray([Link]());
// instantate the Serial Communication
String thePortName = [Link]()[1];
theSerialPort = new Serial(this, thePortName, 9600);
}
void draw() {
// Display time settings
background(70);
fill(fillColor);
textSize(25);
text(hour() + ":" + minute() + ":" + second(), 50, 50);
// the last read millis sent by the Arduino
text("Incoming elapsed: " + mills, 50, 100);
// the current elapsed since first read in Processing
text("Local elapsed: " + (now - firstReading), 50, 150);
// display the current difference
text("Diff: " + (mills - (now - firstReading)), 50, 200);
// Check if 1 hour has passed and if the first hour store the difference
if (((now - firstReading)>= 3600000) && (DiffPerHour == 0)){
DiffPerHour = mills - (now - firstReading);
}
// Display the first difference and the difference after the first hour
text("Diff after 1 hour: " + DiffPerHour, 50, 300);
}
void serialEvent(Serial myPort) {
// read a byte from the serial port
inByte = [Link]();
if (init == false) { // if not yet handshaked the see if handshake byte
if (inByte == 'Z') { // if the byte read is Z
[Link](); // clear the serial port buffer
init = true; // store the fact we had the first hello
[Link]('Z'); // tell the Arduino to send more
if (first == 0){
first = millis();
}
}
}
else {
// if there already was the first hello
// Add the latest byte from the serial port to array
if (inByte != 69) { // Check not the end of message character E
if (bytesCount < 14) {
serialBytesArray[bytesCount] = inByte;
bytesCount++;
}
}
if (inByte == 69) {
// End of message
// store local time elapsed
now = millis();
// calculate incoming millis()
mills = 0;
for (int i = 1; i <= bytesCount; i++) {
mills += (serialBytesArray[i - 1] - 48) * pow(10, (bytesCount - i));
}
// Say we are ready to accept next message
// if this is the first reading then set the first difference
if (firstReading == 100000) {
firstReading = now;
}
[Link]('Z');
// Reset bytesCount:
bytesCount = 0;
}
}
}