Oscillating the direction of a motor based upon state of two switches

I basically have two push button switches and a motor. When the program starts the motor turns a mechanism until the first push button switch is pressed, the motor then changes direction. When it reaches the next push button switch I want it to stop and change direction until the first push button switch is pressed.

Of course, I've been trying to get it work by trying so many different things that the original stuff that was working isn't anymore.

#include <Servo.h>  // servo library
//Used IOs
// 2,3,4,5,6,7,8,9,10,11,12,13
    Servo servo1;  // servo
    int led1 = 4;  // This is the red light
    int led2 = 5;  // This is the yellow light
    const int button3Pin = 6;   // pushbutton 3 pin
    const int button2Pin = 7;   // pushbutton at the end of the drawer
    const int button1Pin = 10;  // pushbutton 1 pin
    int intReverse;
int button1State, button2State, button3State;
 
void setup() {
    Serial.begin(9600);
    pinMode(12, OUTPUT);       //Initiates Motor Channel A pin
    pinMode(9, OUTPUT);        //Initiates Brake Channel A pin
    pinMode(13, OUTPUT);       //Initiates Motor Channel B pin
    pinMode(8, OUTPUT);        //Initiates Brake Channel B pin
  
  //seting up for the lights
    pinMode(led1, OUTPUT); 
    pinMode(led2, OUTPUT); 

  //seting up for the buttons switches
    pinMode(button1Pin, INPUT);  //This is the switch on the back of the unit
    pinMode(button2Pin, INPUT);  //This is the switch on the front of the unit
    pinMode(button3Pin, INPUT);  //This is the switch on the side of the unit
    servo1.attach(2);            //This attaches the servo to IO 2
    
}

void loop() {
 
    
  button1State = digitalRead(button1Pin);


//when it is LOW (off) this code will not run
//when it is HIGH (on) this code will run

if (button1State == HIGH){
 MotorBaseBack();  //motor is high
 /*if (intReverse==1){
 MotorBaseBack2();
}

 if (intReverse==2){
 ForwardsA();
}*/

}

if (button1State == LOW){  //LOW is pressed and HIGH is not pressed
Back();  //the motor is low

}
}  //end of loop

void Back(){
   MotorBaseStop();
   ForwardsA();
/*   delay(5000);
   MotorsOff();
   delay(2000);
   DispenseMotor();*/
   intReverse=1;
}

void MotorBaseBack2(){
//  while (button1State =! HIGH && (intReverse==2)){
    digitalWrite(12, HIGH);     //Establishes forward direction of Channel A
    digitalWrite(9, LOW);       //Engage the Brake for Channel A
    analogWrite(3,175);  
}

void MotorBaseBack(){
    digitalWrite(12, HIGH);     //Establishes forward direction of Channel A
    digitalWrite(9, LOW);       //Engage the Brake for Channel A
    analogWrite(3, 175);
}

void ForwardsA(){
    digitalWrite(12, LOW); //Establishes forward direction of Channel A
    digitalWrite(9, LOW);   //Engage the Brake for Channel A
    analogWrite(3, 250);
}

void MotorBaseStop(){
    delay(50);
    digitalWrite(9, HIGH);   //Engage the Brake for Channel A

}

void MotorsOff(){
    digitalWrite(8, HIGH);  //Engages the brake for motor A
    digitalWrite(9, HIGH);  //Engages the brake for motor B
}

void DispenseMotor(){
    digitalWrite(13, HIGH); //Establishes forward direction of Channel B
    digitalWrite(8, LOW);   //Engage the Brake for Channel B
    analogWrite(11, 150);
    delay(1000);             //This dispenses the pills for 3/4 second
    digitalWrite(8, HIGH);  //Engage the Brake for Channel B
    }

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png or pdf?

Also are you using a motor, if so why do you have the servo library loaded.

Are you using a motor shield or a H-Bridge driver if you are using motors?

What exactly is your project not doing or doing wrong?

Tom...... :slight_smile:

I basically have two push button switches

On pins named button1Pin, button2Pin, and button3Pin?

It's not clear from your description if the servo pushes the buttons or a person does. It sounds like the servo pushes the buttons.

Think about the problem as a series of short sentences

motor direction is CounterClockwise (CCW)
motor moves
if left-button is pressed
motor direction changes to CW
motor moves
if right-button is pressed
motor direction changes to CCW

And this can be simplified into something like this

char motorDir = 'F';

void loop() {
  motorMove(); 
  if (digitalRead(left-button-pin) == LOW) {
      motorDir = 'F';
  }
  if )digitalRead(right-button-pin) == LOW) {
      motorDir == 'R';
  }
}

void motorMove() {
   if (motorDir == 'F') {
       // make motor move one way
   }
  else {
       // make motor move the other way
   }
}

...R

Thanks for everyone's input. I know it was a horribly written post when I was writing it, but figured there was enough to go on:-)

I tried breaking it up in small sections but it seemed to skip the section where it reverses direction. I'll clean up the code that doesn't affect my problem and simply it.

I'm using a motor shield for the motors.