Advertisement
KRITSADA

Processing Serial Control

Sep 18th, 2019
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import processing.serial.*;
  2.  
  3. Serial myPort;
  4.  
  5. int windowWidth = 1280;
  6. int windowHeight = 700;
  7.  
  8. int upperValue = 700;
  9.  
  10. int[] valueArray = new int[windowWidth];
  11.  
  12. void setup() {
  13.   size(1280, 700);
  14.   println(Serial.list());
  15.  
  16.   myPort = new Serial(this, Serial.list()[2], 9600);
  17.   myPort.bufferUntil('\n');
  18. }
  19.  
  20. void draw() {
  21.   background(0);
  22.   if (valueArray != null) {
  23.     for (int i=0; i<valueArray.length; i++) {
  24.       stroke(127, 34, 255);
  25.       line(i, height, i, height - valueArray[i]);
  26.     }
  27.   }
  28.  
  29.   textSize(10);
  30.   for (int i=0; i<height; i++) {
  31.     if (i % (50) == 0) {
  32.       text(int((map(i, 0, height, upperValue, 0))) + " W", 10, i);
  33.       stroke(60);
  34.       line(0, i, width, i);
  35.     }
  36.   }
  37. }
  38.  
  39. void serialEvent(Serial myPort) {
  40.   String inString = myPort.readStringUntil('\n');
  41.   println(inString);
  42.   if (inString != null) {
  43.     if (inString.startsWith("W ")) {
  44.       println("Watt value!");
  45.       String val = inString.substring(2);
  46.       val = trim(val);
  47.  
  48.       float inByte = float(val);
  49.       inByte = map(inByte, 0, upperValue, 0, height);
  50.      
  51.       // Add last value to array
  52.       shiftAndAdd(valueArray, int(inByte));
  53.     }
  54.   }
  55. }
  56.  
  57. void shiftAndAdd(int a[], int val) {
  58.   int a_length = a.length;
  59.   System.arraycopy(a, 1, a, 0, a_length-1);
  60.   a[a_length-1] = val;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement