stepper motor control by Push Button

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 {
}
}

You are reading the state of the button pin then ignoring it. Why ?

What is connected to the Serial input ?

The ubiquitous pair (pinMode(buttonPin,INPUT); and if(buttonState==HIGH)) :slight_smile:
Do you have pullup or pulldown resistors on input pins to keep the pins from "floating" and causing false HIGHs or LOWs when the button is not pressed? Best way is like S3 in the diagram, no external resistor needed, logic is reversed though (pin is LOW when button is pressed).

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks... Tom... :slight_smile: