Clock

Here’s a quick digital clock in Processing. It also sends the time out a serial port. I use this for testing when I need for a microcontroller or other serial device to receive a string.

/*
  Clock

 Draws a digital clock in the center of the screen
 and sends the string out the serial port.

 created 7 Oct 2008
 by Tom Igoe
 */

import processing.serial.*;

Serial myPort;    // instance of the serial library

void setup() {
  // open the applet window:
  size(300, 200);
  // initialize the font for text display:
  PFont myFont = createFont(PFont.list()[0], 24);
  textFont(myFont);
  // align all text center:
  textAlign(CENTER);
  // open a serial port:
  myPort = new Serial(this, Serial.list()[0], 9600);
}

void draw(){
  // clear the screen:
  background(0);
  // make a string of hour: minute:second.  Use nf()
  // to format the numbers in two digits each:
  String theTime = nf(hour(), 2) + ":" + nf(minute(), 2) + ":"+ nf(second(), 2);
  // draw it in the middle of the screen:
  text(theTime, width/2, height/2);
  // send it out the serial port:
  myPort.write(theTime + "\n");
  // wait one second before doing it again:
  delay(1000);
}