Help with Stepper Motor Code

I just purchased an Arduino and Motor controller from Adafruit along with a stepper motor. I have everything hooked up and working. The problem is I am having trouble with the code since I am not really a programmer.

I wanted to have the servo go forward a number of revolutions, stay there for a few hours and then reverse a number of revolutions back to its original position. The application would be to lower a small shelf, sit for awhile and then return back into the ceiling. I would also like to use a push-button to override the timer and make the shelf go up or down.

I cannot figure out a few methods. Such as the timer ? Is there a 24 hr. timer built into the Arduino ? Also the ability to make the stepper motor turn a number of revolutions. Are there preset calls/programs that in the Arduino code to say ex. “number of forward rev = 100”.

Any help much appreciated.

Jim

My Flow would be:

  1. Timer sets off at 6:00pm

  2. Servo goes forward direction a number of turns and drops shelf

3.Timer sets off at 8:00pm

  1. Servo goes backward direction a number of turns shelf returns back to ceiling.

5 Timer sets off at 6:00pm

6.Process repeats

  1. Ability to override timer by pushing one of two switches. Switch one, UP, switch two Down.

Another perfect application for a finite state machine. I happen to be working on an Arduino centric tutorial on this very subject. In the mean time, search this forum for FSM and goggle it as well.

The Arduino does not have a real time clock, but it does have a timer (millis) that tell you the number of milliseconds since it was turned on. If you need real time, a small RTC module can easily be interfaced to the Arduino.

(And if you bought all this stuff from AdaFruit, why ask here? :slight_smile: )

Thanks for your help. I did a google search and you are 100% correct my application mimics an FSM. Also thanks for you suggestion on the RTC makes sense.

As for the post great talent hangs around all over the place.

Jim

P.s. I have a great application for your tutorial !

Glad to be of some help. Make sure to come back if you have specific problems as you progress your project.

Skye,

Thanks for your offer and I will take you up on it. My son and I have been playing with code that comes with the motor shield and we have a understanding of it (listed below). I have a question about the libraries and functions as listed in http://arduino.cc/en/Reference/Stepper. Looking at the test stepper code provided by Adafruit we get the forward, backward,steps etc. but how would we stop(brake) the motor, or count revolutions. Are there functions for this as in “forward/backward” to do braking , etc. ? or does this require some type of custom code ??

Also I found this website http://arduino.cc/playground/Main/CustomStepper with custom code, but when I ran it there were constant undefined errors even though i loaded the library.

Thanks for your help !!

Jim

// Adafruit Motor shield library

// copyright Adafruit Industries LLC, 2009

// this code is public domain, enjoy!

#include <AFMotor.h>

// Connect a stepper motor with 48 steps per revolution (7.5 degree)

// to motor port #2 (M3 and M4)

AF_Stepper motor(48, 2);

void setup() {

Serial.begin(9600); // set up Serial library at 9600 bps

Serial.println(“Stepper test!”);

motor.setSpeed(10); // 10 rpm

}

void loop() {

Serial.println(“Single coil steps”);

motor.step(100, FORWARD, SINGLE);

motor.step(100, BACKWARD, SINGLE);

Serial.println(“Double coil steps”);

motor.step(100, FORWARD, DOUBLE);

motor.step(100, BACKWARD, DOUBLE);

Serial.println(“Interleave coil steps”);

motor.step(100, FORWARD, INTERLEAVE);

motor.step(100, BACKWARD, INTERLEAVE);

Serial.println(“Micrsostep steps”);

motor.step(100, FORWARD, MICROSTEP);

motor.step(100, BACKWARD, MICROSTEP);

}

The first thing to realize is that a stepper is unlike a DC motor. On a DC motor when you stop it, you remove power from the comutator and hence the ability to brake or coast the motor. In a stepper, you rarely remove power from the coils. If you did, the motor could cog to a new position without you knowing it. Hence you are always “braking” a stepper.

Second, no two stepper motor controllers or SW are the same. But for the most part, the library gives to a method to cog it CW or CCW by one full step or by one microstep. It may be that they give you a routine to cog it by n steps as well. But the total number of steps you move is normaly up to you to keep track of.

I can’t comment directly on the libraries you pointed to. Perhaps someone else with direct experience will jump in.

Skye,

Thanks again, we are making progress on the code and I may have another question soon.

Thanks !!

Jim