Wednesday, February 27, 2013
Breathing in Banff
For the past seven weeks I have been working on a new version of the Respiratory-Musical Interface in Banff, AB, as part of the Winter Creative Residency. Somewhat belatedly, I wanted to return to posting updates about the project. There is quite a lot to catch up with so I will try to post a very short code excerpt every day from now until I leave, rather than trying to get EVERYTHING into a single post.
*Apologies for the lack of formatting in the code...blogger seems to make that disappear when I copy and paste. I'll look for a solution when I have a chance!
Anyway, for starters, here is the LilyPad Arduino code:
int message = 0;
int numSensors = 6; //number of analog sensors used
int sensorVal;
int sensorIn = 0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
for(int a=0; a<numSensors; a++) //loop through sensors 0 through 5
{
sensorVal = analogRead(a); //read sensor values at a
Serial.write(sensorVal/4); //IMPORTANT - USE Serial.write NOT serial.print or SC will have a bit of a FIT
}
Serial.write(message); //send message so SC can distinguish between serial values
// delay(175);
delay(500);
}
The 'message' variable is there to help the SC side of the equation distinguish between the values coming in for the various pins from the LilyPad. When it gets the message (a zero), it knows that the subsequent value it receives will be from analog pin 0. (After receiving the zero it puts the next 7 values into an array in which the last index is the 0 message.)
Now, moving to SuperCollider, the most important part of the code below is assigned to the ~myPort variable, which tells SC where to find the serial values. SerialPort.listDevices justs lets you find the name of the proper device.
SerialPort.listDevices;
~myPort = SerialPort("/dev/tty.usbserial-A600dSr9", baudrate: 9600, crtscts: true);
~myPort.close
SerialPort.closeAll;
~pieceBegins = thisThread.seconds; //assign the start of the piece
After assigning the start of the piece, the next step is to actually read in some values from the analog sensors. The code below reads in data from the serialPort, ~myPort, assuming it is notNil, and then posts it.
(
~serialReceive = Task({ //create a new task to get serial data
inf.do({
if(~myPort.notNil, { //if the serial port is notNil
~incData = ~myPort.next; //then assign the new incoming serial value to incData
if(~incData.notNil, { //if the values coming in are notNil
~incData.postln;//post them
});
});
0.1.wait;
});
});
)
~serialReceive.play;
~serialReceive.stop;
Tomorrow I will post the code that captures this data in an array, and perhaps the code that enables a comparison between old and new values from the sensors.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment