Pages: [1] 2   Go Down
Author Topic: Controlling direction of unipolar stepper with buttons  (Read 1281 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 146
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am trying to control a unipolar stepper with two buttons, one on either side of a salvaged typwriter cage. The sketch will run it one one direction but the
button does not reverse the motor. Thanks for advise.
Code:
#include <Stepper.h>
Stepper s(20,8,9,10,11);
int button1 = 1;
int button2 = 2;
int buttonState1 = 0;
int buttonState2 = 0;

void setup()
{
 pinMode(1,INPUT); //buttons
 pinMode(2,INPUT);
 
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);

s.setSpeed(500);
s.step(1500);//200
//delay(5000);
}

void loop()
{
 buttonState1 = digitalRead(button1);
 buttonState2 = digitalRead(button2);

if (buttonState1 == HIGH)
{
s.setSpeed(-500);
s.step(1500);//200
//delay(1000);
}

if (buttonState2 == HIGH)
{
s.setSpeed(500);
s.step(-1500);
delay(1000);
}


}
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 644
Posts: 50463
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
pinMode(1,INPUT); //buttons
 pinMode(2,INPUT);
No internal pullup resistors. So, you need external resistors. How are the "buttons" wired? Why not uses switches, instead?

Indenting
is
a
good
thing.
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 118
Posts: 4353
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Forget the buttons for now. 

Write a sketch that turns the motor in one direction for a period then reverses it for a period and repeats.  You will then know that you can control the motor and can introduce the buttons.  Your sketch is considerably more complicated than the example from the stepper library.  Do you really need to set the pinMode of pins 8 to 11 for example ?
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Full Member
***
Karma: 0
Posts: 146
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, I have external, 10K pull down resistors. I edited the code a bit but it originally was running a back and forth pass at the proper speed. When I manually push the buttons, it will only advance the motor, not change direction.
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 118
Posts: 4353
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your method of reversing the motor seems very complicated
The example from the stepper library manages to do it like this
Code:
void loop()
{
  myStepper.step(stepsPerRevolution);
  delay(500);
  myStepper.step(-stepsPerRevolution);
  delay(500);
}

When you do this
Code:
    s.setSpeed(-500);
    s.step(1500);

Is it by any chance equivalent to
Code:
    s.setSpeed(500);
    s.step(-1500);

Negative speed and positive steps.  Positive speed and negative steps
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Full Member
***
Karma: 0
Posts: 146
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, that seems simple enough but how do I plug in the buttons?
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 146
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The buttons and direction is moving correctly but this sketch will only move a limited number of steps rather than back and forth. A sonar sensor input will initiate and stop the motor.

Code:
#include <Stepper.h>

int button1 = 1;
int button2 = 2;
int buttonState1 = 0;
int buttonState2 = 0;

const int stepsPerRevolution = 100; 

Stepper myStepper(stepsPerRevolution, 8,9,10,11);           

void setup() {
  pinMode (button1,INPUT);
  pinMode (button2,INPUT);
  // set the speed at 60 rpm:
  myStepper.setSpeed(80);
  myStepper.step(stepsPerRevolution);
  //delay(500);
   
}
void loop() {
  buttonState1 = digitalRead (button1);
  buttonState2 = digitalRead (button2);
  if(buttonState2 == HIGH)
  {
  // step one revolution  in one direction:
  myStepper.step(stepsPerRevolution);
  //delay(500);
  }
 
  if (buttonState1 == HIGH)
  {
   // step one revolution in the other direction:
  myStepper.step(-stepsPerRevolution);
  //delay(500);
  }
}
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 644
Posts: 50463
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
A sonar sensor input will initiate and stop the motor.
How?

Quote
but this sketch will only move a limited number of steps rather than back and forth.
That sketch would only go back and forth (whatever than means for a motor that rotates) if you alternated which switches you pressed.

You still haven't said how the switches are wired. You need to state exactly what happens when you press ONE of the switches, and exactly what happens when the press the other switch (and when you press that switch - while the stepper is moving or after it completes a rotation).

Please keep in mind that we are not trying to harass you. We can't see the hardware you have, or what it (or you) is/are doing.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 146
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry, my description of the problem is often another problem! Here is a photo of the setup http://www.flickr.com/photos/50454200@N06/8508893686/ You can see the blue button at the far left of the carriage. The stepper is mounted on the slide which presses the buttons on either end. I want the mechanism to slide back and forth, limited only by the buttons which send it in the opposite direction.  Now it will only go as far as  "myStepper.step(stepsPerRevolution);"
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 644
Posts: 50463
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What you need to do is know which way you are stepping. On each pass through loop, see if the "Stop, you've hit the end" switch is pressed. If not, step ONCE. If so, change the direction.

That way, each pass through loop you read one of the switches (the one that the carriage is moving towards) and step or change direction without stepping.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 146
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I understand what you are explaining but am at a near complete loss how to program the solution. How does the sketch determine the current direction?
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 118
Posts: 4353
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
How does the sketch determine the current direction?
Because you tell it.

Code:
loop
  start moving right
  while rightSwitch not pressed
    move a step to the right
  end of while
start moving left
  while leftSwitch not pressed
    move a step to the left
  end of while
end of loop
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Full Member
***
Karma: 0
Posts: 146
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I tried to adapt the example stepper sketch without success.

Code:



#include <Stepper.h>


const int stepsPerRevolution = 48;  // change this to fit the number of steps per revolution
                                     // for your motor
int buttonOne = 1;
int buttonTwo = 2;
int buttonValOne = 0;
int buttonValTwo = 0;

Stepper myStepper(stepsPerRevolution, 8,9,10,11);           

int stepCount1 = 0;
int stepCount2 = 0;// number of steps the motor has taken

void setup() {
  pinMode (buttonOne, INPUT);
  pinMode (buttonTwo, INPUT);
 
}

void loop() {
  buttonValOne = (digitalRead, buttonOne);
  buttonValTwo = (digitalRead, buttonTwo);
 
  while (buttonValOne !=HIGH)
  {
   // step one step:
  myStepper.step(1);
  stepCount1++;
  delay(500);
}

  while (buttonValTwo !=HIGH)
  {
   // step one step:
  myStepper.step(1);
  stepCount1--;
  delay(500);

}
}
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 118
Posts: 4353
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I can't see anything in your code that reverses the direction of the motor.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Full Member
***
Karma: 0
Posts: 146
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Forgot to add the "-" --------myStepper.step(-1);  If I change "if" to "while", the buttons don't respond but if continually pressed manually, they do operate as intended. Of course they should I want just a momentary switch to send in in reverse.  Thanks for the help!
Logged

Pages: [1] 2   Go Up
Jump to: