SEND several double from MAX to Arduino through Serial port. RESOLVED

benjamin vyger's icon

Hello.

I'm trying to send 24 datas from Max to Arduino, but I can't read all data

But they don't appears in Arduio at the good place.

The aim is to send data from 0 to 3200 and ten datas from 0 to 100 and 4 others int

Max Patch
Copy patch and select New From Clipboard in Max.

Here's Arduino code

//int rc = 0; // for incoming serial data
//byte rc;// = 0;
char rc;// = 0; // for incoming serial data

#define NBDATA 24
int var[NBDATA] = { 0, 0, 0, 0, 0 };
#define LED_BUILTIN 16
#define BUILTIN_LED LED_BUILTIN

 void setup() {
 pinMode (LED_BUILTIN,OUTPUT);
 Serial.begin(115200);
 SerialUSB.begin(115200);
}

void dataFromMaxToManageInArduino()
{ 
      if (Serial.available() > 0 ) //  SerialUSB.println("");
      {
         for(uint8_t i = 0; i < 24; i++)
        {  
       rc = Serial.read(); // SerialUSB.println(rc); 
       var[i]=rc;   
        }
      }     
      SerialUSB.println(""); //Serial.println("");       
}        
   
void loop() {
dataFromMaxToManageInArduino();
  for(uint8_t i = 0; i < 24; i++)
      {
           SerialUSB.print(var[i]); SerialUSB.print(" ");
      }
          digitalWrite(LED_BUILTIN,var[NBDATA-1]); // test LED
}

Here what I receive in Arduino

3 20 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 
3 20 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 
3 20 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 
3 20 

Source Audio's icon

what is the question about ?

this here makes no sense:

"The aim is to send data from 0 to 3200 and ten datas from 0 to 100 and 4 others int"

............

this is one of your 24 "datas" looked at maximum value output.

and you punch that directly as 24 numbers list into serial object ???

you must state what exactly you want to send from max

and what should arduino do with it.

P.S. you scale 0 - 127 to 0. 32. , then again to 0 3200, then multiply that by another 127 * 3200 , summ that and send to arduino ?

sources are 2 7bit bytes ?

And then you want to try to make what in arduino from that number ?

no comment

was that your topic ?

benjamin vyger's icon

Hi. ! Good morning.

I explained my question too quickly yesterday, I was super tired.

The goal of this part of my project is to control 10 stepper motors by directly sending if possible 10 data from - 650000 to 650000.

With the Max/Msp patch, I receive the position of each stepper motor from 0 to 127 and with the right potentiometer the number of turns from 0 to 127.

First, I resize the position twice to transform it from 0 to 3200.

Second, I multiply the turns to 3200

At the end I have the actual position of the stepper by adding the position with the number of revolutions.

I would like to directly send the 10 data ranging from - 650000 to 650000 via serial port to an arduino. Is this possible, or do we have to split the data?

In my patch and Arduino code I try to send 24 int from 0 to 255. And I already have problems!

The question to Arduino was from me.

I changed the Arduino code to read 24 int but i read

0 61 1 255 2 255 3 255 4 55 5 35 6 32 7 24 8 0 9 0 10 0 11 0 12 0 13 0 14 0 15 0 16 0 17 0 18 0 19 0 20 0 21 0 22 0 23 0 
0 0 1 0 2 0 3 255 4 255 5 255 6 255 7 255 8 255 9 255 10 255 11 255 12 255 13 255 14 255 15 255 16 255 17 255 18 255 19 255 20 255 21 255 22 255 23 255 
0 62 1 255 2 255 3 255 4 55 5 35 6 32 7 24 8 0 9 0 10 0 11 0 12 0 13 0 14 0 15 0 16 0 17 0 18 0 19 0 20 0 21 0 22 0 23 0 
0 0 1 0 2 0 3 255 4 255 5 255 6 255 7 255 8 255 9 255 10 255 11 255 12 255 13 255 14 255 15 255 16 255 17 255 18 255 19 255 20 255 21 255 22 255 23 255 
0 62 1 255 2 255 3 255 4 55 5 35 6 32 7 24 8 0 9 0 10 0 11 0 12 0 13 0 14 0 15 0 16 0 17 0 18 0 19 0 20 0 21 0 22 0 23 0 
0 0 1 0 2 0 3 255 4 255 5 255 6 255 7 255 8 255 9 255 10 255 11 255 12 255 13 255 14 255 15 255 16 255 17 255 18 255 19 255 20 255 21 255 22 255 23 255 
0 63 1 255 2 255 3 255 4 55 5 35 6 32 7 24 8 0 9 0 10 0 11 0 12 0 13 0 14 0 15 0 16 0 17 0 18 0 19 0 20 0 21 0 22 0 23 0 
0 0 1 0 2 0 3 255 4 255 5 255 6 255 7 255 8 255 9 255 10 255 11 255 12 255 13 255 14 255 15 255 16 255 17 255 18 255 19 255 20 255 21 255 22 255 23 255 

while I send

print: 60 55 35 32 24 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
print: 61 55 35 32 24 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
print: 62 55 35 32 24 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
print: 62 55 35 32 24 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
print: 63 55 35 32 24 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
send 24 int.

Max Patch
Copy patch and select New From Clipboard in Max.

Here my Arduino code

byte inChar; // Where to store the character read
#define NBDATA 24
int var[NBDATA] = { 0 };

