/* * Application to test battery life * by calculating the time until serial * input is no longer detected. * * by Rob Faludi * http://rob.faludi.com */ import processing.serial.*; ////// CHANGE THIS VALUE FOR EACH TEST //// String titleText = "Arduino XBee 9V NiMH 170 mAhH"; ////////////////////////////////////////// String version = "1.03"; String startTimeText, startTimeCoded, lastTimeText; Serial port; int thisByte; long startTime, lastUpdate; PFont theFont; boolean firstContact = false; // used to ensure that timing starts from the first received serial event void setup() { size(700, 400); // Print a list pf ports //println("Available serial ports:"); println(Serial.list()); port = new Serial(this, Serial.list()[2], 9600); // SET THIS TO BE THE APPROPRIATE SERIAL PORT, USING THE LIST PRINTED TO DEBUG WINDOW theFont = loadFont("AmericanTypewriter-48.vlw"); textAlign(CENTER); } void draw() { background(245); fill(0); textFont(theFont,36); textAlign(CENTER); text(titleText, width/2, height/5); // display the current run time text("Run Time: " + (lastUpdate - startTime)/1000/60/60 + " hrs, " + ((lastUpdate - startTime)/1000/60)%60 + " mins, " + ((lastUpdate - startTime)/1000)%60 + " secs" , width/2, height/2); textFont(theFont,18); // show when the first and most recent serial event happened text("Started: " + startTimeText + " Last Update: " + lastTimeText, width/2, height/5*4); textAlign(LEFT); textFont(theFont,12); text("Last byte: " + str(thisByte),2,height-2); textAlign(RIGHT); textFont(theFont,12); text("Battery Tester v" + version + " rob.faludi.com",width-2,height-2); } void serialEvent(Serial port) { String minuteTxt, secondTxt; // prep for leading zeros on minute and second text strings if(minute()<10) { minuteTxt = "0"+minute(); } else { minuteTxt = str(minute()); } if(second()<10) { secondTxt = "0"+second(); } else { secondTxt = str(second()); } // if this is the very first serial event, mark the start time if (firstContact == false) { startTimeText = str(hour()) + ":" + minuteTxt + ":" + secondTxt + " " + str(month()) + "/" + str(day()) + "/" + str(year()); startTimeCoded = str(year()) + str(month()) + str(day()) + str(hour()) + str(minute()) + str(second()); startTime = millis(); lastUpdate = millis(); firstContact = true; } // read a byte and note the time thisByte = port.read(); // read a byte from the serial buffer println(thisByte); lastUpdate = millis(); // note the time and create a text string using this time lastTimeText = str(hour()) + ":" + minuteTxt + ":" + secondTxt + " " + str(month()) + "/" + str(day()) + "/" + str(year()); // save a record of this event to a text file, in case it's the last serial event // this file gets overwritten every time there's a new serial event String[] joinedStrings; joinedStrings = new String[5]; joinedStrings[0] = titleText; joinedStrings[1] = "Battery Tester v" + version + " rob.faludi.com"; joinedStrings[2] = "Run Time: " + (lastUpdate - startTime)/1000/60/60 + " hrs, " + ((lastUpdate - startTime)/1000/60)%60 + " mins, " + ((lastUpdate - startTime)/1000)%60 + " secs"; joinedStrings[3] = "Started: " + startTimeText; joinedStrings[4] = "Last Update: " + lastTimeText; saveStrings("data/logs/" + titleText + "-" + startTimeCoded + ".txt", joinedStrings); }