/*
This is a test sketch for the Adafruit assembled Motor Shield for Arduino v2
It won't work with v1.x motor shields! Only for the v2's with built in PWM
control
For use with the Adafruit Motor Shield v2
----> http://www.adafruit.com/products/1438
*/
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
int buttonPin1 = 2 ;
int buttonPin2 = 3 ;
bool runState = false;
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61);
// Connect a stepper motor with 200 steps per revolution (1.8 degree)
// to motor port #2 (M3 and M4)
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);
void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Stepper test!");
pinMode (buttonPin1, INPUT_PULLUP);
pinMode (buttonPin2, INPUT_PULLUP);
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);
AFMS.begin(); // create with the default frequency 1.6KHz
//AFMS.begin(1000); // OR with a different frequency, say 1KHz
myMotor->setSpeed(10); // 10 rpm
}
void loop() {
if (digitalRead(buttonPin1) == LOW) //if start button is pressed
{
runState = true; //start running stepper
}
if (digitalRead(buttonPin2) == LOW) //if stop button is pressed
{
runState = false; //stop running stepper
}
if (runState) //if the stepper should be running
{
myMotor->step(1, FORWARD, SINGLE); //advance it one step.
}
}
You need to make the motor variable name match the name you are already using in your code.
If the motor name in the current version of your code is 'myMotor', then change 'motor' to 'myMotor'.
No effect when the motor speed potentiometer is rotated
I want to change my plan before, I use the above code to move a stepper motor manually using a 2 button (start and stop) where the rotation of the motor can be determined during the stop button has not been pressed
now, I want to work the motors automatically by adding 1 button where the motor just spins round once then stop
/*
This is a test sketch for the Adafruit assembled Motor Shield for Arduino v2
It won't work with v1.x motor shields! Only for the v2's with built in PWM
control
For use with the Adafruit Motor Shield v2
----> http://www.adafruit.com/products/1438
*/
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
int buttonPin1 = 2 ;
int buttonPin2 = 3 ;
bool runState = false;
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61);
// Connect a stepper motor with 200 steps per revolution (1.8 degree)
// to motor port #2 (M3 and M4)
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);
void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Stepper test!");
pinMode (buttonPin1, INPUT_PULLUP);
pinMode (buttonPin2, INPUT_PULLUP);
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);
AFMS.begin(); // create with the default frequency 1.6KHz
//AFMS.begin(1000); // OR with a different frequency, say 1KHz
myMotor->setSpeed(10); // 10 rpm
}
void loop() {
if (digitalRead(buttonPin1) == LOW) //if start button is pressed
{
runState = true; //start running stepper
}
if (digitalRead(buttonPin2) == LOW) //if stop button is pressed
{
runState = false; //stop running stepper
}
if (runState) //if the stepper should be running
{
myMotor->step(1, FORWARD, SINGLE); //advance it one step.
}
}
/*
This is a test sketch for the Adafruit assembled Motor Shield for Arduino v2
It won't work with v1.x motor shields! Only for the v2's with built in PWM
control
For use with the Adafruit Motor Shield v2
----> http://www.adafruit.com/products/1438
*/
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
int buttonPin1 = 2 ;
int buttonPin2 = 3 ;
bool runState = false;
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61);
// Connect a stepper motor with 200 steps per revolution (1.8 degree)
// to motor port #2 (M3 and M4)
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);
void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Stepper test!");
pinMode (buttonPin1, INPUT_PULLUP);
pinMode (buttonPin2, INPUT_PULLUP);
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);
AFMS.begin(); // create with the default frequency 1.6KHz
//AFMS.begin(1000); // OR with a different frequency, say 1KHz
}
void loop() {
int speed = map(analogRead(0), 0, 1024, 0, 255); // Map the potentiometer input intoa motor speed
myMotor->setSpeed(speed);
if (digitalRead(buttonPin1) == LOW) //if start button is pressed
{
runState = true; //start running stepper
}
if (digitalRead(buttonPin2) == LOW) //if stop button is pressed
{
runState = false; //stop running stepper
}
if (runState) //if the stepper should be running
{
myMotor->step(1, FORWARD, SINGLE); //advance it one step.
}
}