Pages: 1 [2]   Go Down
Author Topic: Controlling direction of unipolar stepper with buttons  (Read 1155 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

This sketch "works" sometimes and not others. The button input is very sluggish and slow.

Code:
#include <Stepper.h>

  int button1 = 2;
  int button2 = 3;
  int stepCount=0;
  int buttonState1 = 0;
  int buttonState2 = 0;
  const int stepsPerRevolution = 100;

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

void setup() {

  Serial.begin(9600);
  pinMode (button1,INPUT);               //shift to pin 2
  pinMode (button2,INPUT);               //shift to pin 3
  myStepper.setSpeed(30);

  myStepper.step(1);
  //delay(500);
  digitalWrite (button1, LOW);
  digitalWrite (button2, LOW);
}

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

  if (buttonState1 == HIGH)
  {
  stepCount ++;
  }
  if (stepCount >1)
  {               
  myStepper.step(stepsPerRevolution);//stepsPerRevolution
  }
  if (buttonState2 == HIGH)
  {
    stepCount --;
  }
  if (stepCount  < 1)
  {           
  myStepper.step(-stepsPerRevolution);
  }
  Serial.println(stepCount);
}

 
Logged

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

Why have you changed the way you were doing things ?
You have moved away from
Code:
loop
  start moving right
  while rightSwitch not pressed
    move a step to the right
  end of while //go back and check the right switch
start moving left
  while leftSwitch not pressed
    move a step to the left
  end of while //go back and check the left switch
end of loop
Did you ever explain how the switches are wired ?  Pullup/pulldown resistors or not ?
In each pass through loop() you check both switches.  Why ?
What's all the messing about with stepCount supposed to do ?
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

The 10k resistor goes to ground. When the button is pressed, the meter shows 6v. I tried the "while" but didn't know how to write the code. I thought checking the switches would define direction, i.e. 1 or 0. They do sometimes but the serial monitor shows inconsistent values. If I hold one of the buttons for several seconds, it will add or subtract but of course this is too long. 
Logged

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

The switch pins will normally be LOW
Try this
Code:
void loop()
{
  while (digitalRead(rightSwitchPin) == LOW) //keep going until limit switch reached
  {
    myStepper.step(1);  //move right
  }

  while (digitalRead(leftSwitchPin) == LOW) //keep going until limit switch reached
  {
    myStepper.step(-1);  //move left
  }
}
NOTE 1 - I do not have a stepper motor to try this on.  You may need to adjust speed etc
NOTE 2 - The directions may be wrong.  If so,  swap the 1 and -1 in the step calls.
NOTE 3 - I have changed the names of the switch pins to make it more obvious what is going on
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

This is working as long as the respective button is pressed. When released, it kind of stutters along. I am probably not describing what I want clearly. On either side of the printer carriage there is a limit switch. When this is pressed, I want the motor reversed. At that moment, the switch will go low again. The problem is writing code that will disregard the low state but send the mechanism to the switch at the opposite end.  That is what the count business (0,1) was intended to achieve. 0 for left, 1 for right.
Logged

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

I understand what you want and that is what the code provides for.  When the right hand limit switch is reached the input to its Arduino pin is no longer LOW and the program exits the first while loop.  The state of the right hand switch no longer matters as it is not being checked.  The same thing happens going from right to left.

Comment out the second while loop (3 lines).  Does the carriage move to the end and stop ?
Put the second while loop back in place and comment out the first one (3 lines).  Does the carriage move to the other end and stop ?

You say in an earlier post that when pressed the switches go to 6V.  This sounds suspicious.  Where are you measuring the 6V ?
Can you please draw a simple diagram to show the circuit including the switch, ground the Arduino power pins, resistor and switch ?
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

Beautiful, incredible, flawless, marvelous!  The fault was the switches; they were floating. I had them connected; pin to resistor to ground.  Your patience is much appreciated. This sculpture involves servos, steppers. and DC motors, none of which I have much experience with.  I am breaking it down, one challenge at a time. Splicing it altogether is going to be a hair pulling ordeal! Thanks again.
Keith
Logged

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

Glad it worked.
See you at the next stage of the challenges
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

Here is a fuzzy video of the stepper, part of an armature for a sculpture. http://www.youtube.com/watch?v=9IfrorvjIXQ&feature=youtu.be
Logged

Pages: 1 [2]   Go Up
Jump to: