Hi all, would anyone be interested In helping modify this code? First off, I did read the "Planning and Implementing an Arduino Program" written by Robin2 and did start to put together a program, then I noticed Robin2's "Simple Stepper Program" is very similar to my needs and much simpler then what i had created, so I thought it might be more practical just to adjust from his code.
Essentially I would like the clockwise button (buttonCW) to turn the motor at 9 RPM. I would like the counter clockwise button (buttonCCW) to spin at a faster arbitrary speed. (Whatever speed my battery voltage can support)
I would also like to add a home and limit switch to the device.
What I am using this for is a barndoor tracker. Basically when the motor runs CW at 9RPM the device will slowly move to track the sky. When the device reaches the end of it's mechanical limits it hits the "rewind limit switch" and this slews the motor counter clockwise until the device hits the "home"" limit switch. If all goes well the device is running again at 9RPM clockwise after it hits the home limit.
In theory I guess the whole operation could be controlled by just 2 limit switches, however I would like to have a couple of buttons to add control. Example at times I may need to rewind the device early, or perhaps pause the device.
In reality I suppose the "Rewind limit switch" and "Rewind" button could actuality be the same thing ie: 2 parallel switches wired to one pin on the Arduino.
I will be using the UNO, Pololu DRV8834 Low-Voltage Stepper Motor Driver Carrier, NEMA 17 200 step motor and 11.1v (10.8v) Lithium battery packs.
I would like to use 1/32 micro-stepping as well.
Anyhow I think that covers it, any help would be appreciated.
// testing a stepper motor with a Pololu A4988 driver board or equivalent
// this version uses millis() to manage timing rather than delay()
// and the movement is determined by a pair of momentary push switches
// press one and it turns CW, press the other and it turns CCW
byte directionPin = 9;
byte stepPin = 8;
byte buttonCWpin = 10;
byte buttonCCWpin = 11;
boolean buttonCWpressed = false;
boolean buttonCCWpressed = false;
byte ledPin = 13;
unsigned long curMillis;
unsigned long prevStepMillis = 0;
unsigned long millisBetweenSteps = 25; // milliseconds
void setup() {
Serial.begin(9600);
Serial.println("Starting Stepper Demo with millis()");
pinMode(directionPin, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(buttonCWpin, INPUT_PULLUP);
pinMode(buttonCCWpin, INPUT_PULLUP);
}
void loop() {
curMillis = millis();
readButtons();
actOnButtons();
}
void readButtons() {
buttonCCWpressed = false;
buttonCWpressed = false;
if (digitalRead(buttonCWpin) == LOW) {
buttonCWpressed = true;
}
if (digitalRead(buttonCCWpin) == LOW) {
buttonCCWpressed = true;
}
}
void actOnButtons() {
if (buttonCWpressed == true) {
digitalWrite(directionPin, LOW);
singleStep();
}
if (buttonCCWpressed == true) {
digitalWrite(directionPin, HIGH);
singleStep();
}
}
void singleStep() {
if (curMillis - prevStepMillis >= millisBetweenSteps) {
prevStepMillis += millisBetweenSteps;
digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, LOW);
}
}