#define LED_BUILTIN 16
#define BUILTIN_LED LED_BUILTIN

 void setup() {
 pinMode (LED_BUILTIN,OUTPUT);
 Serial.begin(115200);
}
void loop() {
dataFromMaxToManageInArduino();
}
void dataFromMaxToManageInArduino() {  
  if (Serial.available() > 0){

     for(uint8_t i = 0; i < NBDATA; i++) {
       inChar = Serial.read();
       var[i] = inChar; // Integer   
     SerialUSB.print (i);
     SerialUSB.print ( " ");
     SerialUSB.print(var[i]);
     SerialUSB.print ( " ");
     digitalWrite(LED_BUILTIN,( 255- (var[NBDATA-1]) ) );
  }
  SerialUSB.println ();
}
}
Source Audio's icon

-650000 ~ 650000 needs type int32 or long in arduino.

you have to take that into account when both sending

values from Max as well when you try to receive them in Arduino.

get rid of that enttec patcher and use proper ascii to bytes conversion

Why do you allways post stuff that has nothing to do with what you really want ?

if you need 10 long values , why do you post that patches with 24 ints

which you are not able to send as well as parse ?

see here - yellow multislider sends 24 long numbers

blue one gets them back from arduino...

benjamin vyger's icon

Doin't look at this post, I didn't see your previous one!

Thanks for your help.

I convert a list of 24 int to an ASCII message and I'm trying to send a list of ASCII via Serial port but I have this message error

serial: doesn't understand "16510 11090 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 5670"

Could you fix it it, please?

Second, what is the method to replace the space between each int with a coma ?

Thank you so much

Max Patch
Copy patch and select New From Clipboard in Max.

Source Audio's icon

you don't understand anything here.

why atoi to itoa ???

You have to convert list to ascii bytes using atoi and then send 10

(new line) at end.

Did you look at screenshhot I posted at all ?

what is that sprintf doing there ? and zl.group ?

then where are this negative values happenning ?

I mean you asked for -650000 to 650000 range ?

to drive 10 stepper motors ?

What are this 24 numbers representing in that sense ?

don't use live gui objects if you don't want to automate them.

You have 48 elements and want the list to be updated whenever one of them changes, but not send whole list to serial too fast.

If you need 3200, then set slider size to 3201 !

This is what it should look like :

if you can't reproduce that, then

post, I will upload patch with this few objects inside.

benjamin vyger's icon

Hi,

This new patch works perfectly!

The Arduino given from the previous previous post print datas as soon as mouse is released from slider or knock! ;)

With my iPhone, I didn't see the entire previous previous post, I din't see the picture, sorry, next time I will check better your post.

Here the patch

Max Patch
Copy patch and select New From Clipboard in Max.

Source Audio's icon

Problem with live gui objects is that they get stored & automated in live set,

and may have init values, which can disturb wanted usage

of 48 gui elements like what you have.

normal max dial or number can be excluded completely from that

and added to own storage or automation system if needed, remote control etc.

......

next :

That last patch sends complete list only on mouse release if used by

mousing, it will send directly if values come from outside,

like midi, live automation ... whatever.

That could overload serial port.

In case you need to send values on every change and not only at end of

value change, then you need something like what DMX does,

send list at specified interval which is fast enough to run the lights, or motors,

but does not overload serial port.

Max Patch
Copy patch and select New From Clipboard in Max.

Source Audio's icon

P.S. you asked in previous post how to replace each space in the list with comma ?

replace or add ?

22 33 44

22,33,44

or

22, 33, 44

both is easy to do using for example regexp and few other objects

maybe you should explain what you need this for

benjamin vyger's icon

Hi,

To receive well 24 datas in Arduino I need to send from Max through serial datas like this

<22,23,44,-55,...,3200>

I need to add < and > as start and end marker.

data coma no space.
Max Patch
Copy patch and select New From Clipboard in Max.

Here how data are parsed with Arduino


void loop() { 
recvWithStartEndMarkers(); // recevoir chaine de character et disperse en unité 
    if (newData == true) {
        strcpy(tempChars, receivedChars);
            // this temporary copy is necessary to protect the original data
            //   because strtok() used in parseData() replaces the commas with \0
        parseData();        
        newData = false;
     
    }  
  }
}

void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;

    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }

        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}

//================================================================= RECEIVE 24 DATAS FROM MAX or PROCESSING

void parseData() {      // split the data into its parts

    char * strtokIndx; // this is used by strtok() as an index

    strtokIndx = strtok(tempChars,",");      // get the first part - the string

    for(uint8_t i = 0; i < NBMOTEURS; i++) { 

    dataFromMax[i] = atoi(strtokIndx);     // convert this part to an integer
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    }

    }
 

Source Audio's icon

here is what you asked for

it will output list exactly like this :

<0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0> + line feed

or bytes : **** 44 is ascii comma *** / < > = 60 & 62 / 48 is zero

60 48 44 48 44 48 44 48 44 48 44 48 44 48 44 48 44 48 44 48 44 48 44 48 44 48 44 48 44 48 44 48 44 48 44 48 44 48 44 48 44 48 44 48 44 48 44 48 62 10

I don't know if that is what your Arduino code expects, but you asked for exactly that.

in case you don't need line feed at the end, remove t 10 l

benjamin vyger's icon

Thank you so much!

It's perfect. ;) ;) ;)