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)