Dear Arduino Community,
I am new to Arduino, and I am stuck on how to program an Arduino with an Adafruit Motor Shield V2.
I want to add 2 buttons as my input, and make one button move 2 stepper motors one direction, and make the other button move the same 2 stepper motors in the other direction.
I have also noticed that in my code, the stepper motor code blocks, so they move separately and not in sync.
I am also having trouble configuring the 2 buttons to the Arduino board.
Thank you!!
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Connect two steppers with 200 steps per revolution (1.8 degree)
// to the top shield
Adafruit_StepperMotor *myStepper1 = AFMS.getStepper(200, 1);
Adafruit_StepperMotor *myStepper2 = AFMS.getStepper(200, 2);
const int button0pin = 8; //button variables
const int button1pin = 9;
int val; //reads pin status
int val2;
void setup() {
myStepper1->setSpeed(700); //speed
myStepper2->setSpeed(700); //speed
pinMode(button0pin, INPUT); //input values
pinMode(button1pin, INPUT);
}
void loop() {
val = digitalRead(button0pin);
val2 = digitalRead(button1pin);
if (val == HIGH){ //if button 1 is pressed
myStepper1->step(700, FORWARD, DOUBLE); //move stepper motor forward
myStepper2->step(700, FORWARD, DOUBLE); //move second stepper motor forward
}
if (val2 == HIGH){ // if button 2 is pressed
myStepper1->step(700, BACKWARD, DOUBLE); //move stepper motor backwards
myStepper2->step(700, BACKWARD, DOUBLE); //move stepper motor backwards
}
}