Have you tested your code?
You're joking, right ? Do you think I would post untested code ? (never)
Here is your code from Reply#2 this post:
http://forum.arduino.cc/index.php?topic=263501.new;topicseen#new
// simple program that makes stepper move up or down depending on which button is pressed
unsigned int microsBetweenSteps = 2000;
byte buttonUpPin = 10;
byte buttonDownPin = 11;
byte dirPin = 5;
byte stepPin = 4;
byte buttonUp = HIGH;
byte buttonDown = HIGH;
void setup() {
pinMode(buttonUpPin, INPUT_PULLUP);
pinMode(buttonDownPin, INPUT_PULLUP);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
}
void loop() {
readButtons();
moveMotor();
}
void readButtons() {
buttonUp = digitalRead(buttonUpPin);
buttonDown = digitalRead(buttonDownPin);
}
void moveMotor() {
if (buttonUp == LOW) {
motorStep('F');
}
if (buttonDown == LOW) {
motorStep('R');
}
}
void motorStep(char dirn) {
if (dirn == 'F') {
digitalWrite(dirPin, HIGH);
}
else {
digitalWrite(dirPin, LOW);
}
digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, LOW);
delayMicroseconds(microsBetweenSteps);
}
Here is the OP's code (by Tom Ignoe)
/*
Stepper Motor Control - one revolution
This program drives a unipolar or bipolar stepper motor.
The motor is attached to digital pins 8 - 11 of the Arduino.
The motor should revolve one revolution in one direction, then
one revolution in the other direction.
Created 11 Mar. 2007
Modified 30 Nov. 2009
by Tom Igoe
*/
#include <Stepper.h>
const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// for your motor
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8,9,10,11);
void setup() {
// set the speed at 60 rpm:
myStepper.setSpeed(60);
// initialize the serial port:
Serial.begin(9600);
}
void loop() {
// step one revolution in one direction:
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
delay(500);
// step one revolution in the other direction:
Serial.println("counterclockwise");
myStepper.step(-stepsPerRevolution);
delay(500);
}
This is the OP's code with your button read code added and all your step and dir pin code and step clock code omitted.
/*
Stepper Motor Control - one revolution
This program drives a unipolar or bipolar stepper motor.
The motor is attached to digital pins 8 - 11 of the Arduino.
The motor should revolve one revolution in one direction, then
one revolution in the other direction.
Created 11 Mar. 2007
Modified 30 Nov. 2009
by Tom Igoe
*/
#include <Stepper.h>
//Motor A
int enableA = 5;
int pinA1 = 8;
int pinA2 = 9;
//Motor B
int enableB = 6;
int pinB1 = 12;
int pinB2 = 7;
const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// for your motor
unsigned int microsBetweenSteps = 2000;
byte buttonUpPin = 10;
byte buttonDownPin = 11;
byte dirPin = 5;
byte stepPin = 4;
byte buttonUp = HIGH;
byte buttonDown = HIGH;
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, pinA1,pinA2,pinB1,pinB2);
void setup()
{
Serial.begin(9600);
// set the speed at 60 rpm:
myStepper.setSpeed(60);
// initialize the serial port:
pinMode (enableA, OUTPUT);
pinMode (pinA1, OUTPUT);
pinMode (pinA2, OUTPUT);
pinMode (enableB, OUTPUT);
pinMode (pinB1, OUTPUT);
pinMode (pinB2, OUTPUT);
digitalWrite (enableA, HIGH);
digitalWrite (enableB, HIGH);
}
void loop()
{
readButtons();
}
void readButtons()
{
buttonUp = digitalRead(buttonUpPin);
if (buttonUp == LOW)
{
// step one revolution in one direction:
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
}
buttonDown = digitalRead(buttonDownPin);
if (buttonDown == LOW)
{
// step one revolution in the other direction:
Serial.println("counterclockwise");
myStepper.step(-stepsPerRevolution);
}
}
I don't understand why you have both
That is an EXCELLENT question !
See , I knew as soon as you asked that question that you had found a mistake I made when modifying the code to work with the OP's cheap driver that only has IN1, IN2, IN3, & IN4 and nothing else. I'm a hardware guy and when it comes to software I'm like a bull in a china shop. I went back and looked at it and then commented out the step clock code your were referring to and it STILL worked ! Obviously that means I should have omitted the step clock code along with the other code I omitted from your example when I added your button read code to the OP's example. Thanks for pointing that out. So yes, the only code that came from your example is the button read code but is that not the only reason for this post in the first place ? I don't think the OP has enough experience to realize how specific he needs to be when describing his design criteria so we don't really know if the code you wrote for a Pololu A4988 using step and dir inputs is what the OP wanted, but that's not your fault. So asuming it is what he wanted, all I did was make the example he posted work by adding your button code. You can accept credit for it or disavow it but either way I am not going to take credit for it because I didn't write it.
And yes it works (of course) or I wouldn't have posted it. I know from past experience that every post is like chumming for sharks.
Some times you get nothing and sometimes you get a feeding frenzy so you better not post any code that is bogus or you'll attract the sharks.... XD