Guest User

Untitled

a guest
Apr 25th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. import processing.serial.*;
  2.  
  3. Serial[] myPorts = new Serial[2]; // Create a list of objects from Serial class
  4. int[] dataIn = new int[2]; // a list to hold data from the serial ports
  5.  
  6. void setup() {
  7. size(400, 300);
  8. // print a list of the serial ports:
  9. println(Serial.list());
  10. // On my machine, the first and third ports in the list
  11. // were the serial ports that my microcontrollers were
  12. // attached to.
  13. // Open whatever ports ares the ones you're using.
  14.  
  15. // get the ports' names:
  16. String portOne = Serial.list()[0];
  17. String portTwo = Serial.list()[2];
  18. // open the ports:
  19. myPorts[0] = new Serial(this, portOne, 9600);
  20. myPorts[1] = new Serial(this, portTwo, 9600);
  21. }
  22.  
  23.  
  24. void draw() {
  25. // clear the screen:
  26. background(0);
  27. // use the latest byte from port 0 for the first circle
  28. fill(dataIn[0]);
  29. ellipse(width/3, height/2, 40, 40);
  30. // use the latest byte from port 1 for the second circle
  31. fill(dataIn[1]);
  32. ellipse(2*width/3, height/2, 40, 40);
  33. }
  34.  
  35. /**
  36. * When SerialEvent is generated, it'll also give you
  37. * the port that generated it. Check that against a list
  38. * of the ports you know you opened to find out where
  39. * the data came from
  40. */
  41. void serialEvent(Serial thisPort) {
  42. // variable to hold the number of the port:
  43. int portNumber = -1;
  44.  
  45. // iterate over the list of ports opened, and match the
  46. // one that generated this event:
  47. for (int p = 0; p < myPorts.length; p++) {
  48. if (thisPort == myPorts[p]) {
  49. portNumber = p;
  50. }
  51. }
  52. // read a byte from the port:
  53. int inByte = thisPort.read();
  54. // put it in the list that holds the latest data from each port:
  55. dataIn[portNumber] = inByte;
  56. // tell us who sent what:
  57. println("Got " + inByte + " from serial port " + portNumber);
  58. }
  59.  
  60. /*
  61. The following Wiring/Arduino code runs on both microcontrollers that
  62. were used to send data to this sketch:
  63.  
  64. void setup()
  65. {
  66. // start serial port at 9600 bps:
  67. Serial.begin(9600);
  68. }
  69.  
  70. void loop() {
  71. // read analog input, divide by 4 to make the range 0-255:
  72. int analogValue = analogRead(0)/4;
  73. Serial.print(analogValue, BYTE);
  74. // pause for 10 milliseconds:
  75. delay(10);
  76. }
  77.  
  78.  
  79. */
Add Comment
Please, Sign In to add comment