daily pastebin goal
3%
SHARE
TWEET

Processing: Rolling Graph

aboes Oct 28th, 2013 3,262 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import processing.serial.*;
  2. Serial myPort;
  3. int current;
  4. float inByte;
  5.  
  6. int[] yValues;
  7. int w;
  8.  
  9. void setup()
  10. {
  11.   size(640, 360);
  12.   w = width-10;
  13.   strokeWeight(3);
  14.   smooth(); // or noSmooth();
  15.   yValues = new int[w];
  16.   String portName = Serial.list()[0];
  17.   myPort = new Serial(this, portName, 9600);
  18. }
  19.  
  20. void draw()
  21. {
  22. // Here are simplifications possible
  23. // Probably with read() http://processing.org/reference/libraries/serial/Serial_read_.html
  24. // And get rid of this String-Stuff
  25.   String inString = myPort.readStringUntil('\n');
  26.  
  27.   if(inString != null){
  28.     inString = trim(inString);
  29.     inByte = float(inString);
  30.     current = int(map(inByte, 0, 1023, 0, height));
  31.        
  32.     background(55);
  33.    
  34.     for(int i = 1; i < w; i++) {
  35.       yValues[i-1] = yValues[i];
  36.     }
  37.  
  38.     yValues[w-1] = current;
  39.    
  40.     stroke(255, 200, 0);
  41.     line(w, current, width, current);
  42.     strokeWeight(1);
  43.     line(0, current, width, current);
  44.     strokeWeight(3);
  45.  
  46.     for(int i=1; i<w; i++) {
  47.       stroke(220, 75, yValues[i]);
  48.       point(i, yValues[i]);
  49.     }
  50.   }
  51. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top