I have two adafruit V2 Motor Shields stacked onto my Arduino Mega
I have two nema 17 steppers per shield
I previously ran this code with only a single motor shield and two motors and it worked great. I was able to have the code ask me for the number of steps I wanted. I would enter my response into the serial monitor and then it would drive the respective motor the number of steps I asked for. It also displays a copy of the information on a 16x2 LCD
Now the issue is twofold:
1. when I enter the number of steps for motor 1, it then drives motors 1 &3 simultaneously.
2.when I enter the number of steps for motor 2, it then drives motors 2 &4 simultaneously.
3. then it asks for the number of steps for motor 3 (which is correct) and when I enter the steps, it responds with a backward "?" symbol, and then resets the program.
I have copied the text from the serial monitor below (My entries are in bold)
~~~~~~~~~~~~~~~~~
Stepper test!
Motor Shield found.
Motor 1 Steps?
200.00
Motor 2 Steps?
200.00
Motor 3 Steps?
200.⸮Stepper test!
Motor Shield found.
Motor 1 Steps?
~~~~~~~~~~~~~~~~~~
here is my code:
Code: Select all
/*
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 <Adafruit_MotorShield.h>
#include <LiquidCrystal.h>
//This is the part I may have screwed up...
// 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 AFMS2 = Adafruit_MotorShield(0x61);
// Connect a stepper motor with 200 steps per revolution (1.8 degree)
// to motor port #2 (M3 and M4)
Adafruit_StepperMotor *myMotor1 = AFMS.getStepper(200, 1);
Adafruit_StepperMotor *myMotor2 = AFMS.getStepper(200, 2);
Adafruit_StepperMotor *myMotor3 = AFMS2.getStepper(200, 1);
Adafruit_StepperMotor *myMotor4 = AFMS2.getStepper(200, 2);
int dt=500;
float M1Steps;
float M2Steps;
float M3Steps;
float M4Steps;
int rs=7; //LCD Pins are 7 through 12
int en=8;
int d4=9;
int d5=10;
int d6=11;
int d7=12;
String AskM1="Motor 1 Steps?";
String AskM2="Motor 2 Steps?";
String AskM3="Motor 3 Steps?";
String AskM4="Motor 4 Steps?";
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);
void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
lcd.clear();
lcd.begin(16,2);
while (!Serial);
Serial.println("Stepper test!");
if (!AFMS.begin()) { // create with the default frequency 1.6KHz
// if (!AFMS.begin(1000)) { // OR with a different frequency, say 1KHz
Serial.println("Could not find Motor Shield. Check wiring.");
while (1);
}
Serial.println("Motor Shield found.");
myMotor1->setSpeed(10); // 10 rpm
myMotor2->setSpeed(10);
myMotor3->setSpeed(10);
myMotor4->setSpeed(10);
}
void loop() {
//Control of Motor ONE code below
Serial.println(AskM1);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(AskM1);
while (Serial.available ()==0){
}
M1Steps=Serial.parseFloat();
lcd.setCursor(0,1);
lcd.print(M1Steps);
Serial.println(M1Steps);
myMotor1->step(M1Steps, FORWARD, SINGLE);
//Control of Motor TWO code below
lcd.clear();
lcd.begin(16,2);
Serial.println(AskM2);
lcd.setCursor(0,0);
lcd.print(AskM2);
while (Serial.available ()==0){
}
M2Steps=Serial.parseFloat();
lcd.setCursor(0,1);
lcd.print(M2Steps);
Serial.println(M2Steps);
myMotor2->step(M2Steps, FORWARD, SINGLE);
//Control of Motor THREE code below
lcd.clear();
lcd.begin(16,2);
Serial.println(AskM3);
lcd.setCursor(0,0);
lcd.print(AskM3);
while (Serial.available ()==0){
}
M3Steps=Serial.parseFloat();
lcd.setCursor(0,1);
lcd.print(M3Steps);
Serial.println(M3Steps);
myMotor3->step(M3Steps, FORWARD, SINGLE);
//Control of Motor FOUR code below
lcd.clear();
lcd.begin(16,2);
Serial.println(AskM4);
lcd.setCursor(0,0);
lcd.print(AskM4);
while (Serial.available ()==0){
}
M4Steps=Serial.parseFloat();
lcd.setCursor(0,1);
lcd.print(M4Steps);
Serial.println(M4Steps);
myMotor4->step(M4Steps, FORWARD, SINGLE);
}
I'm pretty new at this stuff, so be gentle!
Austin