Using Stepper motors to lift a model bridge
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- roadshark
- Posts: 652
- Joined: Sun Nov 29, 2015 5:53 am
Re: Using Stepper motors to lift a model bridge
Have just posted in previous post some motor specs.
- roadshark
- Posts: 652
- Joined: Sun Nov 29, 2015 5:53 am
Re: Using Stepper motors to lift a model bridge
Serial.println(stepperLH.currentPosition()); Yep at the end of the bridge lift stepper LH is at 800!
- adafruit_support_bill
- Posts: 88154
- Joined: Sat Feb 07, 2009 10:11 am
Re: Using Stepper motors to lift a model bridge
Please post your full code.
- roadshark
- Posts: 652
- Joined: Sun Nov 29, 2015 5:53 am
Re: Using Stepper motors to lift a model bridge
So I decided to do this, int buttonState = digitalRead(bridgeDeckDown); Read to see if the button is registering.
I placed the read in the code here
When I run the code I see the steps appear on the serial monitor and stop at 800. But the button state does not appear in the monitor. It is like the program gets stuck in the bridge lift code and will not exit??? or I probably have not placed the read button state in the correct place. Here is the full code.
I placed the read in the code here
Code: Select all
Serial.println(buttonState);
//Moves the bridge deck down
if ((digitalRead(bridgeDeckDown))&& (stepperLH.currentPosition() == 800))
{
Code: Select all
#include "AccelStepper.h"
// Library created by Mike McCauley at http://www.airspayce.com/mikem/arduino/AccelStepper/
// AccelStepper Setup
AccelStepper stepperLH(AccelStepper::DRIVER, 23, 25);
AccelStepper stepperRH(AccelStepper::DRIVER, 31, 33);
// Define the Pins used
#define home_switchLH 9 // Pin 9 connected to Home Switch (MicroSwitch)
#define home_switchRH 10// Pin 10 connected to Home switch
#define bridgeDeckUp 34// Raise the bridge deck
#define bridgeDeckDown 36// lowers deck
int outPutPins[] = {22, 24, 25, 26, 28, 30, 32, 37};// out put pins (22, 24, 26,28 used to set step rate on the MP6500. Pins 25 and 37 used to set current limit)pin 30/32 is sleep pin
int pinCount = 7;
int buttonState = digitalRead(bridgeDeckDown);
// Stepper Travel Variables
long initial_homing = -1; // Used to Home Stepper at startup
void setup() {
Serial.begin(9600);
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(outPutPins[thisPin], OUTPUT);
}
pinMode (bridgeDeckUp, INPUT_PULLUP);
pinMode (bridgeDeckDown, INPUT_PULLUP);
pinMode(home_switchLH, INPUT_PULLUP);
pinMode(home_switchRH, INPUT_PULLUP);
digitalWrite (30, HIGH);// Enables the sleep pin on the MP6500/stepperLH
digitalWrite (32, HIGH);// Enables the sleep pin on the MP6500/stepperRH
digitalWrite (25, LOW);// StepperLH
digitalWrite (37, LOW);// StepperRH
// Pins to drive steps
//Change to HIGH OR LOW designates step rate pin 22 and 24 are for LHstepper 26 and 28 for RHstepper To speed up homing set to full step.
digitalWrite (22, LOW);
digitalWrite (24, LOW);
digitalWrite (26, LOW);
digitalWrite (28, LOW);
// Set Max Speed of each Steppers at startup for homing
stepperLH.setMaxSpeed(50.0);
stepperRH.setMaxSpeed(50.0);// Set Max Speed of Stepper (Slower to get better accuracy)
// Start Homing procedure of Stepper Motor at startup
while (digitalRead(home_switchLH) || digitalRead(home_switchRH))
{ // Move motors towards home until both switches are activated
if (digitalRead(home_switchLH))
{
stepperLH.moveTo(initial_homing);// homing switch on this motor should stop both motors
stepperLH.run();
}
if (digitalRead(home_switchRH))
{
stepperRH.moveTo(initial_homing);// Set the position to move to
stepperRH.run();
}
initial_homing--; // Decrease by 1 for next move if needed
}
stepperLH.setCurrentPosition(0); // Set the current position as zero for now
stepperRH.setCurrentPosition(0);
stepperLH.setMaxSpeed(25.0); // Set Max Speed of Stepper (Slower to get better accuracy)
stepperRH.setMaxSpeed(25.0);
digitalWrite (22, LOW);//Sets the step rate high for accuracy sgtep set at one eighth.
digitalWrite (24, LOW);
digitalWrite (26, LOW);
digitalWrite (28, LOW);
initial_homing = 0;
// Move motors AWAY from home until both switches are deactivated and set position to 0.
while (!digitalRead(home_switchLH) || !digitalRead(home_switchRH))
{
if (!digitalRead(home_switchLH))
{
stepperLH.moveTo(initial_homing);// homing switch on this motor should stop both motors
stepperLH.run();
}
if (!digitalRead(home_switchRH))
{
stepperRH.moveTo(initial_homing);// Set the position to move to
stepperRH.run();
}
initial_homing++; // Decrease by 1 for next move if needed
}
stepperLH.setCurrentPosition(0);
stepperRH.setCurrentPosition(0);
//stepperLH.setMaxSpeed(200.0); // Set Max Speed of Stepper (Faster for regular movements)
//stepperRH.setMaxSpeed(200.0);
digitalWrite (30, LOW);// Disables the sleep pin on the MP6500/stepperLH
digitalWrite (32, LOW);// Disables the sleep pin on the MP6500/stepperRH
}
void loop() {//The bridge deck lift
if ((!digitalRead(bridgeDeckUp)) && (stepperLH.distanceToGo() == 0))
{
digitalWrite (30, HIGH);// Enables the sleep pin on the MP6500/stepperLH
digitalWrite (32, HIGH);// Enables the sleep pin on the MP6500/stepperRH
delay(1000);
stepperLH.setMaxSpeed(200.0);
stepperLH.setAcceleration(25.0);
stepperRH.setMaxSpeed(200.0);
stepperRH.setAcceleration(25.0);
stepperLH.moveTo(800);//Deck moves to top and stops.
stepperRH.moveTo(800);
while ((stepperLH.distanceToGo() != 0) || (stepperRH.distanceToGo() == 0))
{
stepperRH.run();
stepperLH.run();
Serial.println(stepperLH.currentPosition());
}
//Bridge deck at top so turn off motors.
digitalWrite (30, LOW);// Enables the sleep pin on the MP6500/stepperLH
digitalWrite (32, LOW);// Enables the sleep pin on the MP6500/stepperRH
}
Serial.println(buttonState);
//Moves the bridge deck down
if ((digitalRead(bridgeDeckDown))&& (stepperLH.currentPosition() == 800))
{
digitalWrite (30, HIGH);// Enables the sleep pin on the MP6500/stepperLH
digitalWrite (32, HIGH);// Enables the sleep pin on the MP6500/stepperRH
stepperLH.setMaxSpeed(50.0);
stepperLH.setAcceleration(25.0);
stepperRH.setMaxSpeed(50.0);
stepperRH.setAcceleration(25.0);
stepperLH.moveTo(0);//Deck moves to bottom and stops.
stepperRH.moveTo(0);
while ((stepperLH.distanceToGo() != 0) || (stepperRH.distanceToGo() == 0))
{
stepperRH.run();
stepperLH.run();
}
//Bridge deck at bottom so turn off motors.
digitalWrite (30, LOW);// Enables the sleep pin on the MP6500/stepperLH
digitalWrite (32, LOW);// Enables the sleep pin on the MP6500/stepperRH
}
}
- adafruit_support_bill
- Posts: 88154
- Joined: Sat Feb 07, 2009 10:11 am
Re: Using Stepper motors to lift a model bridge
You are reading button-state at global-scope. You need to move that inside your loop:
And this will get you stuck in the lowering loop:
You need a '!=' on the RH side of that too.
Code: Select all
int buttonState = digitalRead(bridgeDeckDown);
Code: Select all
while ((stepperLH.distanceToGo() != 0) || (stepperRH.distanceToGo() == 0))
- roadshark
- Posts: 652
- Joined: Sun Nov 29, 2015 5:53 am
Re: Using Stepper motors to lift a model bridge
Code: Select all
if ((digitalRead(bridgeDeckDown)) && (stepperLH.currentPosition() == 800))
{
Serial.println(buttonState);
digitalWrite (30, HIGH);// Enables the sleep pin on the MP6500/stepperLH
digitalWrite (32, HIGH);// Enables the sleep pin on the MP6500/stepperRH
stepperLH.setMaxSpeed(50.0);
stepperLH.setAcceleration(25.0);
stepperRH.setMaxSpeed(50.0);
stepperRH.setAcceleration(25.0);
stepperLH.moveTo(0);//Deck moves to bottom and stops.
stepperRH.moveTo(0);
while ((stepperLH.distanceToGo() != 0) || (stepperRH.distanceToGo() != 0))
{
stepperRH.run();
stepperLH.run();
}
//Bridge deck at bottom so turn off motors.
digitalWrite (30, LOW);// Enables the sleep pin on the MP6500/stepperLH
digitalWrite (32, LOW);// Enables the sleep pin on the MP6500/stepperRH
}
I put the button read inside the bridgeDeckDown Loop. After the bridgeDeckUp Loop completes I press the button but nothing appears in the serial monitor. To my thinking there are 3 possibilities. 1. The button is not functioning (Tested this out by reversing the buttons so that button was now deckBridgeUp. It worked. The pin 36 in the mega was faulty so I changed to pin 38, still no go. 2. The position of the stepper is not really at 800. The serial monitor shows it is. 3. The code is not getting to that point and is hanging in the bridgeDeckUp loop?
- adafruit_support_bill
- Posts: 88154
- Joined: Sat Feb 07, 2009 10:11 am
Re: Using Stepper motors to lift a model bridge
Did you fix this too? See the second part of my previous post.
Code: Select all
while ((stepperLH.distanceToGo() != 0) || (stepperRH.distanceToGo() != 0))
- roadshark
- Posts: 652
- Joined: Sun Nov 29, 2015 5:53 am
Re: Using Stepper motors to lift a model bridge
yep.
while ((stepperLH.distanceToGo() != 0) || (stepperRH.distanceToGo() != 0))
{
stepperRH.run();
stepperLH.run();
}
//Bridge deck at bottom so turn off motors.
while ((stepperLH.distanceToGo() != 0) || (stepperRH.distanceToGo() != 0))
{
stepperRH.run();
stepperLH.run();
}
//Bridge deck at bottom so turn off motors.
- adafruit_support_bill
- Posts: 88154
- Joined: Sat Feb 07, 2009 10:11 am
Re: Using Stepper motors to lift a model bridge
The loop part of the deck-up code is just this:The code is not getting to that point and is hanging in the bridgeDeckUp loop?
Code: Select all
while ((stepperLH.distanceToGo() != 0) || (stepperRH.distanceToGo() == 0))
{
stepperRH.run();
stepperLH.run();
Serial.println(stepperLH.currentPosition());
}
Is it still printing out the current position? Post a sample of the output.
- roadshark
- Posts: 652
- Joined: Sun Nov 29, 2015 5:53 am
Re: Using Stepper motors to lift a model bridge
I even tried this by just reading if the button was pressed without checking the stepper
//Moves the bridge deck down
if (!digitalRead(bridgeDeckDown))
{
Serial.println(buttonState);
digitalWrite (30, HIGH);// Enables the sleep pin on the MP6500/stepperLH
digitalWrite (32, HIGH);//
still no go.
here is the serial monitor. Ends on 800 and nothing else
//Moves the bridge deck down
if (!digitalRead(bridgeDeckDown))
{
Serial.println(buttonState);
digitalWrite (30, HIGH);// Enables the sleep pin on the MP6500/stepperLH
digitalWrite (32, HIGH);//
still no go.
here is the serial monitor. Ends on 800 and nothing else
- Attachments
-
- monitor.jpg (167.53 KiB) Viewed 123 times
- adafruit_support_bill
- Posts: 88154
- Joined: Sat Feb 07, 2009 10:11 am
Re: Using Stepper motors to lift a model bridge
We need some more diagnostic output:
Print both LH and RH positions in your while loop, and another line when you exit the loop:
Print both LH and RH positions in your while loop, and another line when you exit the loop:
Code: Select all
while ((stepperLH.distanceToGo() != 0) || (stepperRH.distanceToGo() != 0))
{
stepperRH.run();
stepperLH.run();
Serial.print(stepperLH.currentPosition());
Serial.print(" - ");
Serial.println(stepperRH.currentPosition());
}
Serial.println("Bridge is down!");
- roadshark
- Posts: 652
- Joined: Sun Nov 29, 2015 5:53 am
Re: Using Stepper motors to lift a model bridge
YEP IT SHOWS THE BUTTON STATE
- Attachments
-
- monitor2.jpg (189.38 KiB) Viewed 121 times
- adafruit_support_bill
- Posts: 88154
- Joined: Sat Feb 07, 2009 10:11 am
Re: Using Stepper motors to lift a model bridge
OK. Now add the same diagnostic output to your 'down' loop so we can see what it is trying to do.
- roadshark
- Posts: 652
- Joined: Sun Nov 29, 2015 5:53 am
Re: Using Stepper motors to lift a model bridge
Hi Bill, Not sure what telepathic communication you are doing with my pc but the code u sent I placed in my code and everything works. OMG it was just looking at the serial monitor?? I am perplexed to say the least. Just like the kid that fools around in class, when you take a close look at them they behave?? Thanks for your mammoth efforts in assisting me!
Here are the specs of the new motors though the threaded rod is a bit large in diameter.
tepper Motor Model: 17HS1538-P4170
Motor Height 40 mm
Weight 320 g
Type 2 Phases, 4-Wire
Torque (Rated, Manufacturer) 0.45 Nm
Step Angle 1.8º
Operating Voltage 12 V (Recommended)
Current (Rated) 1.68 A/Phase
Connector Type XH2.54 (4-pin)
Do I have to alter the current limiting?
I do believe it would be wise to put a fail safe switch to cut the power (or use the reset pin) in the case the mega has a problem....at the top of the and the bottom. It would be best to have this not attached in any way to the processor I guess.
In the actual protype of the bridge the ships apparently signal to the bridge operator with their ships horns. Then the train signals further up the track would turn red (quite a way especially with your American freight trains). A siren sounds and the bridge lifts. I plan to use the music maker shield to play the sounds of the electrical motors and the clanking of the bridge as it lifts including the siren sounds. I am not sure if they have boom gates across the lines in front of the bridge or not. Was the bridge you showed me a train or traffic bridge? Might have to include servos to operate the boom gates. I hope the mega can handle all this.
Here are the specs of the new motors though the threaded rod is a bit large in diameter.
tepper Motor Model: 17HS1538-P4170
Motor Height 40 mm
Weight 320 g
Type 2 Phases, 4-Wire
Torque (Rated, Manufacturer) 0.45 Nm
Step Angle 1.8º
Operating Voltage 12 V (Recommended)
Current (Rated) 1.68 A/Phase
Connector Type XH2.54 (4-pin)
Do I have to alter the current limiting?
I do believe it would be wise to put a fail safe switch to cut the power (or use the reset pin) in the case the mega has a problem....at the top of the and the bottom. It would be best to have this not attached in any way to the processor I guess.
In the actual protype of the bridge the ships apparently signal to the bridge operator with their ships horns. Then the train signals further up the track would turn red (quite a way especially with your American freight trains). A siren sounds and the bridge lifts. I plan to use the music maker shield to play the sounds of the electrical motors and the clanking of the bridge as it lifts including the siren sounds. I am not sure if they have boom gates across the lines in front of the bridge or not. Was the bridge you showed me a train or traffic bridge? Might have to include servos to operate the boom gates. I hope the mega can handle all this.
- adafruit_support_bill
- Posts: 88154
- Joined: Sat Feb 07, 2009 10:11 am
Re: Using Stepper motors to lift a model bridge
Good to hear that is working for you now.
Those motors are rated for 1.68A/phase. So your 1.5A current limit should be fine. And putting them to sleep between moves should keep them running cool.
You won't find boom-gates on railroad bridges like the Joliet or the Cape Cod Canal bridge I linked earlier. Unlike automobile drivers, train engineers are less likely to disregard the signals.
Those motors are rated for 1.68A/phase. So your 1.5A current limit should be fine. And putting them to sleep between moves should keep them running cool.
You won't find boom-gates on railroad bridges like the Joliet or the Cape Cod Canal bridge I linked earlier. Unlike automobile drivers, train engineers are less likely to disregard the signals.
Please be positive and constructive with your questions and comments.