Serial.read Array

Hi,

I am currently sending an array from Supercollider to Arduino. My problem is on the Arduino code side, I am struggling as I am not familiar with the language. Basically, I want to read the data, from SuperCollider, in Arduino as an Array, and then isolate each number from that Array.

Can some one please help me or point me in the right direction

-Robyn :-/

Can some one please help me or point me in the right direction

Only if you post more information. Here are some questions and a request that will hopefully help you get help...

Are you sending the array elements one at a time or is there something in Supercollider that allows an entire array to be sent?

By "sending an array" do you mean over an RS232 link, ethernet connection, pack mule, or something else?

How big is the array? What's in the array (number, strings, both)?

Please post the relevant code.

Hi coding badly,

I am sending an array of ascii integers all at the same time from supercollider to the arduino via the serial port.

So basically where I am stuck, is how arduino picks the array up as an array and can assign each integer to a different variable. (currently it seems to pick each integer from the array separately)

hope this makes more sense :stuck_out_tongue:
:slight_smile:

//sc code:

(
p = SerialPort(
      "/dev/tty.usbserial-A9005de2",
      baudrate: 115200,
      crtscts: true);
)

(
~packet = { var channel,value, val1, val2, val3, output;
       
          channel = 4.asHexString;
        channel = channel[7].ascii.asInteger;
        value =  10.asHexString;
        val1 =  value[5].ascii;
        val2 =  value[6].ascii;
        val3 =  value[7].ascii;
        output = [channel,val1.asInteger,val2.asInteger,val3.asInteger];
        output.postln;
};
~packet.value.postln;

 p.putAll(~packet.value;);
 )
//arduino code a friend gave me to look at, from the void loop onwards

void loop()
{
  int rxdata, channel, val1, val2, val3;
  int val = 0;
  int dumpme = 0;
                          
 if (Serial.available() > 0){                //wait for data
      val = Serial.read();
 
     if (val == 35)                          //indicates # start of packet and channel number                      
       {while (Serial.available() < 1){};    //dont do anything until that next byte gets in
          channel = Serial.read();           //first byte is the channel                
          if(channel > 57)                   //of course its ascii encoded hex so we need to turn it into an int
          {
            channel -= 55;
          }
           else channel -= 48;
        
        
        while (Serial.available() < 1){};    
          val1 = Serial.read();              
          if(val1 > 57)
          {
            val1 -= 55;
          }
           else val1 -= 48;
          rxdata = val1 << 8;                // first byte specifies top 4 bits of brightness so shift it up, now val = 111100000000
        
        
         while (Serial.available() < 1){};   //dont do anything until that next byte gets in
          val2 = Serial.read();              //this byte has the middle four bits of brightness 000011110000
          if(val2 > 57)                      //is it the ascii for A to F i.e. 65 to 70
          {
            val2 -= 55;                      //A in hex is 10 in dec. 
          }
         else val2 -= 48;
          val2 = val2 << 4;                   //this byte has the middle four bits of brightness 000011110000
          rxdata += val2;                    
         
         
         while (Serial.available() < 1){};   //dont do anything until that next byte gets in
          val3 = Serial.read();              //this byte has the bottom four bits of brightness 00001111
          if(val3 > 57)                      //is it the ascii for A to F
          {
            val3 -= 55;                      //convert it to a number
          }
         else val3 -= 48;                    //otherwise its the ascii for 0 to 9 i.e. 48 to 57
          rxdata += val3;                    //now rxdata has the three bytes of info in it 1111 1111 1111 
          
         Tlc.set(channel, rxdata);
         updateflag = 1; 
      }
      
      else
      {dumpme = Serial.read();}
      
  };

//the actual lighting part
if ((millis()%5 == 0) && (updateflag == 1))  //every 5ms as long as the updateflag says so
    
      {
        Tlc.update();
        updateflag = 0;
      }


  
}

I am sending an array of ascii integers all at the same time from supercollider to the arduino via the serial port

Got it.

currently it seems to pick each integer from the array separately

That's what it looks like to me as well.

So basically where I am stuck, is how arduino picks the array up as an array and can assign each integer to a different variable

So, instead of doing something with the values as they're read from the serial port, you'd like to store the values and use them later?

Slightly OT hint: Your sketch is easier to read and understand if you use literal constants, instead of decimal numbers, thusly:

if (val == '#')                          //indicates # start of packet and channel number                      
       {while (Serial.available() < 1){};    //dont do anything until that next byte gets in
          channel = Serial.read();           //first byte is the channel                
          if(channel > '9')                   //of course its ascii encoded binary so we need to turn it into an int
          {
            channel -= '7';
          }
           else channel -= '0';