Guest User

Untitled

a guest
Apr 25th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. import processing.serial.*;
  2. import ddf.minim.*;
  3. Serial[] myPorts = new Serial[2];
  4. String[] dataIn = new String[2];
  5. String[] myStrings = new String[2];
  6.  
  7. Minim minim;
  8. AudioSample [] tone = new AudioSample[5];
  9.  
  10. void setup() {
  11. size(10,10);
  12.  
  13. // List all the available serial ports
  14. println(Serial.list());
  15.  
  16. // Get the ports' names:
  17. String portOne = Serial.list()[0];
  18. String portTwo = Serial.list()[2];
  19.  
  20. // open the ports:
  21. myPorts[0] = new Serial(this, portOne, 9600);
  22. myPorts[1] = new Serial(this, portTwo, 9600);
  23.  
  24. // read bytes into a buffer until you get a linefeed (ASCII 10):
  25. myPorts[0].bufferUntil('\n');
  26. myPorts[1].bufferUntil('\n');
  27.  
  28. minim = new Minim(this);
  29. tone[0] = minim.loadSample("GoraicA5.aif");
  30. tone[1] = minim.loadSample("GoraicG5.aif");
  31. tone[2] = minim.loadSample("GoraicF5.aif");
  32. tone[3] = minim.loadSample("GoraicE5.aif");
  33. tone[4] = minim.loadSample("GoraicD5.aif");
  34. }
  35.  
  36. void draw() {
  37. // clear the screen:
  38. background(0);
  39.  
  40. }
  41.  
  42. void serialEvent(Serial thisPort) {
  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.  
  53. // read a byte from the port:
  54. //int inByte = thisPort.read();
  55. String inByte = thisPort.readStringUntil('\n');
  56. // put it in the list that holds the latest data from each port:
  57. dataIn[portNumber] = myStrings[portNumber] = inByte;
  58.  
  59. // tell us who sent what:
  60. println("Got " + inByte + " from serial port " + portNumber);
  61.  
  62. myStrings[portNumber] = trim(myStrings[portNumber]);
  63.  
  64. // split the string at the commas
  65. // and convert the sections into integers:
  66. int sensors[] = int(split(myStrings[portNumber], ','));
  67.  
  68. // print out the values you got:
  69. for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) {
  70. //print("Sensor " + sensorNum + ": " + sensors[sensorNum] + "\t");
  71. }
  72.  
  73. // add a linefeed after all the sensor values are printed:
  74. println();
  75.  
  76. if(sensors[0] > 4) {
  77. tone[0].trigger();
  78. }
  79. if(sensors[1] > 4) {
  80. tone[1].trigger();
  81. }
  82. if(sensors[2] > 4) {
  83. tone[2].trigger();
  84. }
  85. if(sensors[3] > 4) {
  86. tone[3].trigger();
  87. }
  88. if(sensors[4] > 4) {
  89. tone[4].trigger();
  90. }
  91.  
  92. }
Add Comment
Please, Sign In to add comment