I'm trying to send data from a processing sketch to my Arduino Uno, using serial, in order to control the speeds of two motors, that need to run at separate speeds. I'm using the controlP5 library for processing with two sliders ranging from 0 to 255. What I would like to achieve is for my computer to send data prefixed by 'L' or 'R' to indicate the motor and the value of the speed desired, e.g 'L127'.

I have been looking for a while and I can't figure how to do this. I'm not fully sure of the difference between strings and bytes or which would be better for my task?

If you could point me in the direction of two things that would be great:

1. How to send this data from processing?2. How to interpret this data on the arduino, so basically decide which motor to affect by the 'L' or 'R' and then apply the value.

I'm reasonably new to Arduino, so bear with me! Would I initialize an integer for the motor speed and use for example "rightmotorspeed = atoi (serialreadvalue)"? How would I then determine between the left and right motor? What function would be required for this?

Would I initialize an integer for the motor speed and use for example "rightmotorspeed = atoi (serialreadvalue)"?

That depends on that serialreadvalue is. If it is an array of chars, and is properly null terminated, yes. If not, no.

Quote

How would I then determine between the left and right motor? What function would be required for this?

Again, this depends on how you are getting the serial data. If the data is stored in a NULL terminated array of chars, then strlen() on that array will give the length. The upper index is then one less than the length. The character at that position will be 'L' or 'R'.

Thanks for all your help so far. I'm trying to use the atoi function as suggested by PaulS, but I'm having some problems.I've decided to send the data from Processing to the arduino in this format: "127,255" with 127 and 255 being the speeds of the left and right motor respectively, so it only sends one bunch of serial data for both motors.

This may sound a bit cheeky, but can you see any problems with my code, unfortunately I don't have my Arduino at home at the moment, and I'm dying to know whether it will work!

// We are here either because all pending serial // data has been read OR because an end of // packet marker arrived. Which is it? if(started && ended) { // The end of packet marker arrived. Process the packet

// Reset for the next packet started = false; ended = false; index = 0; inData[index] = '\0'; }}To read the data. Where it says "Process the packet" is where you should use strtok() and atoi() to extract "100" and "150" as tokens, and convert them to 100 and 150.