xxxxxxxxxx
83
// parameters of the clock:
var clockRadius = 200;
var hourHand = 60;
var minuteHand = 90;
var secondHand = minuteHand;
var handStart = -10;
var currentHour = 12;
var currentMinute = 60;
var currentSecond = 60;
var serial;
var fromSerial = 0
var time = 0
function setup() {
// set the general parameters for drawing:
createCanvas(windowWidth, windowHeight);
smooth();
strokeWeight(2);
strokeCap(ROUND);
serial = new p5.SerialPort();
// serial.on('list',printList);
serial.on('data', serialEvent);
serial.list();
serial.open("/dev/cu.usbmodem146101");
}
function draw() {
background(255); // white background
translate(width / 2, height / 2); // move to the center of the window
rotate(3 * PI / 2); // rotate drawing 270 degrees to get 0 at the top
// draw second hand:
drawHand(second() + currentSecond, '#ccc', secondHand, 60);
// draw minute hand:
drawHand(minute() + currentMinute, '#ace', minuteHand, 60);
// draw hour hand:
drawHand(hour() + currentHour, '#ace', hourHand, 12);
// drawHand(hour(), '#ace', hourHand, 12);
// draw arc from 0 to current second:
push();
stroke("#5597cf"); // set arc color
noFill(); // no fill for the arc
arc(0, 0, clockRadius, clockRadius, 0, second() * PI / 30);
pop();
}
// this function draws a hand, given the unit value, hand color, length,
// and how many divisions in a circle (e.g. minute: 60, hour: 12):
function drawHand(unitValue, handColor, handLength, divisions) {
push();
rotate(unitValue * 2 * PI / divisions); // rotate to draw hand
stroke(handColor); // set line color
line(handStart, 0, handLength + handStart, 0);
pop();
}
function serialEvent() {
// this is called when data is recieved, data will then live in fromSerial
var data = serial.readLine();
if (data != "") {
print(data);
var handInfo = split(data, ",");
if (handInfo[0] == "1") {
currentHour = parseInt(handInfo[1]);
} else if (handInfo[0] == "2") {
currentMinute = parseInt(handInfo[1]);
} else if (handInfo[0] == "3") {
currentSecond = parseInt(handInfo[1]);
}
}
//print(currentHour);
// if (stringFromSerial.length>0){
// var trimmedString = trim(stringFromSerial);
// fromSerial= Number(trimmedString);
// }
}
//function settime (time) {