Go Down

Topic: Sampling rate from Arduino to max/msp (Read 2001 times) previous topic - next topic

stockfish

Hi,

I want to get data form a touch sensor that works as a potenciometer (is the same sensor used in R2M by Doepfer), and use this data in max/msp.  

I have instaled the drivers, the SimpleMessageSystem library, etc. and Arduino2Max_v4.

I can get data from the sensor pluged into an analog input, but I have problems with the frequency of this incoming data.

in the patch i can change two frequencies:

A: "Change how often MAX asks the Arduino for serial data: 10 miliseconds is the default."

B: "Change how often MAX reads the computer's serial port for incoming data. Making this too slow will cause crashes! 2 milliseconds is the default."

I have been trying to change both frequencies, and it looks like when i choose 100 miliseconds or uper, everyhing is ok. but when i go below 70 or 60 miliseconds, then the velocity of incoming data gets slower instead of increasing... and when i go below 10 miliseconds for "how often MAX asks the Arduino for serial data" then the patch just stops working.

Actually I can never get more than 10 incoming values per second...

how can i get faster interaction?

Thank's
jordi

Daniel

#1
Dec 05, 2006, 06:45 pm Last Edit: Dec 05, 2006, 06:56 pm by Daniel Reason: 1
Hi,

to get a possibly faster sampling rate (although I haven't tried this) you can delete the lines of code that read the pins you aren't using in the Arduino.

Right now,  the Arduino reads all the digital and analog pins:
Code: [Select]
void loop()
{
 
 
  if (messageBuild()) { // Checks to see if the message is complete
     firstChar = messageGetChar(); { // Gets the first word as a character

    if (firstChar = 'r') { // Checking for the character 'r'
         secondChar = messageGetChar(); // Gets the next word as a character
         if (firstChar = 'd') // The next character has to be 'd' to continue
              messageSendChar('d');  // Echo what is being read

      for (int i=0;i<=5;i++) {
      messageSendInt(analogRead(i)); // Read analog pins 0 to 5
         }

      for (int m=2;m<=13;m++) {
      messageSendInt(digitalRead(m)); // Read digital pins 2 to 13
         }
           
      messageEnd(); // Terminate the message being sent
      delay(100);
         
                   }
     }
  }
}
 

 
 



To potentially speed things up, you can try to just read the pins you need.
Here's an example that would just read two analog and two digital pins:
Code: [Select]
void loop()
{
 
 
[color=#cc0000]   if (messageBuild()) { // Checks to see if the message is complete
     firstChar = messageGetChar(); { // Gets the first word as a character

    if (firstChar = 'r') { // Checking for the character 'r'
         secondChar = messageGetChar(); // Gets the next word as a character
         if (firstChar = 'd') // The next character has to be 'd' to continue
              messageSendChar('d');  // Echo what is being read

      for (int i=0;i<=1;i++) {
      messageSendInt(analogRead(i)); // Read analog pins 0 and 1
         }

      for (int m=2;m<=4;m++) {
      messageSendInt(digitalRead(m)); // Read digital pins 2, 3 and 4
         }
           
      messageEnd(); // Terminate the message being sent
      delay(100);
         
                   }
     }
  }
}




Other than this, you can adjust the settings in MAX:

A: "Change how often MAX asks the Arduino for serial data: 10 miliseconds is the default."
this is how often MAX asks the Arduino to perform read its pins.

B: "Change how often MAX reads the computer's serial port for incoming data. Making this too slow will cause crashes! 2 milliseconds is the default."
This is how often MAX checks the serial buffer for data. If it's too high of a number, the serial port buffer won't get checked often enough, and it will overflow.

You might try:
- eliminating the MAX> Arduino requests altogether, and just having the Arduino send data continuously, but this will probably crash the serial port.
- adjusting and tweaking the settings above for the best compromise.

Ultimately I think you are going to be limited by the serial port speed, but you can probably do a lot better than it is now. If you get an improved verion, can you take hte time to post it in the playgorund with a higher version number?

Thanks

D

stockfish


Hi!!!

it works!

the main problem was the "delay(100);" at the end of the code message. That's why i could never get more than 10 values per second even if i change the setting values in Max....

so i have tryed to get down this "delay", and max works faster. i have tryed until "delay(1);" and it was still working, so i finally have delated this sentence from the code...
I don't know anything about programming, but i hope that this delay was optional in the code... am i right?
in any case, now it works faster and the setting values in the max patch are really the ones that controle the sapmling rate.

then i have tried some new codes where i change the quantity of pins to be read: the reall sampling rate is of course the one I choose in the max patch, but the difference is that i can go until faster sampling rates when i read less number of pins (i am talking always about "A: Change how often MAX asks the Arduino for serial data: 10 miliseconds is the default."). For example:

- if i read all analogs and all digitals pins i can go down until 17 ms (and i get around 62 value lists per second). under 17 milisecons the patch stops working.

- if i read 2 analog pins and 1 digital i can go down until 8ms (and i get around 124 lists per second)

so my final code would be:


void loop()
{
   
  if (messageBuild()) { // Checks to see if the message is complete
     firstChar = messageGetChar(); { // Gets the first word as a character

    if (firstChar = 'r') { // Checking for the character 'r'
         secondChar = messageGetChar(); // Gets the next word as a character
         if (firstChar = 'd') // The next character has to be 'd' to continue
              messageSendChar('d');  // Echo what is being read

      for (int i=0;i<=1;i++) {
      messageSendInt(analogRead(i)); // Read analog pins 0 and 1
         }

      messageEnd(); // Terminate the message being sent
         
                   }
     }
  }
}


so: do you thing that it can be a problem if i completely delete the parts "digitalRead(m)" and "delay(100)" from the code?
I have also delete this  "" from the code that you sugested me, because when i wanted to "compile" i got an error message.
what is this "" for?

thank you very much and i will tell you more about my testing

jordi

Daniel

#3
Dec 06, 2006, 07:18 pm Last Edit: Dec 06, 2006, 07:19 pm by Daniel Reason: 1
hi

it's not a problem to delete the digital reads and the delay(100);.. the delay shouldn't have been in there anyway. I think I had it there for testing. How many times a second can you read the analog pins?

D

PS: the [color] [/color] code is left from the bulletin board software

stockfish


if i read only 2 analog pins and no digitals i can go down until 7ms and i get around 140 lists per second

Daniel

#5
Dec 06, 2006, 07:36 pm Last Edit: Dec 06, 2006, 07:38 pm by Daniel Reason: 1
that's terrific.. now you can try to crank up the serial speed.. 230,400... 460,800. I read a post in here somewhere that siad the max serial speed could be higher than 115200. The specs for the FTDI USB chip say it will do three million baud...

Daniel

hey Jordi,

what platform/operating system are you running it on? I will update the Playground to reflect what machines it works on.

D

stockfish


hi
sorry, but i don't know a lot about computers... so i don't understand very well your last message "that's terrific.. now you can try to crank up the serial speed.. 230,400... 460,800. I read a post in here somewhere that siad the max serial speed could be higher than 115200. The specs for the FTDI USB chip say it will do three million baud..."

about my computer, i use a PC Intel(R) Celeron(R) M processor 1400MHz 256 RAM, with Windows XP Professional version 2002 Service Pack 2

thank's
jordi

Daniel

HI Jordi,

I meant that you can try higher serial speeds, in the Arduino code and in MAX. The next one up from 115200 is 230,400, and the next is 460,800... and so on. I don't know if it will work, but it is worth a try.


D

stockfish

ok
sorry, i missunderstood the "," with the "." ... so when you said 230,400 i read 230 and 400... ::)

yes, i know what you mean
I will try and let you know

stockfish

hi

i have tested diferent serial speeds, and it looks like the upper limit for my PC would be 115200, because when i try 230,400 and 460,800 the patch doesn't work anymore...

today i have tried with a PowerPC G5, OS X 10.4.8. the patch didn't work at the begining, so i have changed also the serial speed from arduino and from the patch and it works when i fix it to 57600... is that normal?


Daniel

thanks, that is good to know. It sounds like 115200 is the upper limit for reliable transmission. Yes, on a slower G5 it might be 56k, that to sounds normal.

Go Up