0

Run stepper on button press stop working
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Run stepper on button press stop working

by Techguy817 on Wed Oct 21, 2020 10:06 pm

Hi all,

Working on a relatively simple project with my Leonardo and the motor shield. When pressing the momentary button I'd like to run the stepper a short turn. This actually appears to work for a small random amount of times (maybe 2-5) but then it stops until I do a reset.

Code: Select all | TOGGLE FULL SIZE
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include <Adafruit_PWMServoDriver.h>

Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);

int buttonState = 0;        // initialize button to 0

void setup() {
  pinMode(2, INPUT);        // input pin for button
 
  AFMS.begin();             // intialize motor shield
  myMotor->setSpeed(100);   // set RPM
  myMotor->step (100, FORWARD, SINGLE);   // do initial step on start-up
 
}

void loop() {
    buttonState = digitalRead(2);
   
    if(buttonState == LOW) //button pressed
    {
      digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
    myMotor->step (30, FORWARD, DOUBLE);

    }
}


Any ideas?

Techguy817
 
Posts: 17
Joined: Fri Oct 16, 2020 11:44 pm

Re: Run stepper on button press stop working

by franklin97355 on Wed Oct 21, 2020 11:54 pm

What is the resistance per phase on the stepper and how long do you wait per button push?

franklin97355
 
Posts: 21965
Joined: Mon Apr 21, 2008 2:33 pm
Location: Lacomb, OR.

Re: Run stepper on button press stop working

by Techguy817 on Thu Oct 22, 2020 8:00 am

I'm using the motor at https://www.adafruit.com/product/324. I wait a few seconds between button presses.

Techguy817
 
Posts: 17
Joined: Fri Oct 16, 2020 11:44 pm

Re: Run stepper on button press stop working

by adafruit_support_bill on Thu Oct 22, 2020 8:55 am

Please post some photos showing your soldering and connections.

Unless you have an external pullup resistor on your button, you need to enable the input one in your pinMode statement: https://www.arduino.cc/reference/en/lan ... o/pinmode/
Code: Select all | TOGGLE FULL SIZE
 pinMode(2, INPUT_PULLUP);        // input pin for button

adafruit_support_bill
 
Posts: 81589
Joined: Sat Feb 07, 2009 10:11 am

Re: Run stepper on button press stop working

by Techguy817 on Thu Oct 22, 2020 9:19 am

Here are some photos. I tried using the Fritzing tool to create a schematic diagram but I didn't find the Adafruit motorshield in there.


PXL_20201022_130110211.jpg
PXL_20201022_130110211.jpg (779.17 KiB) Viewed 112 times

PXL_20201022_130100802.jpg
PXL_20201022_130100802.jpg (793.27 KiB) Viewed 112 times

PXL_20201022_130045377.jpg
PXL_20201022_130045377.jpg (788.29 KiB) Viewed 112 times

Techguy817
 
Posts: 17
Joined: Fri Oct 16, 2020 11:44 pm

Re: Run stepper on button press stop working

by adafruit_support_bill on Thu Oct 22, 2020 9:32 am

What are you using to power the motor? Does the green power led on the shield remain on even when the motor does not run?

adafruit_support_bill
 
Posts: 81589
Joined: Sat Feb 07, 2009 10:11 am

Re: Run stepper on button press stop working

by Techguy817 on Thu Oct 22, 2020 9:33 am

The green LED on the motor shield remains on. I'm using a 12V power source wired up.

Techguy817
 
Posts: 17
Joined: Fri Oct 16, 2020 11:44 pm

Re: Run stepper on button press stop working

by adafruit_support_bill on Thu Oct 22, 2020 9:48 am

I'm using a 12V power source

Volts is not power. You need amps too. https://www.engineeringclicks.com/watts ... -ohms-law/
What 12v supply are you using to power your motor?

I'd add a Serial.println() statement in your loop to print the buttonState and verify that it is changing as expected when pressing the button.

adafruit_support_bill
 
Posts: 81589
Joined: Sat Feb 07, 2009 10:11 am

Re: Run stepper on button press stop working

by Techguy817 on Thu Oct 22, 2020 10:04 am

Supply is rated at 5 amp output.

Techguy817
 
Posts: 17
Joined: Fri Oct 16, 2020 11:44 pm

Re: Run stepper on button press stop working

by Techguy817 on Thu Oct 22, 2020 2:56 pm

In looking into this further, it seems to be some kind of blocking issue with the stepper code/libraries. Using the momentary button to trigger an LED works fine continuously.

Any suggestions for tweaking my code?

Techguy817
 
Posts: 17
Joined: Fri Oct 16, 2020 11:44 pm

Re: Run stepper on button press stop working

by adafruit_support_bill on Thu Oct 22, 2020 3:47 pm

Did you add the serial debugging output as suggested? Does the output change every time you press the button?

adafruit_support_bill
 
Posts: 81589
Joined: Sat Feb 07, 2009 10:11 am

Re: Run stepper on button press stop working

by Techguy817 on Thu Oct 22, 2020 4:03 pm

I have not. Can you please point me to the details on serial debugging and I'll get that enabled?

Techguy817
 
Posts: 17
Joined: Fri Oct 16, 2020 11:44 pm

Re: Run stepper on button press stop working

by adafruit_support_bill on Thu Oct 22, 2020 4:07 pm


adafruit_support_bill
 
Posts: 81589
Joined: Sat Feb 07, 2009 10:11 am

Re: Run stepper on button press stop working

by Techguy817 on Thu Oct 22, 2020 4:41 pm

Thanks. I configured the serial debugging and it outputs 1 until the button is pressed. Then it outputs 0 a couple of times depending how long its pressed. When the board locks up it just freezes and no more serial output. E.g.

1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
0

Techguy817
 
Posts: 17
Joined: Fri Oct 16, 2020 11:44 pm

Re: Run stepper on button press stop working

by adafruit_support_bill on Thu Oct 22, 2020 6:07 pm

Sounds like probably a marginal connection on the i2c bus. Please post some photos showing your soldering to the shield.

adafruit_support_bill
 
Posts: 81589
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.