RGB LCD Shield + Stepper Motor Driver v2.3 Menu Issue

Hi All,

I am trying to use the following hardware to control the motion of a stepper motor via a menu on the LCD shield:

Arduino UNO R3

Stepper Motor Shield v2.3

RGB LCD Shield

NEMA 17 Stepper Motor (Scroll down for Spec. sheet)

The stepper shield is powered by a 12V 3A power supply and the Arduino is powered by the USB port.

Here is the code I am using:

#include <LiquidCrystal.h>
#include <Wire.h>
#include <Adafruit_RGBLCDShield.h>
#include <Adafruit_MotorShield.h>
#include “utility/Adafruit_MS_PWMServoDriver.h”

// Initialize the libraries for LCD and Motor
Adafruit_RGBLCDShield lcd;
// 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);

//Global for motor speed initialized to 1
int motorSpeed = 1;

void setup() {
Serial.begin(9600);
//Set the characters and column numbers.
lcd.begin(16, 2);
}
//main loop this calls the function that sets speed and direction.
void loop() {
getSpeed();
myMotor->setSpeed(motorSpeed);
lcd.clear();
delay(50);
setDirection();
}

//Gets user input for desired speed
void getSpeed(){
clearPrintTitle();
lcd.print(“Select Speed”);
lcd.setCursor(0,1);
lcd.print(motorSpeed);
int isSet=1;
//loop until speed is set by user pressing select. Max motor speed of 250 min of 1.
while(isSet){
uint8_t buttons = lcd.readButtons();
if (buttons) {
lcd.setCursor(0,1);
if (buttons & BUTTON_UP) {
if (motorSpeed < 250) {
motorSpeed++;
lcd.print(motorSpeed);
delay(100);
}
}
if (buttons & BUTTON_DOWN) {
if (motorSpeed > 1 ) {
motorSpeed–;
lcd.print(motorSpeed);
delay(100);
}
}

if (buttons & BUTTON_SELECT) {
//want to use this to save the value and move onto another function.
clearPrintTitle();
lcd.print(“Speed Set As:”);
lcd.setCursor(0,1);
lcd.print(motorSpeed);
isSet=0;
delay(3000);
}

}
}
}

//Function to select direction
void setDirection() {
//State = 0 every loop cycle.
int motorDirection = 1;
int lastMotorDirection = 1;
//Set the Row 0, Col 0 position.
lcd.setCursor(0,0);
lcd.print(“Set Direction”);
lcd.setCursor(0,1);
displayMenu(motorDirection);
int isSet = 1;
while(isSet){
uint8_t buttons = lcd.readButtons();
if (buttons) {
Serial.print(“buttons loop”);
if (buttons & BUTTON_UP) {
Serial.print(“up”);
if (motorDirection == 3){
lastMotorDirection = motorDirection;
motorDirection = 1;
}
else{
lastMotorDirection = motorDirection;
motorDirection++;
}
displayMenu(motorDirection);
}
if (buttons & BUTTON_DOWN) {
Serial.print(“down”);
if (motorDirection == 1){
lastMotorDirection = motorDirection;
motorDirection = 3;
}
else{
lastMotorDirection = motorDirection;
motorDirection–;
}
displayMenu(motorDirection);
}
if (buttons & BUTTON_SELECT) {
Serial.print(“select”);
//want to use this to save the value and move onto another function.
clearPrintTitle();
lcd.print(“Direction Is:”);
lcd.setCursor(0,1);
lcd.print(motorDirection);
isSet=0;
delay(3000);
}
}
delay(30);
}
runMotor(motorDirection);
}

//Display Menu Option based on Index.
//Helper function for setDirection
void displayMenu(int x) {
switch (x) {
case 1:
lcd.clear();
lcd.print(“Set Direction”);
lcd.setCursor(0,1);
lcd.print (“Forward”);
break;
case 2:
lcd.clear();
lcd.print(“Set Direction”);
lcd.setCursor(0,1);
lcd.print (“Backwards”);
break;
case 3:
lcd.clear();
lcd.print(“Set Direction”);
lcd.setCursor(0,1);
lcd.print (“Back & Forth”);
}
}
//Run the motor forever.
//Display direction and speed
void runMotor(int x) {
switch (x) {
case 1:
lcd.clear();
lcd.setCursor(0,0);
lcd.print (“Forward”);
lcd.setCursor(0,1);
lcd.print(“Speed:”);
lcd.print(motorSpeed);
while(true){
myMotor->step(-1, FORWARD, DOUBLE);
}
case 2:
lcd.clear();
lcd.setCursor(0,0);
lcd.print (“Backward”);
lcd.setCursor(0,1);
lcd.print(“Speed:”);
lcd.print(motorSpeed);
while(true){
myMotor->step(-1, BACKWARD, DOUBLE);
}
case 3:
lcd.clear();
lcd.setCursor(0,0);
lcd.print (“Back & Forth”);
lcd.setCursor(0,1);
lcd.print(“Speed:”);
lcd.print(motorSpeed);
//run forever
while(true){
myMotor->step(48,FORWARD, DOUBLE);
myMotor->step(48,BACKWARD,DOUBLE);
delay(5);
}
}
}

//Resets the lcd
void clearPrintTitle(){
lcd.clear();
lcd.setCursor(0,0);
}

I am having two issues:

  1. When I try to power the Arduino via the barrel jack connector instead of the USB the stepper does not respond.

  2. I only get the stepper to respond by running the StepperTest program first, from the Adafruit_MotorShield Library(see below), and then running the program above.

/*
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_MS_PWMServoDriver.h”

// 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!”);

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() {
Serial.println(“Single coil steps”);
myMotor->step(100, FORWARD, SINGLE);
myMotor->step(100, BACKWARD, SINGLE);

Serial.println(“Double coil steps”);
myMotor->step(100, FORWARD, DOUBLE);
myMotor->step(100, BACKWARD, DOUBLE);

Serial.println(“Interleave coil steps”);
myMotor->step(100, FORWARD, INTERLEAVE);
myMotor->step(100, BACKWARD, INTERLEAVE);

Serial.println(“Microstep steps”);
myMotor->step(50, FORWARD, MICROSTEP);
myMotor->step(50, BACKWARD, MICROSTEP);
}

Some other posts suggest this may be a timing issue and to try using an interrupt, but I don’t know if this is the best route to take. If this is the case, how should I go about implementing an interrupt?

Please let me know what you think.

Anyone?

Problem solved. I did not have AFMS.begin(); declared in void setup()