Motor Shield V2 | Program is not automatically executed

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
effra030
 
Posts: 16
Joined: Tue Nov 19, 2013 8:52 am

Motor Shield V2 | Program is not automatically executed

Post by effra030 »

Whenever I power up the Arduino/motor shield v.2 with my two stepper motors my program just won't run the motors (the press button-light up LED function still works). I can only solve this issue by hooking up via USB to my computer and overwrite my code with "Stepper Test" once, to run motors or just one motor for a bit. This done, I can overwrite again with my intended code and everything works just fine. To get things running, I have to do this every time after I switched power off on my Arduino/motor shield and motors.

Just hooking up the Arduino to the computer doesn't do the trick, so maybe no RX/TX problem?!
I have to overwrite my code but overwriting with let's say "Examples > Blink" doesn't help, it has to be a code that runs at least one motor. There is no difference whether my power supply for the motors comes from a wall wart or from a NiMH battery pack, in either case the set-up won't run from the start but will do so after I overwritten the code and tested my motors once.

// Thank you for helping !

Set-up:
[*] Arduino Duemilanove
[*] Motor Shield v.2
[*] 2 small steppers (https://www.adafruit.com/products/858)
[*] 2 separate power supplies for Arduino and motors (jumper on motor shield removed)


This is my code:

Code: Select all

// 17.07.2014

#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 == HIGH) {     
    // 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());

}

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Motor Shield V2 | Program is not automatically executed

Post by adafruit_support_rick »

What happens if you leave stepperTest in there, and disconnect the power? Does it still work when you re-power the system?

effra030
 
Posts: 16
Joined: Tue Nov 19, 2013 8:52 am

Re: Motor Shield V2 | Program is not automatically executed

Post by effra030 »

I just checked:
StepperTest runs fine after unplugging.
The problem only occurs with my code. Will remove LED for now to see if that changes anything.

effra030
 
Posts: 16
Joined: Tue Nov 19, 2013 8:52 am

Re: Motor Shield V2 | Program is not automatically executed

Post by effra030 »

Doublechecked hardware and tried different codes, also from AccelStepper library – they all work fine. The problem must lie within my code :(

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Motor Shield V2 | Program is not automatically executed

Post by adafruit_support_rick »

You're doing the button a little differently thn most people. Do you have a pull-down resistor on buttonPin? Otherwise, the state of the pin will float when you're not pressing it.

Most people tie the button between the pin and gnd, then enable the internal pullup on the pin. So the button is active LOW, and pulled HIGH when you're not pressing it

Code: Select all

  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT_PULLUP);

effra030
 
Posts: 16
Joined: Tue Nov 19, 2013 8:52 am

Re: Motor Shield V2 | Program is not automatically executed

Post by effra030 »

I am using a pulldown resistor. But will try your suggestion and add that bit of code. Thanks a lot so far!

effra030
 
Posts: 16
Joined: Tue Nov 19, 2013 8:52 am

Re: Motor Shield V2 | Program is not automatically executed

Post by effra030 »

It's working now!

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Motor Shield V2 | Program is not automatically executed

Post by adafruit_support_rick »

Nice!

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”