Cannot get Motor to start "if.. else" cycle again after switch is flipped.

I am trying to get a toggle switch to trigger a stepper motor and control the amount of times it steps. The goal for my code is to turn the switch on, make the motor step 12 times, and stop by itself, then when i turn the switch back off then on again, the motor should step another 12 times. With my current code the motor will complete 12 steps when the switch is first turned on, but it does not reset when I turn the switch off and on again, and the step count goes back to 0, but turning the switch back on again does not make it increment from 0 to 12. I have tried adding a Boolean variable and using that for the switching case, but that gave me the same problem. I am not sure what I may have done, but it seems the code does not exit the final “else” statement once it has entered.

#include <LiquidCrystal.h>
#include <Stepper.h>

// LCD Display
LiquidCrystal lcd(8,9,10,11,12,13);

// Motor
int stepCount = 0;
const int stepsPerRevolution = 48;
Stepper myStepper(stepsPerRevolution, 2,3,4,5);

// Pushbutton for moving motor
int inPin = 6;
int reading;

void setup() {
// LCD
  pinMode(inPin, INPUT);
void loop() {
  lcd.print("Motor is: ");
  reading = digitalRead(inPin);
  if (reading == HIGH) {
   if (stepCount <= 12){
 }  else {
  else {
    lcd.print("idle      ");
    stepcount = 0;


Do you have a pull down resistor on the switch input?

Do you have a pull down resistor on the switch input?

Yes I have tried a circuit with a pull-up resistor, and another with it as a pull-down resistor. I believe once the stepCount goes to 0, it doesnt start increasing again when the switch is flipped the second time.

I have no experience with stepper motors, but is it because you set your speed to zero here?


So the next time it tries to move, it tries to move at a speed of zero which means it won't move.