Handling multiple variables over serial from Max/MSP to Arduino

    Feb 23 2012 | 7:09 pm
    I'm a third year at university in the UK working on a project which is essentially (for the time being) controlling a 4x4 Matrix of LEDs that have been multiplexed through programming using Arduino, via Max/MSP. Without Max/MSP, I'm able to program the Matrix as I like, chosing which LEDs are on, and which are off. However I'm having very little luck using Max/MSP to control it.
    My Max/MSP patch uses switches to represent a binary value (ie. 1010 = On/Off/On/Off) for each row of the matrix, converts the binary value to an integer, and adds an identifier (0-3) before sending through serial to Arduino.
    Max/MSP Code:
    The code below attempts to gather 2 bytes of data from the serial buffer, and put each byte into an array. Depending on what the first byte is (0,1,2,3) depends on what row of the LED matrix the second value of the buffer is assigned to.
    if (Serial.available() > 1){ //if 2 bytes available in the Serial buffer...
    for (int i=0; i bufferArray[ i ] = Serial.read(); // put into array output = 1; // show new data has been recieved } }
    if (output != 0) // if new bytes have been recieved { rowNumber = bufferArray[0]; // store first byte as "identifier" rowBinary = bufferArray[1]; // store second byte as "value" output = 0; }
    if (rowNumber == 0){ ledRow1 = rowBinary; }
    if (rowNumber == 1){ ledRow2 = rowBinary; }
    if (rowNumber == 2){ ledRow3 = rowBinary; }
    if (rowNumber == 3){ ledRow4 = rowBinary; }
    I've spent a lot of time trying to get this to work, and have scoured the forums trying to find an answer to my problem, but I still can't figure out why it isn't working. The link below is the EXACT problem I'm facing, but sadly the OP didn't confirm how he got it to work!
    Any help on this would be great!
    Cheers, Ben K
    Edit: Formatting

    • Feb 24 2012 | 9:57 am
      The problem may be that the stream is not synced, i.e. bufferArray[0] doesn't contain the identifier but the data, and bufferArray[1] the identifier (you can check this sending back the datas to Max over serial).
      Instead of using 0, 1, 2 & 3 to identify your rows, i would use values which are different from the 4 bits you want to send. I generally tag my datas with 'A', 'B', 'C', etc. 'A' is ASCII 65, so greater than the biggest value you can get with the 4 bits you send.
      Then I'd wait for an incoming value >= 'A', then read the next incoming value (which is the binary data representing the state of your LEDs).
      BTW, a metro 1 is generally a bad idea, it's too fast.