Simple Step Motor Push Button Help!

Hello all,

This is my first Arduino so please forgive me I'm very new to all this. I am trying to use this stepper motor and a push button so that I can push the button once to move the stepper x degrees left and push it twice to move it x degrees right (basically to flip a little switch). I don't know why, but I haven't had any issues programming so far but I have been with this one. Whenever I power the stepper and the Arduino up, the stepper just starts moving and then delaying a second and repeating (see the code). I haven't starting on working on the whole push twice thing, I just have been trying to get it to at least move when the button is pushed.

Thanks so much for any help.

#include <Stepper.h>


#define STEPS 32

const int buttonPin = 12;
int Steps2Take;
int buttonState = 0;

Stepper small_stepper(STEPS, 8, 10, 9, 11);


void setup() {
  Serial.begin(9600);
  pinMode(buttonPin, INPUT);
  
}

void loop() {
  buttonState = digitalRead(buttonPin);
  
  if (buttonState == HIGH) {
    Serial.println("Motor On!");
    small_stepper.setSpeed(700);
    Steps2Take = 2048;
    small_stepper.step(Steps2Take);

    delay(1000);
  }

  else {
    small_stepper.setSpeed(0);
    Serial.println("Motor Off!");
  }

}

I probably embedded the code wrong, so sorry about that it is my first time on the forum

You did a great job embedding the code but you told us nothing about your hardware (which Arduino? What shield? How is the Arduino powered? How is the step motor powered?) and nothing about how the pushbutton is wired or what is being printed.

You probably want to do something when the pushbutton goes from LOW to HIGH, not just do something whenever the pushbutton is HIGH. The Arduino IDE has a State Change Detection example that is helpful with this.

Again: How is the pushbutton wired?

Avoid the delay(...) function. delay(1000) wastes the considerable power of your Arduino for a whole second.

Start now to get iinto the habit of naming your variables using "camel case" that starts with a lower case letter. Thus, Steps2Take should be stepsToTake and small_stepper should be smallStepper. Your way is not an error, but the suggested way will make it easier to get help from experts.

Thank you so much for the reply. It is the Arduino UNO, there are no shields on it, it is powered via USB and same for the stepper. Also, I uploaded one of the IDE tests that is the one where it goes through each individual step to ensure that your motor is wired up correctly, and in fact it is so that is worth noting. So I have 5V running from the Arduino to the breadboard and going diagonally across the switch, the jumper wire then leads to pin 12 which I set as an input.

Great to know about the delay function, what do you use instead of that? I will definitely start using camel case as well thank you.

Thanks so much! Any help is greatly appreciated.

.. still we need to know what stepper motor you connected.
When I read you have no shield in between the Uno and the motor I don't feel comfortable.
Unless you got a very very tiny stepper, your Arduino won't never be able to drive that stepper directly.

Arduino pins can only deliver up to 40mA current and a stepper coil normally draws a multiple of that to run.
In addition pls upload one or more pictures of your wiring (Arduino, motor, power supply).

This is not helpful:

So I have 5V running from the Arduino to the breadboard and going diagonally across the switch, the jumper wire then leads to pin 12 which I set as an input.

Switches can be very different from each other, so diagonally means nothing.
What is "the jumper wire"?
I see nothing about a resistor and the code did not show INPUT_PULLUP.
A schematic or other wiring diagram would help.
Also, an Arduino cannot drive most stepper motors directly.

delay(...) can be handy for debugging but should be avoided in most other cases. It is better to note the time of an interesting event using millis() (store the result into an unsigned long variable), and, each time that loop() is called, check whether sufficient time has passed to do something:

  if (millis()-interestingEventTime>=sufficientTime) { //do something }

Only use subtraction! Never addition!

Gammon Forum : Electronics : Microprocessors : millis() overflow ... a bad thing? explains why in detail.

Oh sorry, the stepper is separately powered with a little power supply (like the ones in the Arduino kits) and it is wired up with the little motor controller. I am sorry you're right I did not specify the switch, when it is closed, it closes the wires across from each other. The stepper motor is one of those generic 5V ones that come in the kits as well.

I am trying to just get an led to work with the push button and I can't even get that to work! I used one of the examples as mentioned above and there's like a long delay when trying to count the pushes, and sometimes it doesn't even count them.

Tried to do a simple setup but the led is always on, 220 ohm resistor on led and then I have 5V running to the push button and then a 10K resistor from there to Arduino.

const int led = 9;
const int button = 2;

void setup() {
  // put your setup code here, to run once:
  pinMode(led, OUTPUT);
  pinMode(button, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (digitalRead(button) == HIGH) {
    digitalWrite(led, HIGH);
  }
  else {
    digitalWrite(led, LOW);
  }
}

I have no clue why this doesn't even work. Someone please help!

One pushbutton terminal (B1) is already connected to 5V. So connect the 10K resistor from the other terminal (B2) to GND.
(this pulls the voltage to GND when the button is open)

Now connect B2 to Arduino pin 2.
(done)

How do you know which is B1 and B2? Sorry I don't know:/

I just assigned names to the button pins (for clarity?). Re-worded:

One pushbutton terminal is already connected to 5V. So connect the 10K resistor from the other button terminal to GND. (this pulls the voltage to GND when the button is open) Now also connect the other button terminal to Arduino pin 2. (done)

Tried this and when it turns on the motor just spins, even without the button being pressed. Can someone please help me wit this? I am sure the problem is an easy fix but I am just missing it...sorry I am very new to Arduino UNO.

#include "Stepper.h"

#define STEPS  32  
int  Steps2Take;
int buttonPin = 2;
boolean buttonState = LOW;
int motorEnabled = 0;
boolean previousButtonState = LOW;


Stepper small_stepper(STEPS, 8, 10, 9, 11);

void setup() {
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);

}

void loop() {
  buttonState = digitalRead(buttonPin);
  
  if(previousButtonState != buttonState && buttonState == HIGH){
    motorEnabled = !motorEnabled;
  }

  if(motorEnabled == 1){
    small_stepper.setSpeed(700); 
    Steps2Take  =  512;  
    small_stepper.step(Steps2Take);            //motor on here
    Serial.println("Motor ON!");
  }

  else{
    small_stepper.setSpeed(0);        //turn it off
 
  }

  previousButtonState = buttonState;
}

Any success with your code (and button connections) in reply#7?

Yes it was the same thing that happened. It has to do with reading the input voltage because I checked the voltage with my meter and the button works and all and the voltage flows thru the button back into the arduino.

It works on my Uno R3. Perhaps there's a problem with your Uno or connections. Could you post an image of your circuit and a circuit diagram?

It's not a brand arduino it's probably some kind of issue with it like you said because my connection has been fine uploading it and for he life of me I can't figure this out!

Try this to test your input pin 2 and output pin 9:

Disconnect the pushbutton and resistor.
Just use a jumper wire connected to pin 2.
After uploading the code below, the built-in LED and the LED on pin 9 should both be ON.
Connect the other end of the jumper wire to GND.
The built-in LED and the LED on pin 9 should both be OFF.
Disconnect the jumper wire from GND.
The built-in LED and the LED on pin 9 should both be ON.

const int led = 9;
const int button = 2;

void setup() {
  // put your setup code here, to run once:
  pinMode(led, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(button, INPUT_PULLUP);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (digitalRead(button) == HIGH) {
    digitalWrite(led, HIGH);
    digitalWrite(13, HIGH);
  }
  else {
    digitalWrite(led, LOW);
    digitalWrite(13, LOW);
  }
}