Thursday, February 28, 2013

Storing data in arrays

Yesterday I posted about how my respiratory-musical interface gathers information from the analog sensors (sent via the lilypad) and read in through supercollider.

Today I will post how these values are stored for comparison.

For starters, recall that this code read the serial values in from the lilypad:

(

~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;
      });
});

)

This next code adds on to this basic template.  First we need to add variables (inside the task, but outside the inf loop) for the array where we will store the data:


//6 sensors...1 zero 'message' to say that the array is over
var serialArraySize = 7; 


//make a new array (with space for serialArraySize number of values) for incoming serial data
var serialArray = Array.newClear(serialArraySize); 



Next, inside the inf loop, we will need to tell the serial array to constantly shift values to the left so that new values can replace the old as they are read in:


//make the serial array constantly shift to receive new data
serialArray = serialArray.shift(-1); 


And then we will need to tell the array what values it should storing:
//put the new values into the serial array
serialArray[serialArraySize - 1] = ~incData


Finally, we need to figure out when the analog values coming into the array are in the right position, so that the value for sensor 0 is in index 0 and sensor 1 is in index 1 and so on.  Remember we did this by making the array have one extra index that we fill with a message (a zero) that we can then use to tell our code that the array is complete.  

if(serialArray[serialArraySize - 1]==0, { 
       serialArray.postln; //post the serial array
    });




And we'll stop here to see this altogether:




(

~serialReceive = Task({  //create a new task to get serial data
 
        var serialArraySize = 7; 
        var serialArray = Array.newClear(serialArraySize); 

               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

                                        //make the serial array constantly shift to receive new data
                                         serialArray = serialArray.shift(-1);
                                         //put the new values into the serial array
                                         serialArray[serialArraySize - 1] = ~incData;  

                                                 if(serialArray[serialArraySize - 1]==0, { 
                                                         //if sc receives the 0, the flag or message coming in from the arduino             
                                                         //and it is placed at the end of the serial array,
                         
                                                         serialArray.postln; //post the serial array

                           });
                 });
            0.1.wait;
           });
    });

)

The next step is to store these completed arrays as they come in so that it's possible to compare old sensor values with new ones to tell if there has been a breath.  Note that the time between an old value and a new one is *very* small--I've been setting it at tenth of a second or less.  

No comments:

Post a Comment