Regarding time.h, more specific communication between Arduino IDE and processing

Hello everybody. I have been tearing my brains out the last hours getting this to work. Google hasn't been of much help.

There is an example included in time.h to establish two way communication between the Arduino IDE and processing.
The files are TimeSerial.ino and the processing counterpart is SyncArduinoClock.pde and its purpose is to feed the computer clock (date, time) from processing to arduino.

My problem is opening the serial port (in my case COM7) in both the IDE and processing. When i try to do so either way i get an error message saying that the COM-port is busy. My suspicion is that the program might be written for a slightly different version of either program, but i cant seem to pin it down (This is my second day programming anything... So it may well be trivial).

I'll post you my code:

IDE version: 1.0.5-r2

TimeSerial.ino

/*

  • TimeSerial.pde
  • example code illustrating Time library set through serial port messages.
  • Messages consist of the letter T followed by ten digit time (as seconds since Jan 1 1970)
  • you can send the text on the next line using Serial Monitor to set the clock to noon Jan 1 2013
    T1357041600
  • A Processing example sketch to automatically send the messages is inclided in the download
  • On Linux, you can use "date +T%s > /dev/ttyACM0" (UTC time zone)
    */

#include <Time.h>

#define TIME_HEADER "T" // Header tag for serial time sync message
#define TIME_REQUEST 7 // ASCII bell character requests a time sync message

void setup() {
Serial.begin(9600);
while (!Serial) ; // Needed for Leonardo only
setSyncProvider(requestSync); //set function to call when sync required
Serial.println("Waiting for sync message");
}

void loop(){
if (Serial.available()) {
processSyncMessage();
}
if (timeStatus()!= timeNotSet) {
digitalClockDisplay();
}
if (timeStatus() == timeSet) {
}
delay(1000);
}

void digitalClockDisplay(){
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.print(day());
Serial.print(" ");
Serial.print(month());
Serial.print(" ");
Serial.print(year());
Serial.println();
}

void printDigits(int digits){
// utility function for digital clock display: prints preceding colon and leading 0
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}

void processSyncMessage() {
unsigned long pctime;
const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013

if(Serial.find(TIME_HEADER)) {
pctime = Serial.parseInt();
if( pctime >= DEFAULT_TIME) { // check the integer is a valid time (greater than Jan 1 2013)
setTime(pctime); // Sync Arduino clock to the time received on the serial port
}
}
}

time_t requestSync()
{
Serial.write(TIME_REQUEST);
return 0; // the time will be sent later in response to serial mesg
}

My processing version: 2.2.1 (Have also tried 1.5.1 with no luck.)

SyncArduinoClock.pde

/**

  • SyncArduinoClock.
  • portIndex must be set to the port connected to the Arduino
  • The current time is sent in response to request message from Arduino
  • or by clicking the display window
  • The time message is 11 ASCII text characters; a header (the letter 'T')
  • followed by the ten digit system time (unix time)
    */

import processing.serial.*;
import java.util.Date;
import java.util.Calendar;
import java.util.GregorianCalendar;

public static final short portIndex = 0; // select the com port, 0 is the first port
public static final String TIME_HEADER = "T"; //header for arduino serial time message
public static final char TIME_REQUEST = 7; // ASCII bell character
public static final char LF = 10; // ASCII linefeed
public static final char CR = 13; // ASCII linefeed
Serial myPort; // Create object from Serial class

void setup() {
size(200, 200);
println(Serial.list());
println(" Connecting to -> " + Serial.list()[portIndex]);
myPort = new Serial(this,Serial.list()[portIndex], 9600);
println(getTimeNow());
}

void draw()
{
textSize(20);
textAlign(CENTER);
fill(0);
text("Click to send\nTime Sync", 0, 75, 200, 175);
if ( myPort.available() > 0) { // If data is available,
char val = char(myPort.read()); // read it and store it in val
if(val == TIME_REQUEST){
long t = getTimeNow();
sendTimeMessage(TIME_HEADER, t);
}
else
{
if(val == LF)
; //igonore
else if(val == CR)
println();
else
print(val); // echo everying but time request
}
}
}

void mousePressed() {
sendTimeMessage( TIME_HEADER, getTimeNow());
}

void sendTimeMessage(String header, long time) {
String timeStr = String.valueOf(time);
myPort.write(header); // send header and time to arduino
myPort.write(timeStr);
myPort.write('\n');
}

long getTimeNow(){
// java time is in ms, we want secs
Date d = new Date();
Calendar cal = new GregorianCalendar();
long current = d.getTime()/1000;
long timezone = cal.get(cal.ZONE_OFFSET)/1000;
long daylight = cal.get(cal.DST_OFFSET)/1000;
return current + timezone + daylight;
}

Thank you in advance for taking interest :slight_smile:

My suspicion is that the program might be written for a slightly different version of either program, but i cant seem to pin it down

No. What happens when you call a friend, and they are already on phone? You get a busy signal. That's what you are getting here. Only one application at a time can be on each end of the serial port, just like a phone line. And, no, there are no call-waiting or three-way calling options.

Yes, i completely understand your argument, as it was my first thought on the subject. But, when i searched around i found places like these:

https://learn.sparkfun.com/tutorials/connecting-arduino-to-processing#shaking-hands-part-1

describing a two way communication scenario. So am i being trolled, or am i misunderstanding something in these articles?

thanks for replying.

So am i being trolled, or am i misunderstanding something in these articles?

I think that you are the only one that can answer that. \

Processing has a serialEvent() method that you can overload. The Serial class has a readUntil() method that you can use to define when the serialEvent() method is called,

You can use Serial.println() in the Arduino, and port.readUntil() to wait for a line feed or carriage return ('\r' or '/n') (it might be readBytesUntil()), to send data from the Arduino to Processing, and then use print() in Processing (in the serialEvent() method) to see what the Arduino had to say.