Hollay:
I'm going to run an astronomical barn door tracker with Arduino nano.
I wrote a sketch, it works so far but I'm not really satisfied with it.
The basic idea is the following:
if powered up, it has to run down (CCW) in parking position. After pushing lower limit switch it has to wait for the START button. If start pressed, it has to turn CW very slow (as it has to follow Earth's turn) until upper limit switch pushed. If pushed, a blinking LED alerts to finish long time exposure.
There is a finetuning included to set +/- the CW speed (it has to be set up while taking long expo photos not to see lines instead of stars due to wrong speed). Speed is saved in Arduino's EEPROM.
Hi Hollay,
I happen to be on a similar issue with these little steppers. I tried SmallSteppers ans AccelSteppers as well, but both didnt really fit my needs.
The following simple example uses Timer1 with two steppers. Within the timed routine it will run the steppers to the position given by steps_X/Y_goto.
#include <TimerOne.h>
byte stepper_sequence_clw[8] = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001};
byte stepper_sequence_ccw[8] = {B00001, B00011, B00010, B00110, B00100, B01100, B01000, B01001};
byte stepperX_spos,stepperY_spos,steps_X_step,steps_Y_step;
byte stepperX_pins[4]={2,3,4,5}; //Stepper X digital pins D2...D5 blue pink yellow orange, ULN2003 1-4
byte stepperY_pins[4]={6,7,8,9}; //Stepper Y digital pins D6...D9
byte stepperX_delay = 0; //X stepper max speed
byte stepperY_delay = 6; //Y stepper slow but with more torque
long int steps_X_goto = 0;
long int steps_Y_goto = 0;
long int steps_X_pos = 0;
long int steps_Y_pos = 0;
void setup(void) {
for(int i=0;i<4;i++) pinMode(stepperX_pins[i],OUTPUT);
for(int i=0;i<4;i++) pinMode(stepperY_pins[i],OUTPUT);
stepperX_spos=0;
stepperY_spos=0;
steps_X_step=0;
steps_Y_step=0;
Timer1.initialize(1250) ; //max speed / lowest torque for 28BYJ
Timer1.attachInterrupt(run_steppers);
}
void run_steppers(void){
doSteps();
//doBlinkLed or whatever
}
void doSteps(){ //Do not use delay here as well as in the called funcs
//having an endswitch it might be read here
//if set, either let goto = pos or set goto = pos = 0
if(steps_X_goto > steps_X_pos) doStepX(1,1);
if(steps_X_goto < steps_X_pos) doStepX(1,0);
if(steps_Y_goto > steps_Y_pos) doStepY(1,1);
if(steps_Y_goto < steps_Y_pos) doStepY(1,0);
}
void doStepX(byte s_step, byte s_direction){
if(!s_step) return;
if(++steps_X_step < stepperX_delay) return;
if(s_direction){
if(++stepperX_spos>7) stepperX_spos=0;
for(byte i=0;i<4;i++)
digitalWrite(stepperX_pins[i],bitRead(stepper_sequence_ccw[stepperX_spos],i));
steps_X_pos++;
} else {
if(++stepperX_spos>7) stepperX_spos=0;
for(byte i=0;i<4;i++)
digitalWrite(stepperX_pins[i],bitRead(stepper_sequence_clw[stepperX_spos],i));
steps_X_pos--;
}
steps_X_step = 0;
}
void doStepY(byte s_step, byte s_direction){
if(!s_step) return;
if(++steps_Y_step < stepperY_delay) return;
if(s_direction){
if(++stepperY_spos>7) stepperY_spos=0;
for(byte i=0;i<4;i++)
digitalWrite(stepperY_pins[i],bitRead(stepper_sequence_ccw[stepperY_spos],i));
steps_Y_pos++;
} else {
if(++stepperY_spos>7) stepperY_spos=0;
for(byte i=0;i<4;i++)
digitalWrite(stepperY_pins[i],bitRead(stepper_sequence_clw[stepperY_spos],i));
steps_Y_pos--;
}
steps_Y_step=0;
}
void loop(void) { //main loop, do non time critical things here
//set the steps to go
steps_X_goto = 4076; //go 1 revolution clockwise
steps_Y_goto = -4076; //and 1 rev counterclockwise
delay(1); //always have a little bit of delay in here
}
The timed function is called every 1250us, resulting to 8k steps per second or 1kHz per cycle ("wave"). In my experiments this gives the stepper a quite decent turn rate leaving at least some torque. For more torque stepperX/Y_delay can be set as plain divider, using a delay of 10 will set the stepper to 100Hz as rated in its specs - which is really slow but should deliver the steppers max torque.
Beneath the timer interrupt and thus no need of using delay() in stepper control this example uses two different wave "signatures" for running back and forth to pick up the exact step in sequence instead of running the whole cycle before changing direction.
Happy experimenting!
Regards, k4rma