Notifications
Clear all

Need help with stepper motor project

31 Posts
5 Users
7 Likes
2,530 Views
(@pappasteve)
Member
Joined: 3 years ago
Posts: 27
Topic starter  

Here is more specific info on my project plans. 

 

My stepper motor homes itself on initial startup.  I would like to use that as my zero point.  I need 2 more positions,  let's say 25 steps away (position  A) and 50 steps away (position B) from the zero position. (My stepper motor has 200 steps per revolution.).

Currently my if, else if, and else conditional statements work as I want them.  I want to add to those statements to make the stepper motor go to position A or B, which ever is true.  I want the stepper motor to stay at that position until one of the other positions become true. And it moves there.   

 

I have gotten the stepper motor to move, but it moves stops, moves stops, constantly. Won't just stay in that position. 


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2504
 

@pappasteve

 

Please show us the code you use to move the stepper.

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
(@pappasteve)
Member
Joined: 3 years ago
Posts: 27
Topic starter  

I can later,  after work.   

 

Oh. Yea, I'm not giving up, I'm just getting started. 


   
ReplyQuote
(@lydara)
Member
Joined: 3 years ago
Posts: 90
 

@pappasteve  Sure it's a stepper and not a servo that requires a PWM to keep it in place?


   
ReplyQuote
(@pappasteve)
Member
Joined: 3 years ago
Posts: 27
Topic starter  

@lydara

 

No, it is a nema 17 stepper motor.

This post was modified 3 years ago by Pappasteve

   
ReplyQuote
(@pappasteve)
Member
Joined: 3 years ago
Posts: 27
Topic starter  

This is it. 

 

Screenshot 20210629 223106 Chrome

 

This post was modified 3 years ago by Pappasteve

   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2504
 
Posted by: @pappasteve

Here is more specific info on my project plans. 

I have gotten the stepper motor to move, but it moves stops, moves stops, constantly. Won't just stay in that position. 

Let me take a guess, it moves to the desired position and then, about half a second later it moves again by about the same amount ...

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
(@pappasteve)
Member
Joined: 3 years ago
Posts: 27
Topic starter  

@will

 

yes, exactly.


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2504
 

@pappasteve

 

That's because you have no way of specifying where you are. You need to set your location after you move and then DON'T move again until you if/then/else says you should be in a different place. Then move to that place, set your location and don't move until the if/then/else says you need to be somewhere else.

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
(@pappasteve)
Member
Joined: 3 years ago
Posts: 27
Topic starter  

@will

 

That sound very possible.

 

What kind of statements does it take to do that?  


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2504
 

@pappasteve

 

Assuming that you have made the addition of the variable "position" and the methods that DaveE suggested with the addition of the update to position I suggested above for the single step method, then you can add a new constants (or #define if you prefer) which specifies the desired position (i.e. step count from 0 i.e. home) for each condition you want to move the stepper

then modify your if/then/else as follows:

if (condition A) {

...

moveToPostion(whatever position is needed);

} etc

 

add the new method

void moveToPosition(long newPosition) {

if (position!=newPosition) {

... enter code here to move forward or backward as many steps as needed to make position==newPosition

}

You have to remember that the stepper just moves forward or backward as many steps as you tel it. It's up to YOU to keep track of where it's pointing. That's why you need to maintain a counter like location which needs to be given an initial reference point (by setting it to zero when you home it at the beginning) and updated when you move a step so that it always tells you how many steps it is from the origin (i.e. where it's pointing now).

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
(@pappasteve)
Member
Joined: 3 years ago
Posts: 27
Topic starter  

Thank you, for the response.  

 

I have work to do.


   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2037
 

@pappasteve

Last time I spent time on a stepper motor project.
https://forum.dronebotworkshop.com/help-wanted/questions-about-changing-to-array-from-buttons/paged/7/
Your project is very simple and you seem to be mostly there.
You have been able to home the stepper motor using a hall effect sensor.
As indicated you must save the current position of the stepper motor in a variable to figure out how far and which direction to move to get to the desired position determined by the sensor.

 

 

int current_position;  'hold current position of stepper motor

void homefunction() {
  // Move motor until home position reached
  while (digitalRead(HALL_SENSOR) == 1) {

    digitalWrite(DIR, setdir);
    digitalWrite(STEP, HIGH);
    delayMicroseconds(4000);
    digitalWrite(STEP, LOW);
    delayMicroseconds(4000);
  }
  
  current_position = 0;  // <----   now at zero position

}

 

 


   
ReplyQuote
(@pappasteve)
Member
Joined: 3 years ago
Posts: 27
Topic starter  

@robotbuilder

 

Thank you for the reply.   That gets me that much closer.  I will check out your link.


   
ReplyQuote
(@pappasteve)
Member
Joined: 3 years ago
Posts: 27
Topic starter  

Update to the project.  I have searched for examples of similar code writing I thought would help.  Found a couple to try.  Did make some progress , but not exactly what I wanted.  My grandson had some time today to come by and help me.  He is going to college for electrical engineering.  Took him about a 1 1/2 hours to get it to work. 

 

 

    


   
DaveE reacted
ReplyQuote
Page 2 / 3