Starting and stopping a loop via a button

Hi guys,

Very new to this, so apologies for any idiotic questions I have.

I have an Arduino connected to a stepper motor driver which is driving a 12v stepper motor.

Using the following code I have the stepper motor successfully controlled via a potentiometer on a loop:

#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
void setup() {
 // nothing to do inside the setup
}
void loop() {
 // 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);
 }
}

I have 2 buttons each connected with a push down resistor to pins 7 and 8, as well as an LED connected to pin 13.

I would like to be able to use the button on pin 7 to start the loop above (as well as turn the LED on), and then pin 8 to end it (and turn the LED off), and be able to start it again on pin 7 but have no idea how to go about this.

Kind thanks for any help in advance :slight_smile:

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
 }
}

These links may be of interest

Stepper Motor Basics
Simple Stepper Code

...R

GoForSmoke:
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
}
}

Hi there, kind thanks for the reply and time.

I got a hold of some 1uF capacitors, and have put them across the switches (not entirely sure which way round these were supposed to go)

and I tried to compile the code you suggested, as shown below:

#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
int ledPin = 13; // set LED pin
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);
  }
  digitalWrite(ledPin, HIGH);
 }
 else
 {
  // turn the motor and led off
  int motorSpeed = 0;
  digitalWrite (ledPin, LOW);
 }
}

but unfortunately came to an error... exit status 1
'setMode' was not declared in this scope

Hope someone can help!

Thanks

Use pinMode() instead of setMode()

noweare:
Use pinMode() instead of setMode()

Thanks! That got rid of that error.

I tried recompiling but I end up with an error about reading the state of pins 7 and 8 with the code below producing the error:

sketch_new:24: error: expected ';' before ')' token

   pin7prevState = readState );

                             ^

sketch_new:34: error: expected ';' before ')' token

   pin8prevState = readState );

                             ^

exit status 1
expected ';' before ')' token
#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
int ledPin = 13; // set LED pin
byte processState;  // 0 is OFF, 1 is ON
byte pin7prevState;
byte pin8prevState;
byte readState;

void setup() 
{
 pinMode( 7, INPUT_PULLUP );  // wire this pin to a switch and the switch to ground
 pinMode( 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);
  }
  digitalWrite(ledPin, HIGH);
 }
 else
 {
  // turn the motor and led off
  int motorSpeed = 0;
  digitalWrite (ledPin, LOW);
 }
}

Thank you so much for the help everyone, very appreciative.

You seem to have a stray ) on line 35 of the code in Reply #5 which is confusing the compiler

...R

pin7prevState = readState );

That extra ) at the end should not be there. I can't post and have quick access to the compiler or I'd have gotten things like that out.

Sometimes that error will happen because a line above the one reported will have the problem to fix.