Pages: [1]   Go Down
Author Topic: Setting a home position for a stepper motor  (Read 3341 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm pulling my hair out with this. I'm working on writing code that will set a home position on start up by running a stepper motor in reverse till it hits a switch. hitting the switch will set its position to zero and then by pushing a button the stepper will run a window blind till it reaches a certain number of steps.
I'm working with an Arduino Mega 2560, easy driver stepper board, a 23KM-K217-P2v mineaba stepper.
I can get the switch to stop the motor, but then nothing happens. I haven't added the main button to activate the blind yet as i'm working on getting the home feature to work.
here is the latest code that only stops the motor when the button is pushed. I'm still learning.
***
//routine to run stepper motor controled blind. will add a button to control blind after homing stepper is working
#include <AccelStepper.h>

AccelStepper stepper1(1, 6, 5);

const int homeButton = A0;
const int ledPin = 13;
int buttonState = 0;

void setup(){
  stepper1.setMaxSpeed(500); //nice and slow for testing
  pinMode(homeButton, INPUT);
  pinMode(ledPin, OUTPUT);
  attachInterrupt(0, homeMotor, CHANGE);
  stepperHome(); //runs routine to home motor
}
void loop(){
  stepper1.moveTo(5000); // random position to end for testing
  stepper1.runToPosition();
  delay(1000);
  stepper1.moveTo(0);
  stepper1.runToPosition();
  delay(1000);
}

void stepperHome(){ //this routine should run the motor
  //backwards slowly till it hits the switch and stops
  stepper1.moveTo(-100);
  stepper1.run();
  digitalWrite(ledPin, LOW); //indicates it's doing something
}

void homeMotor(){
  digitalWrite(ledPin, HIGH); //indicates it's doing something
  stepper1.setCurrentPosition(0); //should set motor position to zero and go back to main routine
}
***
Logged

Offline Offline
Faraday Member
**
Karma: 67
Posts: 2690
The last thing you did is where you should start looking.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I would slowly reverse the motor until the switch is closed then go forward until the switch is open, lets say forward 100, do this in a function.  Call this your zero or home position.
You don't need Interrupts to do this.
I would also enable the pull-up on the switch pin if you don't have an external one.
« Last Edit: August 30, 2013, 12:21:48 am by LarryD » Logged

The way you have it in your schematic isn't the same as how you have it wired up!

Offline Offline
Full Member
***
Karma: 2
Posts: 213
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The interupt-routine acts on a state-change. Thats wrong, because it would also run, when the Motor is leaving the home position and releases the Switch.
Using Input_pullup has already been mentioned
Logged

It's not my fault when my Posts are full of Errors. This stupid autocorrection from my browser is set up for german grammar.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I changed the interrupt to just a button it has an external pullup and goes to ground when pushed. now it does nothing but run the motor at a constant rate.

Quote
#include <AccelStepper.h>

AccelStepper stepper1(1, 6, 5);

const int homeButton = 0;
const int ledPin = 13;


void setup(){
  stepper1.setMaxSpeed(500); //nice and slow for testing
  pinMode(homeButton, INPUT);
  pinMode(ledPin, OUTPUT);
  stepperHome(); //runs routine to home motor
}
void loop(){
  stepper1.moveTo(5000); // random position to end for testing
  stepper1.runToPosition();
  delay(1000);
  stepper1.moveTo(0);
  stepper1.runToPosition();
  delay(1000);
}

void stepperHome(){ //this routine should run the motor
  if (digitalRead(homeButton == HIGH)){
    //backwards slowly till it hits the switch and stops
    stepper1.moveTo(-100);
    stepper1.run();
    digitalWrite(ledPin, LOW); //indicates it's doing something
  }
  else{
    digitalWrite(ledPin, HIGH); //indicates it's doing something
    stepper1.setCurrentPosition(0); //should set motor position to zero and go back to main routine
  }
}

Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok, so i just realized i have to change the HIGH to LOW in the button statement because it's going to ground. so i did that, but it still just runs the motor and doesn't recognize the button push at all.
thanks for any help!
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, now i'm delirious. It''s supposed to be HIGH that way it runs the motor backwards till it hits the switch and goes LOW. I still haven't gotten anywhere with this. it just runs the motor. should the "HomeMotor" sketch be in the setup or main loop?
thanks!
Logged

Offline Offline
Faraday Member
**
Karma: 65
Posts: 2504
Now, More Than Ever
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Generally, "we" do not use pins 0 or 1 for general I/O.  Use pin 2.

should the "HomeMotor" sketch be in the setup or main loop?
It's OK to have the stepperHome() [right?] function call placed in setup ().

Is if (digitalRead(homeButton == HIGH)) good form?
I use a variable.
Code:
const byte homeButton = 2;
byte hBval;   // new variable

//... and now...

void stepperHome()
{ //this routine should run the motor
  hBval = digitalRead(homeButton);  // this could be in setup()
                                   // before calling stepperHome function
  while (hBval == HIGH)
  {
    //backwards slowly till it hits the switch and stops
    stepper1.moveTo(-100);
    stepper1.run();
    digitalWrite(ledPin, LOW);
    hBval = digitalRead(homeButton);
  }
  digitalWrite(ledPin, HIGH); //
  stepper1.setCurrentPosition(0); //
}
Logged

"Hello, I must be going..."
"You gotta fight -- for your right -- to party!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"

0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Awesome! it's working. I've not seen code like that. it's very simple and interesting. I'm going to play with it some. I thank you for your time Runaway Pancake!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hey dude im doing the same i want a home position for the step motor can you pass me the full code of the home step motor

THANKS  a lot you are going to rescue me
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The code is above. that should be enough to work it out. I have no idea what your setup is. I used the following but it might be different for you.

#include <AccelStepper.h>

AccelStepper stepper1(1, 6, 5);

const int homeButton = 2;
const int ledPin = 13;
byte hBval;


void setup(){
  stepper1.setMaxSpeed(500); //nice and slow for testing
  stepper1.moveTo(-3200);
  stepper1.setAcceleration(500);
  pinMode(homeButton, INPUT);
  pinMode(ledPin, OUTPUT);
  stepperHome(); //runs routine to home motor
}
void loop(){
  stepper1.moveTo(5000); // random position to end for testing
  stepper1.runToPosition();
  delay(1000);
  stepper1.moveTo(0);
  stepper1.runToPosition();
  delay(1000);
}
//contributed by Runaway Pancake 9/2/13
void stepperHome(){ //this routine should run the motor
  hBval = digitalRead(homeButton);
  while (hBval == HIGH)
  {
    //backwards slowly till it hits the switch and stops
    stepper1.moveTo(-3200);
    stepper1.run();
    digitalWrite(ledPin, LOW); //indicates it's doing something
    hBval = digitalRead(homeButton);
  }
  digitalWrite(ledPin, HIGH); //indicates it's doing something
  stepper1.setCurrentPosition(0); //should set motor position to zero and go back to main routine
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks Magicd i m using unipolar motors !! can adapt or add a pin here in this line " AccelStepper stepper1(1, 6, 5); " because i need 4 pins for unipolar step motors.... and then how can i do to put all this code only in one proces and then to continue with a void loop ? i mean i onlly want this routine only when the arduino start o restart only one time and then that the oders process continue commonly.

thankkksss a lot !
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

1st of all you need to google the accelstepper library info to find out what the set up is for a unipolar motor. I'm using an easy driver stepper board which uses 3 pins to control a 4 wire stepper. if you aren't using a board then it's different. as for running the home function only on start up, that is the way this code is configured. you can see that in the void setup() it has the "stepperHome" function. when the arduino starts up it runs the void setup() once. whatever is in there runs once and then the void loop() runs for the rest of the time. the parts in the void loop() are just a random test code to run the stepper a little so i know it's working. you would put your main program in the void loop() section. the speed and steps are all dependent on what your motors specs are. some motors stall at too high a speed, and might not do much more than buzz at lower speeds. you need to work that out. There's plenty of tutorials online and on youtube about stepper motors. Rather than just copy paste code it's best to look at the code and try to understand what it does.
oh and the biggest thing to remember is DON'T UNPLUG THE MOTOR FROM YOUR CIRCUIT WHEN YOU HAVE POWERED THE CIRCUIT. if you are using a controller, or running directly from your arudino you will blow up your electronics if you unplug it while it is powered up, even just one wire. so make sure it's all plugged in really well. It will be an expensive mistake.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
//Rhys Mossom
//Unipolar stepper driver - FULL STEP mode.
//http://rhysmossom.com

#define ROTOR1_BLACK 40
#define ROTOR1_GREEN 41
#define ROTOR1_RED 42
#define ROTOR1_BLUE 43

int Rotor1Step = 1;

#define UNWIND 1
#define REWIND 2
#define NOMOTION 0

void setup()
{
pinMode(ROTOR1_BLUE, OUTPUT);
pinMode(ROTOR1_BLACK, OUTPUT);
pinMode(ROTOR1_GREEN, OUTPUT);
pinMode(ROTOR1_RED, OUTPUT);

}

int PreformStep( int Motor1Dir )
{
 if ( Motor1Dir == UNWIND )
      Rotor1Step++;
    else if ( Motor1Dir == REWIND )
      Rotor1Step--;
 
    
    if ( Rotor1Step > 4 )
      Rotor1Step = 1;
    if ( Rotor1Step < 1 )
      Rotor1Step = 4;
    
    
    if ( Motor1Dir != NOMOTION )
    {
       switch ( Rotor1Step )
        {
   case 1:
     digitalWrite(ROTOR1_RED, LOW);  
   digitalWrite(ROTOR1_BLACK, LOW);
   digitalWrite(ROTOR1_GREEN, HIGH);  
  digitalWrite(ROTOR1_BLUE, HIGH);
  delay(2);                    
break;  

case 2:
   digitalWrite(ROTOR1_BLUE, LOW);
   digitalWrite(ROTOR1_BLACK, LOW);
   digitalWrite(ROTOR1_GREEN, HIGH);  
   digitalWrite(ROTOR1_RED, HIGH);
   delay(2);                    
break;  

case 3:
                         digitalWrite(ROTOR1_GREEN, LOW);  
   digitalWrite(ROTOR1_BLUE, LOW);
   digitalWrite(ROTOR1_RED, HIGH);  
   digitalWrite(ROTOR1_BLACK, HIGH);
   delay(2);                
break;  

case 4:
                         digitalWrite(ROTOR1_GREEN, LOW);  
     digitalWrite(ROTOR1_RED, LOW);  
   digitalWrite(ROTOR1_BLACK, HIGH);  
   digitalWrite(ROTOR1_BLUE, HIGH);
   delay(2);                    
break;  
}  
    }

    return 0;

}

void loop()  
{
        while ( i <= 2000 )
        {
         PreformStep( UNWIND );
         delay(15);
         i++;
        }
   digitalWrite(ROTOR1_RED, LOW);  
   digitalWrite(ROTOR1_BLACK, LOW);
   digitalWrite(ROTOR1_GREEN, LOW);  
   digitalWrite(ROTOR1_BLUE, LOW);
}


you can work out the wiring using the images in my blog post: http://rhysmossom.com/2013/08/03/raspberry-pipiface-draw-bot-failure-1/
Logged


Pages: [1]   Go Up
Jump to: