dc motor changing directions and limit switches

Hi,
If anyone could help me with the following problem id be very grateful.

I want a dc motor to rotate clockwise at the push of a button, and then rotate counterclockwise at the push of the same button (this much, I have successful code for)

const int motorbutton = 7;
int oldButtonState;               
int motorDirection = 0;  

void setup() {
  Serial.begin(9600);
  pinMode(motorbutton, INPUT_PULLUP);    
  oldButtonState = digitalRead(motorbutton);   
  pinMode(10, OUTPUT);     
  pinMode(11, OUTPUT);      

}

void loop() {
  int newButtonState = digitalRead(motorbutton);      
delay(50);
  if (newButtonState != oldButtonState) {          
    if (newButtonState == LOW) {               
      Serial.println("Motor Button just pressed");
      motorDirection = !motorDirection; 
      if (motorDirection)
        motorclockwise();
      else
        motoranticlockwise();
    }
  }

  oldButtonState = newButtonState;                 
}

void motorclockwise() {
  digitalWrite(10, HIGH);
  digitalWrite(11, LOW);

}

void motoranticlockwise() {
  digitalWrite(10, LOW);
  digitalWrite(11, HIGH);
 
}

I have 2 limit switches, that I want to program to halt the motor when it touches the limit switch, then wait for the command to move the motor in the other direction from the button. The motor should then rotate until it hits the other limit switch, and then wait for the button to be pressed again to rotate in the opposite direction.

I have attached a schematic, using push buttons instead of limit switches.

Any help in amending the code to include the limit switches would be greatly appreciated as I am a bit out of my depth.

Many thanks

In loop(), if the motor is running forward read the forward limit switch. When the limit switch is activated change the motor direction. Same for motor in reverse but check the reverse limit switch, of course.

You could usefully put the code for these checks and reverses in a function and call it from loop()

Thanks for your reply.

That makes perfect sense. Though I am at a bit of a loss how to code that.
would you be able to offer any more help?

thanks again

An outline of the function (not real code)

void checkLimits()
  if direction is forward
    read the forward limit switch
      if the switch is activated
        change direction
      end if
  end if
else
  if direction is reverse
    read the reverse limit switch
      if the switch is activated
        change direction
      end if
  end if
end function

All of the functions and logic required are used in the main program
Call the function from loop()

Thanks for your help.

I've amended the code as follows, but think i have made a total balls up of it.
would you mind casting your eye over it?

thanks again

const int motorbutton = 7;
const int switchp = 5;
const int switchc = 3;
int oldButtonState;               
int motorDirection = 0;  

void setup() {
  Serial.begin(9600);
  pinMode(motorbutton, INPUT_PULLUP);    
  pinMode(switchp, INPUT);
  pinMode(switchc, INPUT);
  oldButtonState = digitalRead(motorbutton);   
  pinMode(10, OUTPUT);     
  pinMode(11, OUTPUT);      

}

void loop() {
  int newButtonState = digitalRead(motorbutton);      
delay(50);
  if (newButtonState != oldButtonState) {          
    if (newButtonState == LOW) {               
      Serial.println("Motor Button just pressed");
      motorDirection = !motorDirection; 
      if (motorDirection)
        motorclockwise();
      else
        motoranticlockwise();
    }
  }

  oldButtonState = newButtonState;                 
}

void motorclockwise() {
  digitalWrite(10, HIGH);
  digitalWrite(11, LOW);

}

void motoranticlockwise() {
  digitalWrite(10, LOW);
  digitalWrite(11, HIGH);
 
}

void checkLimits(){
  if (motorDirection){
  motorclockwise();
    digitalRead(switchp);
      if (switchp == HIGH);
       Serial.println("Pol");
        motoranticlockwise();
  }
else
  if (motorDirection){
  motoranticlockwise();
    digitalRead(switchc);
      if (switchc == HIGH);
      Serial.println("Clear");
        motorclockwise();
}
}

rtta:
Thanks for your help.

I’ve amended the code as follows, but think i have made a total balls up of it.
would you mind casting your eye over it?

thanks again

you initial code looked close enough just needed some tweaking.

just use the debounce library (link to get library in code below):

// Detect the falling edge

// Include the Bounce2 library found here :
// https://github.com/thomasfredericks/Bounce2

#include <Bounce2.h>

const int motorbutton = 7;
const int switchp = 5;
const int switchc = 3;


// Instantiate a Bounce object :
Bounce debouncer = Bounce(); 

void setup() {
  
  Serial.begin(9600);
  
  // Setup the button with an internal pull-up :
  pinMode(motorbutton,INPUT_PULLUP);
  
  // After setting up the button, setup the Bounce instance :
  debouncer.attach(motorbutton);
  debouncer.interval(500);
  
  pinMode(10, OUTPUT);     
  pinMode(11, OUTPUT); 
}

void loop() {

  // Update the Bounce instance :
   debouncer.update();
   
   // Call code if Bounce fell (transition from HIGH to LOW) :
   if ( debouncer.fell() ) {
     Serial.println("Motor Button just pressed");
      
      if (digitalRead(switchp)) //if this is not the intended direction for 'swichp' change to 'switchc'
        motorclockwise();
      else
        motoranticlockwise();
      delay(100);
   }
   
   //stop motor is reached limit
   if(digitalRead(switchp)|digitalRead(switchc)){
       digitalWrite(10, LOW);
       digitalWrite(11, LOW);
  }
}

void motorclockwise() {
  digitalWrite(10, HIGH);
  digitalWrite(11, LOW);
}

void motoranticlockwise() {
  digitalWrite(10, LOW);
  digitalWrite(11, HIGH);
}

Not a bad start but not right.
I have added comments to your code

void checkLimits()    //you don't call this function from anywhere
{
  if (motorDirection)
  {
    motorclockwise(); //not needed as the motor is already rotating
    digitalRead(switchp); //you read the input and throw away the result
    if (switchp == HIGH); //switchp will always be HIGH but digitalRead(switchp) might be HIGH or LOW
    Serial.println("Pol");
    motoranticlockwise(); //don't forget to invert motorDirection when you change direction
  }
  else if (motorDirection)  //no need to check motorDirection again
  {
    motoranticlockwise();
    digitalRead(switchc);
    if (switchc == HIGH);
    Serial.println("Clear");
    motorclockwise();
  }
}
    if (switchp == HIGH); //switchp will always be HIGH but digitalRead(switchp) might be HIGH or LOW

5 is NOT HIGH. It is extremely rare for an if statement to end with a ;.