…to continuously send sensor data over serial? I need to send analog pin readings as a string like this:
“0 analogRead(0) 1 analogRead(1)” etc. Right now, I can’t get the loop to execute faster than every 7 ms, and sometimes there’s a hiccup in the data flow, which makes my application unreliable. I’ve tried increasing the baud rate to 230400, but that doesn’t speed up the routine.
I would really love if I could bring the loop routine down to about 1 ms, so that the jitter would stay under 5 ms. I need the data from all 8 analog pins.
I’m currently using a char buffer and sprintf. And I’m a newbie in programming, so please excuse the clumsy wording.
Not enough details to give specific advice. My go to answer would be don't send ASCII. Pack the data in a struct and send / receive it as binary. But, need a lot more information in order to flesh that out.
At 115200 you can only transmit 11 characters in 1 ms. How many characters you are transmitting per analog? Multiply this by 8. Each character requires 10 bits to transmit.
Please explain why you think you need to send the data so fast, it seems unlikley to me.
Which Arduino are you using? sprintf() is not quick to execute.
If you really need the speed you have to slim down the data you sent, sending what you are suggesting has a lot of wasted characters in it, I doubt you really need to send 'analogRead' with every message, if it's always the same there is no point in sending it.
Thanks for the replies. Due to my inexperience, I wasn’t able to convey my goal in a clear way. I actually need to transmit the numbers only. An example of the data flow would be:
0 15 1 230 2 188 3 18 4 66 5 655 6 54 7 2
So, in theory, I would need to send eight 10-bit values (the analog sensors are 10-bit, aren’t they?) and a control bit/byte/whatever. I don’t really know how to go about it, sorry… I’m a musician trying to implement a responsive instrument. I’m using Max/MSP at the other side of the serial communication. I thought I would be able to figure out a way that’s faster than Firmata…
Other post/duplicate LOCKED !
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.
Continued cross posting could result in a time out from the forum.
Your example is 41 characters which is 410 bits. At 115200 bps that will take about 3.6ms. The way you are formatting it 1 analog could require up to 8 characters, which would require a message up to 64 characters or 640 bits. At 115200 bps that will take about 5.5ms.
Actually the analog values are 10 bits. 1023 requires 10 bits.
I'm not familiar with Max/MSP but if you can control how Max/MSP interprets the the message you could possibly transmit it in binary, making it more efficient. However, you would need to "frame" the message so it would be synced up. Otherwise the receiving software might try to start interpreting in the middle of a message.
I think it would be 8 values in 16 bytes...but same concept. If OP wanted to get real fancy OP could pack 8 values in 6 bytes and prefix it with a leading word of 0xFFFF.