Advertisement
Guest User

Calamaro_processing

a guest
Feb 14th, 2010
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.64 KB | None | 0 0
  1. // Graphing sketch
  2.  
  3.  
  4. // This program takes ASCII-encoded strings
  5. // from the serial port at 9600 baud and graphs them. It expects values in the
  6. // range 0 to 1023, followed by a newline, or newline and carriage return
  7.  
  8. // Created 20 Apr 2005
  9. // Updated 18 Jan 2008
  10. // by Tom Igoe
  11.  
  12. import processing.serial.*;
  13.  
  14. Serial myPort;        // The serial port
  15. int xPos = 1;         // horizontal position of the graph
  16.  
  17. void setup () {
  18.   // set the window size:
  19.   size(400, 300);        
  20.  
  21.   // List all the available serial ports
  22.   println(Serial.list());
  23.   // I know that the first port in the serial list on my mac
  24.   // is always my  Arduino, so I open Serial.list()[0].
  25.   // Open whatever port is the one you're using.
  26.   myPort = new Serial(this, Serial.list()[0], 9600);
  27.   // don't generate a serialEvent() unless you get a newline character:
  28.   myPort.bufferUntil('\n');
  29.   // set inital background:
  30.   background(0);
  31. }
  32. void draw () {
  33.   // everything happens in the serialEvent()
  34. }
  35.  
  36. void serialEvent (Serial myPort) {
  37.   // get the ASCII string:
  38.   String inString = myPort.readStringUntil('\n');
  39.  
  40.   if (inString != null) {
  41.     // trim off any whitespace:
  42.     inString = trim(inString);
  43.     // convert to an int and map to the screen height:
  44.     float inByte = float(inString);
  45.     inByte = map(inByte, 0, 1023, 0, height);
  46.  
  47.     // draw the line:
  48.     stroke(127,34,255);
  49.     line(xPos, height, xPos, height - inByte);
  50.  
  51.     // at the edge of the screen, go back to the beginning:
  52.     if (xPos >= width) {
  53.       xPos = 0;
  54.       background(0);
  55.     }
  56.     else {
  57.       // increment the horizontal position:
  58.       xPos++;
  59.     }
  60.   }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement