serialSend.pde 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import oscP5.*;
  2. import netP5.*;
  3. OscP5 oscP5;
  4. NetAddress arduinoAddress;
  5. //the number of analog pins on this controller
  6. int analogPins = 16;
  7. //an array of all of the pin values
  8. int[] pinVals = new int[analogPins];
  9. void setup() {
  10. frameRate(60);
  11. size(320, 100);
  12. background(0);
  13. //initialize the listening port
  14. oscP5 = new OscP5(this, 9999);
  15. //the outgoing communication to the arduino
  16. arduinoAddress = new NetAddress("128,32.122.252", 8888);
  17. }
  18. void draw() {
  19. //clear the previous bars
  20. fill(0);
  21. rect(0, 0, width, height);
  22. //draw each of the bars showing the pin value
  23. for (int i = 0; i < analogPins; i++) {
  24. int value = pinVals[i];
  25. int barWidth = width/analogPins;
  26. float barHeight = (value/1024.)*height;
  27. //draws the new bar in white
  28. fill(255);
  29. rect(barWidth*i, height - barHeight, barWidth, barHeight);
  30. }
  31. }
  32. // incoming osc message are forwarded to the oscEvent method.
  33. void oscEvent(OscMessage msg) {
  34. String address = msg.addrPattern();
  35. if (address.startsWith("/analog/")) {
  36. //then it's an analog reading
  37. //split the address
  38. String[] splitAddr = address.split("/");
  39. //the third element should be the number
  40. int pinNum = Integer.parseInt(splitAddr[2]);
  41. int val = msg.get(0).intValue();
  42. pinVals[pinNum] = val;
  43. }
  44. }