I haven't purchased an Arduino yet (but I am soon!). However I'm doodling and having fun coming up with project ideas. One involves a step motor from a computer floppy drive.
Is it possible to tell a step motor to step in reverse?
The idea: An automatic fund raising thermometer! You know, those goal sheets people fill in with their markers? This one will connect to the internet and move based on a web service. I'd be open to any other brainstorms related to this thought.
Well, if you need to run your "fundraising thermometer" backwards, you should probably be more concerned about finding security people to investigate your staff than designing your stepper driver ;D
Here's a simple piece of code that will give you the general idea of how to run motors in both directions. It assumes you're using a driver IC (like an L293D) that needs two outputs to control the two coils of a bipolar stepper. You'll need to tweak it to match your setup, and may need to add some more code for features like the "enable" input on some chips. But it shows some useful principles (like using tables to define the sequence of outputs for stepping). To go in one direction, you increment the table index. To go in the other, you decrement it.
// Change these lines to select the desired pins
#define MOTOR_1_PIN_A 9
#define MOTOR_1_PIN_B 10
const int Pin_A_seq[4] = { HIGH, HIGH, LOW, LOW } ;
const int Pin_B_seq[4] = { LOW, HIGH, HIGH, LOW } ;
byte Motor_1_index = 0;
void setup()
{
pinMode(MOTOR_1_PIN_A, OUTPUT);
pinMode(MOTOR_1_PIN_B, OUTPUT);
digitalWrite(MOTOR_1_PIN_A, HIGH);
digitalWrite(MOTOR_1_PIN_B, LOW);
}
void loop()
{
/*
* Insert code here that will call motor_1_left and motor_1_right
* to spin the motor as desired
*/
}
// The "& 3" in the motor movement routines constrains the
// value of Motor_1_index to a range of 0 through 3, wrapping
// it correctly when it goes out of range.
void motor_1_left()
{
Motor_1_index = (Motor_1_index - 1) & 3;
digitalWrite(MOTOR_1_PIN_A, Pin_A_seq[Motor_1_index]);
digitalWrite(MOTOR_1_PIN_B, Pin_B_seq[Motor_1_index]);
}
void motor_1_right()
{
Motor_1_index = (Motor_1_index + 1) & 3;
digitalWrite(MOTOR_1_PIN_A, Pin_A_seq[Motor_1_index]);
digitalWrite(MOTOR_1_PIN_B, Pin_B_seq[Motor_1_index]);
}
Ha! Yeah hopefully the meter will keep going up. However if I tied the thermometer to a retirement plan, the reverse option would be used more often these days. Or... if a new fundraiser started, you could go back to 0 remotely using the Ethernet shield;
Thanks much for the example. I'll play around with it as soon as I get a hold of the hardware. I don't mind posting the final results if I get something working.
Yes, if you reverse the control sequence that steps the motor forwards, it will step backwards. To make your thermometer, why not get a scrap scanner (parallel port scanners are often available for free on FreeCycle) and use the scan-head movement stepper motor? That way, you get the rotary-to-linear movement mechanism for free!