Sketch architecture question

Hi all!

I'm trying to write a sketch that will take input from another computer in the form of serial messages and then use the rich I/O capability of the Arduino to perform motor control operations.

Currently I am writing a test sketch using the serial port, but will eventually move to the Arduino being a slave on an I2C or SPI bus.

My current difficulty is figuring out how to best architect the sketch for receiving commands and then executing the motor control sequensec contained in the commands. (Creating my own protocol to use for the motor control messages.)

I've done a fair bit of embedded work (but a LOT of years ago) written to the bare metal. For something like that I would code an interrupt handler to receive the characters over the comm channel and then set some sort of signal when a message was completely received. (Either set a flag or use a software interrupt.) But I am not certain how this can be accomplished within the Arduino environment.


What Nick Gammon is trying to say, is that there are libraries for the Arduino.
One of the most used library is the Serial library, to read and write data.

Yes, in case I seemed a little terse ... the Serial library already handles interrupts. Your challenge is to take the incoming text and turn it into something useful, like the commands of which you spoke. You could collect enough data until a command is received, either by looking for a terminator (eg. newline) or use a state machine.

My link above discusses both techniques.

Thanks for the input!

I had already implemented the initial technique in a test sketch. My problem began when I started researching the motor libraries and many of them did not say if the calls were blocking or not. I want to be able to have multiple motors running at the same time. This would be accomplished by sending multiple commands.

On a bit of a side note, does anyone know of a library that implements I2C slave functionality on an Arduino? I've done some Googling and have not had much luck. The one thing I did find seemed to be fairly vague.


What kind of motors?

Looking at making it general purpose, so DC, Stepper, and Servo

Looking at making it general purpose, so DC, Stepper, and Servo

Only the Stepper library is blocking, since it needs to step the required number of times, with a known interval between steps.

DC motors are just turned on or off.

Servos are non blocking, by design.

I2C as slave is done by the standard Wire library : Wire - Arduino Reference
Use Wire.begin() with address : Wire - Arduino Reference
And use OnRequest and OnReceive.