Getting stepper motor to go back and forth when button is pressed

Hi Guys

I am trying to run my stepper motor move back and forth when a button is pressed. And stop when the button is pressed again.

I have attached my code below. \

#include <LiquidCrystal.h>

#define buttonpin 2 //

LiquidCrystal lcd(12,11,5,4,3,2); // LCD

int currentstate; // Defined values for psuh button
int prevstate = LOW;
int running =LOW;
long switchtime = 0; // once u push it
long t_bounce = 150;

int s_delay = 150/2; //This will define the motor
int runs =0;

int i;

unsigned long t_run = 0;

void setup()
{
pinMode(buttonpin, INPUT);
pinMode(6, OUTPUT); // connect to direction 4—> 6
pinMode(7, OUTPUT); // connect to step 5 —> 7
digitalWrite(6, LOW); // step down
digitalWrite(7, LOW);
lcd.begin(16,2);
}

void loop () {
currentstate =digitalRead(buttonpin);

if(currentstate == HIGH && prevstate ==LOW && millis () - switchtime >
if(running == LOW)
{
running = HIGH;
}
else
{
running=LOW;
}
switchtime=millis();
}

if(running == HIGH)
{
t_run = millis() - switchtime;
displayTime(t_run);

for(i=0;i<5000;i++){
digitalWrite(7,HIGH);
delayMicroseconds(s_delay);
digitalWrite(7,LOW);
delayMicroseconds(s_delay);
}

if(digitalRead(6) == LOW)
{
digitalWrite(6,HIGH);
runs = runs + 1;
}
else
{
digitalWrite(6,LOW);
runs = runs + 1;
}
}
if(t_run>600000){
running=LOW;
}
prevstate=currentstate;
}

void displayTime(long r){ // use to convert millis to hour
char buffer[20];
int mins = r/60000;
r = r%60000;
int ssecs = r/1000;
sprintf(buffer,"%02d:%02d",mins,secs);

lcd.setCursor(0,0);
lcd.print(runs);
lcd.setCursor(0,1);
lcd.write("Time: ");
lcd.write(buffer);
}

You have some syntax errors.
I commented out the LCD related lines as I don’t have that library, and fixed the line I think has problems.
Try uncommenting the LCD lines and try it:

//#include <LiquidCrystal.h>

#define buttonpin 2 // 

//LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // LCD

int currentstate; // Defined values for psuh button
int prevstate = LOW;
int running = LOW;
long switchtime = 0; // once u push it
long t_bounce = 150;

int s_delay = 150 / 2; //This will define the motor
int runs = 0;

int i;

unsigned long t_run = 0;

void setup()
{
  pinMode(buttonpin, INPUT);
  pinMode(6, OUTPUT); // connect to direction 4---> 6
  pinMode(7, OUTPUT); // connect to step      5 ---> 7
  digitalWrite(6, LOW); // step down
  digitalWrite(7, LOW);
  //lcd.begin(16, 2);
}

void loop () {
  currentstate = digitalRead(buttonpin);

  if ((currentstate == HIGH) && (prevstate == LOW) && (millis () - switchtime)) {
    if (running == LOW)
    {
      running = HIGH;
    }
    else
    {
      running = LOW;
    }
    switchtime = millis();
  }


  if (running == HIGH)
  {
    t_run = millis() - switchtime;
    displayTime(t_run);

    for (i = 0; i < 5000; i++) {
      digitalWrite(7, HIGH);
      delayMicroseconds(s_delay);
      digitalWrite(7, LOW);
      delayMicroseconds(s_delay);
    }

    if (digitalRead(6) == LOW)
    {
      digitalWrite(6, HIGH);
      runs = runs + 1;
    }
    else
    {
      digitalWrite(6, LOW);
      runs = runs + 1;
    }
  }
  if (t_run > 600000) {
    running = LOW;
  }
  prevstate = currentstate;
}


void displayTime(long r) { // use to convert millis to hour
  char buffer[20];
  int mins = r / 60000;
  r = r % 60000;
  int secs = r / 1000;
  sprintf(buffer, "%02d:%02d", mins, secs);

  //lcd.setCursor(0, 0);
  //lcd.print(runs);
  //lcd.setCursor(0, 1);
  //lcd.write("Time: ");
  //lcd.write(buffer);
}

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
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.

Thanks.. Tom... :slight_smile: