Controlling direction of unipolar stepper with buttons

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

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.

#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);
  } 
}

A sonar sensor input will initiate and stop the motor.

How?

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.

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);"

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.

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?

How does the sketch determine the current direction?

Because you tell it.

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

I tried to adapt the example stepper sketch without success.

#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);

}
}

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

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!

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

#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); 
}

Why have you changed the way you were doing things ?
You have moved away from

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 ?

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.

The switch pins will normally be LOW
Try this

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

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.

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 ?

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

Glad it worked.
See you at the next stage of the challenges

Here is a fuzzy video of the stepper, part of an armature for a sculpture. - YouTube