Stepper motor with pot for speed selection and a toggle switch

Hi everyone,

I have a project where I am driving a stepper motor using an Adafruit Stepper driver. I have a potentiometer connected so the user can select from several different speeds. I also have a toggle switch in order to start and stop the program. However, I have only been successful with starting the program, not stopping it. I am also running into the issue of when the usb cord is detached, the arduino no longer works. This is inconvenient because I want the arduino and motor to work as a stand alone device. If you could help me reach a solution, I would greatly appreciate it!

Here is my code:

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"

Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_StepperMotor *s1 = AFMS.getStepper(200, 2);

int potPin = 0;
int steps = 75;

int buttonPin = 11;
int buttonState = LOW;
int lastButtonState = 0;
void setup() {

pinMode(potPin, INPUT);
int potValue = analogRead(potPin);

pinMode(buttonPin, INPUT);
int buttonState = digitalRead(buttonPin);

AFMS.begin();

}

void loop() {

int potValue = analogRead(potPin);
int buttonState = digitalRead(buttonPin);

if (buttonState != lastButtonState) {

if (lastButtonState == HIGH)
{
buttonState = LOW ;

}
else
{
(lastButtonState == LOW);
buttonState = HIGH ;
}

if (potValue >= 0 && potValue < 205)
{
s1->setSpeed(5);
s1->step(steps, FORWARD, INTERLEAVE);
s1->step(steps, BACKWARD, INTERLEAVE);
}
else if (potValue >= 205 && potValue < 410)
{
s1->setSpeed(30);
s1->step(steps, FORWARD, DOUBLE);
s1->step(steps, BACKWARD, DOUBLE);
}
else if (potValue >= 410 && potValue < 615)
{
s1->setSpeed(60);
s1->step(steps, FORWARD, INTERLEAVE);
s1->step(steps, BACKWARD, INTERLEAVE);
}
else if (potValue >= 615 && potValue < 820)
{
s1->setSpeed(90);
s1->step(steps, FORWARD, DOUBLE);
s1->step(steps, BACKWARD, DOUBLE);
}
else
{
s1->setSpeed(200);
s1->step(steps, FORWARD, INTERLEAVE);
s1->step(steps, BACKWARD, INTERLEAVE);
}

}
}

test72117.ino (1.5 KB)

For starters, upload your code using the code feature. Next your buttonstate and last button state should be booleans not integers. I'm doing this on my phone so if I mess something up, sorry. This should work. I'll write some mistakes under here so you can learn from them but i will definitely forget to write some. It's hard to keep going back and forth on a phone. Also my formatting will be wrong so if you copy and paste just click ctrl + T when you open it up

You don't need to write int before a variable in the loop if you are changing it.

Not really sure what's with that first block so I'll just Redo it.

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"

Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_StepperMotor *s1 = AFMS.getStepper(200, 2);


int potPin = 0;
int steps = 75;

int buttonPin = 11;
boolean buttonState = LOW;
boolean lastButtonState = LOW;
boolean buttonStart = LOW;
void setup() {

  pinMode(potPin, INPUT);
  potValue = analogRead(potPin);

  pinMode(buttonPin, INPUT);
  buttonState = digitalRead(buttonPin);

  AFMS.begin();


}


void loop() {

  potValue = analogRead(potPin);
  buttonState = digitalRead(buttonPin);

if ((buttonState == HIGH) && (lastButtonState == LOW))
{
lastButtonState = HIGH
}
else if ((buttonState == HIGH) && (lastButtonState == HIGH))
{
lastButtonState = LOW
}

if (lastButtonState == HIGH)
{
  if (potValue >= 0 && potValue < 205)
  {
    s1->setSpeed(5);
    s1->step(steps, FORWARD, INTERLEAVE);
    s1->step(steps, BACKWARD, INTERLEAVE);
  }
  else if ((potValue >= 205) && (potValue < 410))
  {
    s1->setSpeed(30);
    s1->step(steps, FORWARD, DOUBLE);
    s1->step(steps, BACKWARD, DOUBLE);
  }
  else if ((potValue >= 410) && (potValue < 615))
  {
    s1->setSpeed(60);
    s1->step(steps, FORWARD, INTERLEAVE);
    s1->step(steps, BACKWARD, INTERLEAVE);
  }
  else if ((potValue >= 615) && (potValue < 820))
  {
    s1->setSpeed(90);
    s1->step(steps, FORWARD, DOUBLE);
    s1->step(steps, BACKWARD, DOUBLE);
  }
  else
  {
    s1->setSpeed(200);
    s1->step(steps, FORWARD, INTERLEAVE);
    s1->step(steps, BACKWARD, INTERLEAVE);
  }


  }
}
}

That should do it so when you click the button and spin the pot, it will do whatever it did before, i didn't check that part since you said it worked. Click the button again and nothing will happen no matter what you do until you press the button again. It's basically a toggle between off and on. Hope that's what you were looking for. For the USB thing, it turns off because the Arduino is using the power from your computer, to use it standalone you will need to power the Arduino with 5v. You can use a 9v battery, or 4 AA batteries, whatever you want.

Hope I helped. Good Luck! :smiley:

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.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile:

NeilSawhney:
You don't need to write int before a variable in the loop if you are changing it.

That is not stated sufficiently strongly.

There MUST NOT be int before the name of a global variable when it is used in a function.

If there is int before the variable name the compiler will create a new local variable with the same name but no relation at all with the global variable.

...R

Robin2:
That is not stated sufficiently strongly.

There MUST NOT be int before the name of a global variable when it is used in a function.

If there is int before the variable name the compiler will create a new local variable with the same name but no relation at all with the global variable.

...R

Yup