// UBW Microcontroller Analog Data Acquisition ;
//
//
jcl...@ieee.org 2013-06-10 Enhancements for use with UV Monitor from Amersham Biosciences
import processing.serial.*;
Serial ubw; // USB microcontroller communication
int loops = 0;
int[] xvals;
void setup() {
String ubwVersion;
boolean found;
float tdelay;
found = false;
size(1000,500);
background(0); // black
frameRate(10); // sets draw rate
stroke(255); // white
xvals = new int[width];
try {
println(Serial.list());
ubw = new Serial(this, Serial.list()[2], 9600); // serial rate doesnt matter
tdelay=millis() + 5000;
// Read firmware version from connected UBW Microcontroller; jcline 2009-03
for (int i=0; millis() < tdelay ; i++) {
// Get+Verify UBW version string, print to console
ubw.write("v\n");
delay(100);
ubwVersion = ubw.readStringUntil('\n');
if (ubwVersion != null) {
if (ubwVersion.startsWith("UBW", 0)) {
println("Found UBW attached to USB:\n");
println(ubwVersion);
found = true;
break;
}
}
}
}
catch (Exception e) {
println("device access error\n");
}
if (found != true) {
println("No UBW found, exit\n");
exit();
}
else {
ubw.write("C,255,255,255,1\n");
ubw.write("CU,1,0\n");
ubw.write("T,200,1\n"); // set timered update in millisec, A=1
ubw.clear(); // flush
}
}
// Microchip's firmware uses %4u output for output of analog data values
// Convert "0023" string to Int 23
Integer string4uToInt(String s) {
char a[] = s.toCharArray();
Integer i = 0;
Integer v;
// char array into positional math based on ASCII value
v = byte(a[0]) - 48;
i += v * 1000;
v = byte(a[1]) - 48;
i += v * 100;
v = byte(a[2]) - 48;
i += v * 10;
v = byte(a[3]) - 48;
i += v;
return (i);
}
// Read analog data depending on framerate & draw graphical level indicator
void draw() {
String cmd;
String data;
Integer val;
loops++;
if (loops > 20000) {
noLoop();
ubw.write("T,0,1\n"); // Turn off timered updates
println("done\n");
ubw.stop();
}
data = ubw.readStringUntil('\n'); // read timered update
if (data == null) {
return; // no periodic data yet
}
if (data.startsWith("A,", 0) == false) {
println("bad input: "+data+"\n");
return;
}
val = string4uToInt(data.substring(2, data.length()));
println(val);
val = val/5;
for(int i=1; i<width; i++) {
xvals[i-1] = xvals[i];
}
stroke(255, 0, 0);
xvals[width-1] = height - val;
background(248, 248, 248);
for(int i=4, sum = 0, sumLast = 0; i<width; i++) {
//point(i, xvals[i]-1);
sum = xvals[i];
sum += xvals[i-1];
sum += xvals[i-2];
sum += xvals[i-3];
sum += xvals[i-4];
sum /= 5;
//line(i, xvals[i], i, xvals[(i-1)]);
point(i, xvals[i]);
line(i, sum, i, sumLast);
line(i, sum+1, i, sumLast+1);
sumLast = sum;
}
}