How to stop and start a stepper (EasyDriver)

I am trying to control a stepper motor and I am using an EasyDriver board. How can I start and stop the stepper motor using a button as an input?
My stepper motor is running fine if I tell it to turn i.e 4000 steps but I am failing to let it run until it hits a sensor or a button.

I have attached my code. The idea is to run the stepper when the button connected to pin 7 is pressed but nothing is happening.
Please advice.

int dirpin = 2;
int steppin = 3;
int button = 7;

void setup() {

pinMode(dirpin, OUTPUT);
pinMode(steppin, OUTPUT);
pinMode(steppin, INPUT);

}
void loop()
{

digitalRead(button);

if (button == HIGH)
{

int i;
digitalWrite(dirpin, HIGH);
for (i = 0; i<1; i++)
{
digitalWrite(steppin, LOW);
digitalWrite(steppin, HIGH);
delayMicroseconds(200);
}
}
}

digitalRead(button);

digitalRead returns a value (the state of the switch). You throw that value away.

int button = 7;
if (button == HIGH)

Should be fairly obvious that button is not equal to HIGH.

for (i = 0; i<1; i++)
   {
     digitalWrite(steppin, LOW);  
     digitalWrite(steppin, HIGH);
     delayMicroseconds(200);
   }

What’s the point in having a for loop that executes exactly once?

Ah, ok I got it.

Its working now, thanks for the reply.
How do I post my code with a grey background?

int dirpin = 2;
int steppin = 3;
int button = 7;
int val = 0;

void setup() {

pinMode(dirpin, OUTPUT);
pinMode(steppin, OUTPUT);
pinMode(button, INPUT);

}
void loop()
{

val = digitalRead(button);
int i;
if (val == HIGH)
{

digitalWrite(dirpin, HIGH);
for (i = 0; i<1; i++)
{
digitalWrite(steppin, LOW);
digitalWrite(steppin, HIGH);
delayMicroseconds(200);
}
}
}

Found it :wink:

int dirpin = 2;
int steppin = 3;
int button = 7;
int val = 0;


void setup() {


  pinMode(dirpin, OUTPUT);
  pinMode(steppin, OUTPUT);
  pinMode(button, INPUT);

}
void loop()
{

  val = digitalRead(button);

  if (val == HIGH)
  {
    int i;

    digitalWrite(dirpin, HIGH);   
    for (i = 0; i<1; i++)
    {
      digitalWrite(steppin, LOW);  
      digitalWrite(steppin, HIGH);
      delayMicroseconds(200);
    } 
  }
}