Skip to content

Instantly share code, notes, and snippets.

@madeintaiwan
Created September 2, 2013 09:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madeintaiwan/6410995 to your computer and use it in GitHub Desktop.
Save madeintaiwan/6410995 to your computer and use it in GitHub Desktop.
This one needs some tweaking but works well in terms of simple data output and logging. Ideally I'd like a linear graph rather than plots
// Processing_Display_Analog_Signal_Fuller.pde
// Graphing sketch for multiple analog signals
// This program takes ASCII-encoded strings from the serial port at 9600 baud
// and graphs them. It expects values in the range 0 to 1023, followed by a newline
// Version 19 Dec 2010
// by Dr. Lynn Fuller, Professor, Microelectronic Engineering, Rochester Institute of Technology
// This code is in the public domain.
import processing.serial.*;
Serial myPort; // The serial port
int xPos = 1; // horizontal position of the graph
PrintWriter output;
void setup () {
output=createWriter("Output_Data_File.txt");//output file name in sketch folder
// set the window size, define (width - 0 on left, height - 0 on top):
size(500,400);
// List all the available serial ports
println(Serial.list());
//Open whatever port is the one you're using. It is COM3 on my computer
myPort = new Serial(this, Serial.list()[2], 9600);//The [2] means COM3
// don't generate a serialEvent() unless you get a newline character:
myPort.bufferUntil('\n');
background(204);// set inital background color, 0=black, 255=white, 204=gray
loadFont("Arial-BoldMT-36.vlw"); // Load Font used on graph
}
void draw () { ;
// make axis, color is set by stroke(v1), v1=0 is black, or stroke(R,G,B)
stroke(0,0,0); // Black
strokeWeight (0); // thin line for x and y axis
line(0,height/2,width,height/2); // x-axis, line(x1,y1,x2,y2)
line(width/2,0,width/2,height); // y-axis, line(x1,y1,x2,y2)
int tics=10; // tic marks, tics is the number of tic marks on y axis
for (int k=0; k<tics; k=k+1) {
line(width/2-5,k*height/tics,width/2+5,k*height/tics);
line(k*width/tics,height/2-5,k*width/tics,height/2+5);
int Scale=5000/tics;// Full scale is 5 volts or 5000 mV for Arduino A to D
// Scale in mV / div
fill(0,0,250);// Blue
text(" mV / div ",width/2+5, height-10);
text(Scale,width/2-30,height-10);// print vertical scale on graph
int pix=5;// increment the horizontal position by "pix" pixels after reading data pts
text(" sec / div",width-50,height/2-10);
text(width/pix/tics,width-65,height/2-10);
text(month()+" / "+day()+" / "+year(),width-125,height-10);// Date Stamp
text(hour()+" : "+ minute()+" : "+second(),width-125,height-20);// Time Stamp
}
}
// everything happens in the serialEvent()
// The data collection/display rate is set by the delay in the Arduino code
int N=4; //the number of different analog signals to plot, can be up to 6
int i=0;
void serialEvent (Serial myPort) {
String inString = myPort.readStringUntil('\n');// get the ASCII string
if (inString != null) {
inString = trim(inString);//trim off any whitespace
// convert to an int and map to the screen height
float inByte = float(inString);
inByte = map(inByte, 0, 1023, 0, height);
fill(204); // same color as background color
rect(50+(width/N)*i,30,60,10); //blank out previous displayed number
stroke(253/(N-1)*i,253/(N-1)*i,253/(N-1)*i);
fill(253/(N-1)*i,253/(N-1)*i,253/(N-1)*i);
// give names for plots
String [] names={"Fixed ","Force ","Temperature ","Variable "};
print("A"+i+" "+names[i]+" ");//print to dialog box
println(height-inByte);//print to dialog box
output.print("A"+i+" "+names[i]+" ");//print to file
output.print(month()+" "+day()+" "+year());// Date
output.print(" "+hour()+" : "+ minute()+" : "+second());// Time
output.println(height-inByte);//print to file plus new line
if (keyPressed == true){
output.flush();// writes the remaining data to the file
output.close();// Finishes the file
exit ();
}
// print text ( names, x position, y position)
text(names[i],50+(width/N)*i,20);
text(inByte*5000/height,50+(width/N)*i,40);
//draw rectangle data point at x=xPos, y=height-inByte, size=2x2 pixels
rect(xPos,height-inByte,2,2);
if (i<N-1){
i=i+1;
}
else {
i=0;
int pix=2;// increment horizontal by "pix" pixels after reading data pts
xPos=xPos+pix;
}
}
// at the edge of the screen, go back to the beginning:
if (xPos >= width) {
xPos = 0;
background(204);
}
}
// all done with processing code
/*
//Arduino code "ArduinoAnalogSendFuller.pde"
// Reads multiple analog signals connected to inputs A0...A3
// Sends ASCII-encoded strings out the USB serial port at 9600 baud
// Created 4 Dec 2010
// Updated 11 Dec 2010
// by Dr. Lynn Fuller, Professor,
// Microelectronic Engineering, Rochester Institute of Technology
// This code is in the public domain.
void setup() {
// initialize the serial communication:
Serial.begin(9600);
}
void loop() {
// send the value of analog input 0:
Serial.print(analogRead(A0));
// to send as an array [A0,A1,A2,A3]
// use Serial.print(' ')between reads and Serial.println( )at end;
// send the value of analog input 1:
Serial.println( );
Serial.print(analogRead(A1));
Serial.println( );
// send the value of analog input 2:
Serial.print(analogRead(A2));
Serial.println( );
// send the value of analog input 3:
Serial.print(analogRead(A3));
Serial.println( );
// wait a bit for the analog-to-digital converter to finish the last read
// to stabilize after the last reading:
// use the serial monitor 7th icon to see what sent
//the delay below sets how often the analog data is sent (in milli-seconds)
delay(100);
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment