I want to control Stepper motor with Push Button. Push-button input taken in PIN2 and power supplied in Motor with external 5 v source. When i m uploading code in arduino... it is automatically running a motor without push-button....plz help
#include <Stepper.h>
// Define number of steps per rotation:
const int buttonPin = 2; // input button pin
const int stepsPerRevolution = 2048;
int ledPin = 13;
Stepper myStepper = Stepper(stepsPerRevolution, 8, 10, 9, 11);
int buttonState=0;
void setup() {
myStepper.setSpeed(10);
pinMode(buttonPin,INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
buttonState=digitalRead(buttonPin);
//Serial.println(buttonState);
if (Serial.available() > 0)
//if(buttonState==HIGH)
{
Serial.println("clockwise");
digitalWrite(ledPin, HIGH);
myStepper.step(stepsPerRevolution);
delay(500);
//Step one revolution in the other direction:
Serial.println("counterclockwise");
myStepper.step(-2*stepsPerRevolution);
//delay(500);
}
else {
}
}