Advertisement
Guest User

Untitled

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