Can't get stepper motor to work in a specific situation

Hi,
So I'm writing code that will move a stepper motor a given amount. I am using a easy driver (EasyDriver - Stepper Motor Driver - ROB-12779 - SparkFun Electronics), a working four wire stepper motor, and an Arduino Uno. I am also using an 16 pin LCD screen. All these components work because I am able to control the stepper motor or write text to the LCD using various other code.

Goal:
Here's what I want the buttons to do. I also want the current step count to be reflected on the LCD.
I have 5 buttons.
Button 1: +1 step
Button 2: -1 step
Button 3: +10 steps
Button 4: -10 steps
Button 5. Actually move stepper.

Problem:
Buttons 1 through 4 do work. Currently I can make the number go up or down at will. The Uno also registers button 5 has been pressed. However, when I hold button 5, the stepper motor does not move. I don't know what my problem is. I'll copy paste my code below, I've also attached it.

I realize I'm asking alot but I would really appreciate your help with this, I'm pretty much stuck.
Thanks,
Brian

#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
int switchState1 = 0;
int switchState2 = 0;
int switchState3 = 0;
int switchState4 = 0;
int switchState5 = 0;
int prevSwitchState1 = 0;
int prevSwitchState2 = 0;
int prevSwitchState3 = 0;
int prevSwitchState4 = 0;
int prevSwitchState5 = 0;
int stepdis = 0;
int dis = 0;
const int switchPin1 = 6;
const int switchPin2 = 7;
const int switchPin3 = 8;
const int switchPin4 = 9;
const int switchPin5 = 10;


void setup(){
  lcd.begin(16,2);
  pinMode(switchPin1,INPUT);
  pinMode(switchPin2,INPUT);
  pinMode(switchPin3,INPUT);
  pinMode(switchPin4,INPUT);
  pinMode(switchPin5,INPUT);
  lcd.print("Measurement Sys");
  lcd.setCursor(0,1);
  lcd.print("1.0");
  
  

  pinMode(0, OUTPUT);     
  pinMode(1, OUTPUT);
  digitalWrite(0, LOW);
  digitalWrite(1, LOW);
}
void loop() {

  switchState1 = digitalRead(switchPin1);
  switchState2 = digitalRead(switchPin2);
  switchState3 = digitalRead(switchPin3);
  switchState4 = digitalRead(switchPin4);
  switchState5 = digitalRead(switchPin5);

 if (switchState1 != prevSwitchState1) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Steps");
    lcd.setCursor(0, 1);
    lcd.print(dis);

    if (switchState1 == HIGH) {

      int ndis = dis + 1;
      dis = ndis;         
    }
  }
   if (switchState2 != prevSwitchState2) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Steps");
    lcd.setCursor(0, 1);
    lcd.print(dis);

    if (switchState2 == HIGH) {

      int ndis = dis - 1;
      dis = ndis;         
    }
  }
   if (switchState3 != prevSwitchState3) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Steps");
    lcd.setCursor(0, 1);
    lcd.print(dis);
 
    if (switchState3 == HIGH) {

      int ndis = dis + 10;
      dis = ndis;         
    }
  }
     if (switchState4 != prevSwitchState4) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Steps");
    lcd.setCursor(0, 1);
    lcd.print(dis);


    if (switchState4 == HIGH) {

      int ndis = dis - 10;
      dis = ndis;         
    }
  }
     if (switchState5 != prevSwitchState5) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Moving...");
    lcd.setCursor(0, 1);
    lcd.print(dis);

    if (switchState5 == HIGH) {

          digitalWrite(1, HIGH);
          delayMicroseconds(100);          
          digitalWrite(1, LOW); 
          delayMicroseconds(100);
          stepdis = stepdis + 1;   
          if (stepdis == dis)
          {
            digitalWrite(0,LOW);
            digitalWrite(1,LOW);
          }
          
    }
  }

 prevSwitchState1 = switchState1;
 prevSwitchState2 = switchState2;
 prevSwitchState3 = switchState3;
 prevSwitchState4 = switchState4;
 prevSwitchState5 = switchState5;
 
}

lcdmeasurement.ino (3.72 KB)

(This is how you should include code use the #-button above the text edit box)

if (switchState5 == HIGH) {
      // move the cursor to the second line
          digitalWrite(1, HIGH);
          delayMicroseconds(100);         
          digitalWrite(1, LOW);
          delayMicroseconds(100);
          stepdis = stepdis + 1;   
          if (stepdis == dis)
          {
            digitalWrite(0,LOW);
            digitalWrite(1,LOW);
          }

That code is missing something to decide if we are going + or -.
As long as button 5 is held down it will toggle pin 1 at 5 Khz - possibly too fast. Your test for stepdis==dis is after you HAVE made a step - wrong logic? You sure you have not mixed up direction and step in your pinout? that is usually the reason "it wont move" Unless you are really desperate short of pins do not use pin 0 or 1 as this is "in parallel" with the USB/serial interface. digital pin 14 is the same as the A0 pin, but in digital form. Lastly but not at least - the top comment in above snippet is wrong -presumably the stepper is not moving any cursor(s). Wrong comments can be really confusing.

Thanks for the info. Looking into what you said, but I just want to apologize for the comments. I retrofitted an old program and I didn't delete the comments because for awhile they were useful. Thanks!

Thanks Msquare, just some followup questions.

How do I get pin 1 to toggle at a slower frequency, and what frequency would you recommend?
I am fairly certain my pinout is correct, I have run another program that oscillates the stepper back and forth, and that worked.

Here is my logic regarding the stepdis == dis

When button 5 is pressed, it writes digital pin 1 high, waits then writes it low. It records that it made that step (stepdis = stepdis +1)
then it checks to see if it has arrived in the desired position using the if statement. If the stepdis (steps traveled) = dis (desired steps to travel) then set pins 0 and 1 to LOW.

P.S. I am changing the pins from pin 0 and 1.
Thanks for the help!

brianb90:
When button 5 is pressed, it writes digital pin 1 high, waits then writes it low. It records that it made that step (stepdis = stepdis +1)
then it checks to see if it has arrived in the desired position using the if statement. If the stepdis (steps traveled) = dis (desired steps to travel) then set pins 0 and 1 to LOW.

So what happens if I have not done any +/- 1 or +/- 10 on the buttons and then push #5? It will do a step (as the only condition for doing a step is that pushbutton 5 is down - as far as I saw) and then it will probably conclude that dis is no longer like stepdis and thus continue.... ahh... no... now I see our common mistake. The outer if checks if button 5 has changed state. And if we keep button 5 depressed it will not have changed state and we will not enter the loop. Ie for each button 5 press the stepper will only move one step. (Apart from button bounce which may make it do an down/up 2 .. 5 times, each time moving one step. Depends on your button quality) So you ned an appropiate for loop and you need to set the direction pin before the step according to something like dis<stepdis.

How do I get pin 1 to toggle at a slower frequency, and what frequency would you recommend?

increase the delayMicroseconds(). If your circuitry has microstepping then 5K may work at 16th step, but if you do full steps then 1K would be fast. Depends on the mechanical load/motorsize. I would do delay(500) in my first version (ie 1Hz steps) to count each step

You do things like
const int switchPin1 = 6so why not

const int StepDirPin = ...

And clean up the errenous comments. When/if you post a new version, do put the code in boxes like every other good person does. Please.