by Jamie Allen Feb 1st, 2007
This is Processing code (Arduino code commented in at the bottom) for graphing a single piece of data. This code updates Tom's original example by simply scaling the data output based on the size of the screen.
This code might be a good starting point for Sensors and Time assignments, among other things.
/*
Datalogger based on Tom Igoe's
by Jamie Allen 2007
This program takes raw bytes from the serial port at 9600 baud and graphs them.
To start/stop the graph, click the mouse.
Created 20 April 2005
Updated 5 July 2005
*/
import processing.serial.*;
Serial myPort; // The serial port
PFont myFont; // The display font:
// initial variables:
int i = 1; // counter
int inByte = -1; // data from serial port
int wrote = 0;
int offset = 0;
int offsettext = 25;
void setup () {
size(500, 500); // window size
myFont = loadFont("Technine-18.vlw");
textFont(myFont, 16);
textAlign(CENTER);
textMode(SCREEN);
fill(#E9FF5B, 200);
// set inital background:
background(#000000);
strokeCap(ROUND);
strokeWeight(3);
// List all the available serial ports
println(Serial.list());
// I know that the third port in the serial list on my mac
// is always my Keyspan adaptor, so I open Serial.list()[2].
// Open whatever port is the one you're using.
myPort = new Serial(this, Serial.list()[4], 9600);
}
void draw () {
if (myPort.available() > 0) {
inByte = myPort.read();
serialEvent();
}
}
void serialEvent () {
// draw the line:
smooth();
stroke(#FF7B00+inByte,i/3);
line(i, height, i, (height - height*(inByte/255.0))+20);
// at the edge of the screen, go back to the beginning:
if (i > width) {
i = 0;
background(#000000);
}
else {
i++;
}
//only display the value once every 25 readings
//just to keep things clean
if (wrote > 27)
{
text(inByte, i, (height - height*(inByte/255.0))+20);
wrote = 0;
}
wrote++;
}
/*
//a simple program to spit data out from Arduino
//to the above graphing program in Processing
int sensorPin = 5; // select the input pin for the sensor
int ledPin = 13; // select the pin for the LED
int val = 0; // variable to store the value coming from the sensor
void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
digitalWrite(ledPin, HIGH); // sets the LED on
val = analogRead(sensorPin); // read the value from the sensor
//Serial.print("Raw Value: ");
//Serial.println(val, DEC); //send as a ASCII encoded decimal
Serial.print(val/4, BYTE); //send as a single byte - a single 8-bit value between 0-255 (1023/4=255)
delay(20);
}
*/