Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import processing.serial.*;
- import ddf.minim.*;
- Serial[] myPorts = new Serial[2];
- String[] dataIn = new String[2];
- String[] myStrings = new String[2];
- Minim minim;
- AudioSample [] tone = new AudioSample[5];
- void setup() {
- size(10,10);
- // List all the available serial ports
- println(Serial.list());
- // Get the ports' names:
- String portOne = Serial.list()[0];
- String portTwo = Serial.list()[2];
- // open the ports:
- myPorts[0] = new Serial(this, portOne, 9600);
- myPorts[1] = new Serial(this, portTwo, 9600);
- // read bytes into a buffer until you get a linefeed (ASCII 10):
- myPorts[0].bufferUntil('\n');
- myPorts[1].bufferUntil('\n');
- minim = new Minim(this);
- tone[0] = minim.loadSample("GoraicA5.aif");
- tone[1] = minim.loadSample("GoraicG5.aif");
- tone[2] = minim.loadSample("GoraicF5.aif");
- tone[3] = minim.loadSample("GoraicE5.aif");
- tone[4] = minim.loadSample("GoraicD5.aif");
- }
- void draw() {
- // clear the screen:
- background(0);
- }
- void serialEvent(Serial thisPort) {
- int portNumber = -1;
- // iterate over the list of ports opened, and match the
- // one that generated this event:
- for (int p = 0; p < myPorts.length; p++) {
- if (thisPort == myPorts[p]) {
- portNumber = p;
- }
- }
- // read a byte from the port:
- //int inByte = thisPort.read();
- String inByte = thisPort.readStringUntil('\n');
- // put it in the list that holds the latest data from each port:
- dataIn[portNumber] = myStrings[portNumber] = inByte;
- // tell us who sent what:
- println("Got " + inByte + " from serial port " + portNumber);
- myStrings[portNumber] = trim(myStrings[portNumber]);
- // split the string at the commas
- // and convert the sections into integers:
- int sensors[] = int(split(myStrings[portNumber], ','));
- // print out the values you got:
- for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) {
- //print("Sensor " + sensorNum + ": " + sensors[sensorNum] + "\t");
- }
- // add a linefeed after all the sensor values are printed:
- println();
- if(sensors[0] > 4) {
- tone[0].trigger();
- }
- if(sensors[1] > 4) {
- tone[1].trigger();
- }
- if(sensors[2] > 4) {
- tone[2].trigger();
- }
- if(sensors[3] > 4) {
- tone[3].trigger();
- }
- if(sensors[4] > 4) {
- tone[4].trigger();
- }
- }
Add Comment
Please, Sign In to add comment