/* This code can be run using Processing software. Download from their site at www.processing.org Any additional documentation can also be found on thier website */ import processing.serial.*; Serial port; PrintWriter output; float t = 0; //time variable int x = 0; //x coordinate float y = 0; //y coordinate float LED = 0; //LED status int flip = 1; //flips the color of the graph float raw; //raw value between 0-1023 (obtained from read value) float v = 0; //converted voltage value between 0-5V (obtained from read value) void setup() { /* size is set to any reasonable value, framerate is 60 hz (higher = more data points per second) set background to white, and setup port that reads values at 9600 baud and set the filename in the createWriter function. Serial.list()[0] takes the first COM port that is available which is assumed to be the arduino. Replace with the COM port of arduino if it gives you trouble. The output is a class of createWriter and can output any type of file, but most success comes from text files */ size(500,300); frameRate(60); background(255); port = new Serial(this, Serial.list()[0], 9600); port.bufferUntil('\n'); output = createWriter( "control.txt" ); output.println("Time(s) , Raw , Voltage(V) , LED status"); } void draw() { //first line drawn clears the previous value with a white line stroke(255); line(x,0,x,height); //flip is (+/-) 1. line color will either be green or blue depending if the end of the graph was reached //(graph alternates blue/green with every iteration). stroke(0,255*flip,255*(-flip)); line(x,height-y,x,height); //rectangle clears the top section to show the input as a voltage value, and then text displays the data stroke(255); fill(255); rect(0,0,width,32); fill(255,0,0); textSize(20); text(v + "V",10,20); if(LED == 0){ fill(255,0,0); text("LED: OFF",width - 100,20); } if(LED == 1){ fill(0,255,0); text("LED: ON",width - 100,20); } //output function writes to a text file. Will show the time in seconds, raw value, voltage value, and LED state output.println(t + " , " + raw + " , " + v + " , " + LED); //if the graph has reached the end, then go back to the beginning and change the color (easier to track visually) if(x < width){ x=x+1; } else { x=0; flip = flip*-1; } //of course, incriment the timer. Assumes 60Hz framerate t = t+(1.0/60.0); } //necessary function for reading values from arduino COM port void serialEvent(Serial port) { raw = float(port.readStringUntil('\n')); v = raw*(5.0/1023.0); y = map(raw,0,1024,0,height-32); } //THIS IS IMPORTANT. When you are done, press enter when your mouse is inside the graph. This function safetly exits //the code and saves any remaining data in the buffer. YOU MAY LOSE DATA IF YOU DON'T DO THIS. void keyPressed() { output.flush(); output.close(); port.write(0); //turns off the LED for safety exit(); } //toggles the LED when the mouse is pressed void mousePressed() { if (LED == 0) { LED = 1; port.write(1); } else if (LED == 1) { port.write(0); LED = 0; } }