Hello,
I'm using the Adafruit Motor Shield V2 for Arduino.
I would like to start and stop my stepper motors when a button is pressed/released. Running the motors has been easy, but I struggle with stopping them. This tutorial (https://learn.adafruit.com/adafruit-mot ... per-motors) mentions "release()" as method to stop motors. But I just get the following error message:
class 'AccelStepper' has no member named 'release'
Do you have all the libraries installed in "install software"?
Could you post your code and a description or drawing of your connections between it all?
Please use the code button as shown below.
#include <AccelStepper.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
Adafruit_MotorShield AFMSbot(0x61); // Rightmost jumper closed
Adafruit_MotorShield AFMStop(0x60); // Default address, no jumpers
Adafruit_StepperMotor *myStepper1 = AFMStop.getStepper(100, 1);
Adafruit_StepperMotor *myStepper2 = AFMStop.getStepper(100, 2);
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int buttonState = 0; // variable for reading the pushbutton status
// wrappers for the first motor!
void forwardstep1() {
myStepper1->onestep(FORWARD, DOUBLE);
}
void backwardstep1() {
myStepper1->onestep(BACKWARD, DOUBLE);
}
//wrappers for the second motor!
void forwardstep2() {
myStepper2->onestep(FORWARD, DOUBLE);
}
void backwardstep2() {
myStepper2->onestep(BACKWARD, DOUBLE);
}
AccelStepper stepper1(forwardstep1,backwardstep1);
AccelStepper stepper2(backwardstep2,forwardstep2);
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
AFMSbot.begin(); // Start the shield
stepper1.setMaxSpeed(50.0);
stepper1.setAcceleration(100.0);
stepper1.moveTo(300);
stepper2.setMaxSpeed(50.0);
stepper2.setAcceleration(100.0);
stepper2.moveTo(300);
}
void loop()
{
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
digitalWrite(ledPin, HIGH);
stepper1.run();
stepper2.run();
}
else {
digitalWrite(ledPin, LOW); // control LED off
stepper1.release(); // stop rotation and turn off holding torque. The code works well when these 2 lines are in comment
stepper2.release(); // stop rotation and turn off holding torque.
}
// Change direction at the limits
if (stepper1.distanceToGo() == 0)
stepper1.moveTo(-stepper1.currentPosition());
if (stepper2.distanceToGo() == 0)
stepper2.moveTo(-stepper2.currentPosition());
}
The AccelStepper class works as a wrapper around the Adafruit_StepperMotor class. The accel object tells the stepper object what to do, but doesn't control the motor directly.
else {
digitalWrite(ledPin, LOW); // control LED off
stepper1.release(); // stop rotation and turn off holding torque. The code works well when these 2 lines are in comment
stepper2.release(); // stop rotation and turn off holding torque.
}
else {
digitalWrite(ledPin, LOW); // control LED off
myStepper1.release(); // stop rotation and turn off holding torque. The code works well when these 2 lines are in comment
myStepper2.release(); // stop rotation and turn off holding torque.
}
#include <AccelStepper.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
Adafruit_MotorShield AFMSbot(0x61); // Rightmost jumper closed
Adafruit_MotorShield AFMStop(0x60); // Default address, no jumpers
// Connect two steppers with 200 steps per revolution (1.8 degree)
Adafruit_StepperMotor *myStepper1 = AFMStop.getStepper(100, 1);
Adafruit_StepperMotor *myStepper2 = AFMStop.getStepper(100, 2);
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int buttonState = 0; // variable for reading the pushbutton status
// wrappers for the first motor!
void forwardstep1() {
myStepper1->onestep(FORWARD, DOUBLE);
}
void backwardstep1() {
myStepper1->onestep(BACKWARD, DOUBLE);
}
//wrappers for the second motor!
void forwardstep2() {
myStepper2->onestep(FORWARD, DOUBLE);
}
void backwardstep2() {
myStepper2->onestep(BACKWARD, DOUBLE);
}
AccelStepper stepper1(forwardstep1,backwardstep1);
AccelStepper stepper2(backwardstep2,forwardstep2);
void setup()
{
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
AFMSbot.begin(); // Start the shield
stepper1.setMaxSpeed(50.0);
stepper1.setAcceleration(100.0);
stepper1.moveTo(300);
stepper2.setMaxSpeed(50.0);
stepper2.setAcceleration(100.0);
stepper2.moveTo(300);
}
void loop()
{
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == LOW) {
// Motors on :
digitalWrite(ledPin, HIGH); // control LED on
stepper1.run();
stepper2.run();
}
else {
digitalWrite(ledPin, LOW); // control LED off
myStepper1.release(); // stop rotation and turn off holding torque.
myStepper2.release(); // stop rotation and turn off holding torque.
digitalWrite(ledPin, LOW); // control LED off
}
// Change direction at the limits
if (stepper1.distanceToGo() == 0)
stepper1.moveTo(-stepper1.currentPosition());
if (stepper2.distanceToGo() == 0)
stepper2.moveTo(-stepper2.currentPosition());
}