It could use more cleaning up, it needs code to stop the motor and led where the comments says to.
It's not tested or even compiled but this should do the job.
Kudos to you for not having blocking code in the first place. It made this easy.
#include <Stepper.h>
const int stepsPerRevolution = 200; //number of steps per revolution
// initialize the stepper library on pins 2 through 5:
Stepper myStepper(stepsPerRevolution, 2, 4, 3, 5);
int stepCount = 0; // number of steps the motor has taken
byte processState; // 0 is OFF, 1 is ON
byte pin7prevState;
byte pin8prevState;
byte readState;
void setup()
{
setMode( 7, INPUT_PULLUP ); // wire this pin to a switch and the switch to ground
setMode( 8, INPUT_PULLUP ); // and put a 1 uF cap across both switches to debounce them
}
void loop()
{
readState = digitalRead( 7 );
if ( pin7prevState != readState )
{
pin7prevState = readState );
if ( readState == LOW ) // button down with INPUT_PULLUP drains the pin to ground.
{
processState = 1;
}
}
readState = digitalRead( 8 ); // note that the stop button is read last. press both makes stop
if ( pin8prevState != readState )
{
pin8prevState = readState );
if ( readState == LOW ) // button down with INPUT_PULLUP drains the pin to ground.
{
processState = 0;
}
}
if ( processState )
{
// read the sensor value:
int sensorReading = analogRead(A0);
// map it to a range from 0 to 100:
int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
// set the motor speed:
if (motorSpeed > 0)
{
myStepper.setSpeed(motorSpeed);
// step 1/100 of a revolution:
myStepper.step(stepsPerRevolution / 100);
}
}
else
{
// turn the motor and led off
}
}