Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.35 KB | None | 0 0
  1. /*
  2. Example of how to parse and display incoming Serial data from an Arduino Uno.
  3. Serial setup is from the Processing.org Serial library reference.
  4.  
  5. Serial data from the Arduino in this example is organized as such (without quotations):
  6.  
  7. "data1|data2|data3"
  8.  
  9. pipe character used as a delimiter, and newline (Serial.println();) is treated as the end-of-packet.
  10. By no means the only way to do it, but using a delimiter makes it easy to send variable length data & text in each chunk
  11.  
  12. Example Arduino code:
  13.  
  14. void loop()
  15. {
  16.     int randBarData = random(0, 100);
  17.     int randColorData = random(0, 255);
  18.    
  19.     Serial.print("Some Text");
  20.     Serial.print('|');
  21.     Serial.print(randColorData);
  22.     Serial.print('|');
  23.     Serial.print(randBarData);
  24.     Serial.println();
  25.  
  26.     delay(100);
  27. }
  28.  
  29. */
  30.  
  31.  
  32. import processing.serial.*;
  33.  
  34. // Flag to indicate if we have any data yet
  35. boolean dataValid = false;
  36.  
  37. Serial myPort;
  38.  
  39. // String array to hold the data chunks we recieve in each packet
  40. String[] serialData;
  41.  
  42. // ASCII #10 = linefeed/newline
  43. int lf = 10;
  44.  
  45. void setup()
  46. {
  47.     size(400,200);
  48.    
  49.   // Open the first connected Serial port
  50.   // If that fails, change to whichever port Arduino is connected to, for example "COM3"
  51.   myPort = new Serial(this, Serial.list()[0], 9600);
  52.  
  53.   // Keep buffering data until we encounter linefeed (doing so triggers serialEvent)
  54.   myPort.bufferUntil(lf);
  55. }
  56.  
  57. void draw()
  58. {
  59.     // Start with a black canvas
  60.     background(0);
  61.    
  62.     // Don't do anything unless we have data
  63.     if(dataValid)
  64.     {
  65.         fill(255);
  66.         text("received: " + serialData[0], 10,20);
  67.         text("received: " + serialData[1], 10,40);
  68.        
  69.  
  70.         // Parse the 2nd data segment into an integer. Trim to remove any non-alphanumericals
  71.         int colorData = parseInt(trim(serialData[1]));
  72.         fill(colorData, 0, 0);
  73.         rect(10, 80, 20, 20);
  74.        
  75.  
  76.         fill(0, 255, 0);
  77.         int barData = parseInt(trim(serialData[2]));
  78.        
  79.         // Map the 3rd data segment from (0 -> 100) into (0 -> width of canvas)
  80.         float barLength = map(barData, 0, 100, 0, width);
  81.         rect(0, 150, barLength, 50);
  82.     }
  83. }
  84.  
  85. void serialEvent(Serial p)
  86. {
  87.     // Store the Serial buffer
  88.     String tString = p.readString();
  89.    
  90.     // Split the String we recieved into individual chunks, using our pipe character as the delimiter token
  91.     serialData = splitTokens(tString, "|");
  92.    
  93.     // serialData is no longer empty, we can allow drawing now
  94.     dataValid = true;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement