I'm using this coding so that my stepper motor can rotate clockwise with one button and counterclockwise with the other. Does this code work?
`#include <Stepper.h>
// constants won't change. They're used here to set pin numbers:
const int buttonPin1 = 2; // the number of the pushbutton pin
const int buttonPin2 = 3; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// for your motor
// variables will change:
int buttonState1 = 0; // variable for reading the pushbutton status
int buttonState2 = 0; // variable for reading the pushbutton status
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin1, INPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin2, INPUT);
// set 7the speed at 60 rpm:
myStepper.setSpeed(60);
// initialize the serial port:
Serial.begin(9600);
}
void loop() {
// read the state of the pushbutton value:
buttonState1 = digitalRead(buttonPin1);
// read the state of the pushbutton value:
buttonState2 = digitalRead(buttonPin2);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState1 == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
// step one revolution in one direction:
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
delay(0);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
// step one revolution in the other direction:
Serial.println("counterclockwise");
myStepper.step(0);
delay(500);
}
//second button
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState2 == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
// step one revolution in one direction:
Serial.println("counterclockwise");
myStepper.step(stepsPerRevolution);
delay(0);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
// step one revolution in the other direction:
Serial.println("clockwise");
myStepper.step(0);
delay(500);
}
}
`