Running Stepper Motors with ofArduino

Hi All,

I’ve got standard firmata loaded on my Arduino which I’m controlling in OF, however to move my steppers I need to set a delayMicroceconds() between the HIGH and LOW calls.

It seems this functionality isn’t available in ofArduino. Can someone point out a workaround?

Here’s the arduino code which moves the motor by 400 steps:

for (int x = 0; x < 400; x++) {
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(350);
}

Thanks in advance,
Charles

HI Charles_Lucien,

there is ofSleepMillis(); or (the better way, as ofSleepMillis and delayMicorseconds are blocking) use Timers. But we are talking about veryshort times here. And you will need time for communication also.

I do not know if it possible at all, but - as steppers need those precise timing - I would not use OF for doing the timing. An OF cycle is much slower than an Arduino cycle and not as precise (your OF App might for example run suddenly slower, when something else on your Computer happens).
Probably the better approach would be to calculate in OF only where your stepper should run, then tell the Arduino this number (serial communication from OF to Arduino). And the let the Arduino do the actual work of moving the stepper there, including delays.

Would the be a good approach?
If yes and if you need help with this, tell me.

have a good day!
oe

Hi Dasoe,

You’ve coined my problem, and given the simplicity of ofArduino I was avoiding switching to ofSerial at all cost. Based on previous experience, I’ve found serial communication to be very tricky to get right.

Perhaps you could briefly highlight how this communication would be best established? Bearing in mind that the solution needs to be scalable, as I’ll be adding more motors down the line.

For efficiency I think the distance could be send as a single byte that the Arduino could make sense of. Lastly would a baudrate of 115200 be too high?

Thanks in advance,
Charles

Hi Charles_Lucien,

You are so very right, serial is a pain, especially if it goes direction OF -> Arduino. It works quite well for me with bakercps ofxSerial, though. There are included examples with Arduino codes. (Only check which branch you need, this is a bit confusing: for ofxIo, which you will need too, the stable branch, for ofxSerial the master branch if you work with OF 0.9). I would suggest to start there.

I worked with 115200 without problems. Although I think you will not need a high serial speed if you have a smart setting (sending only target motor settings whenever needed, Arduino is doing the actual calculation of steps, and drives the motors).

About the scalability: I would probably do something like sending a long hex string with target positions for every motor. First 2 bytes would be target position of motor 1, next 2 bytes target position of motor 2 and so on. So 4 Motors would be 8 bytes. By counting you can also get number of motors of course.
In Arduino you can split this string, figure all out (including how many motors and target position of each motor), remember the actual position of each motor, do the calculation of single steps and drive the motor. You will probably not have to change the code in arduino when adding motors if you write a smart Arduino Code.

does this help?

have a good day!
oe