Limit a DC motor with switches.

Hey everyone,

I have some difficulties with the coding.

List of things that I use:
DC motor and two micro switches ( but I use it like a normal switch, so with two wires on each micro switch)

description what it need to do.
First, I will explain what I like the DC motor do. the DC motor need to move forward, backward and stop
when I press u, i, and o. u=forward, i=backward and o=stop.
Second, when the motor is moving forward and reach the switch. the motor has to stop and can only move backward when I press the key i.
and the same goes when the motor is moving backward and reach the other switch. the motor has to stop and can only move forward when I press the key u.

I have begun to write the code, but my stuck with the code of the switch. I don’t know how to incorporte the code of the switch, I don’t know what code I should write.
help please.

here is a draw of my system link https://postimg.org/image/ini5uy33h/

//input of limit switch
int button_pin=8;
int button_pin2=7;
int buttonState=0;
int pwm1=10;
int pwm2=11;

void setup() {
  //limit switch setup
  pinMode(button_pin, INPUT);
  pinMode(button_pin2, INPUT);
  analogWrite(pwm1,0);
  analogWrite(pwm2,0);


  
  Serial.begin(9600); // in case of USB connection
  //Serial1.begin(115200); // in case of ethernet cable or WiFi


  // clear the input buffer
  //while (Serial.available())
  //   Serial.read();
  //while (Serial1.available())
     //Serial1.read();  

}

void loop() {
  //
  buttonState = digitalRead(button_pin);
  Serial.println(buttonState);

  if(Serial.available() > 0)
  //if(Serial1.available() > 0)
  {

    char Command = Serial.read();
    //char Command = Serial1.read();
    
    switch(Command)
    { 
      case 'u': // move forward
      analogWrite(pwm1,255);
      analogWrite(pwm2,0);
      break;

      case 'i': // move backward
      analogWrite(pwm1,0);
      analogWrite(pwm2,255);
      break;
        
      case 'o':// stop
      analogWrite(pwm1,0);
      analogWrite(pwm2,0);
      break;

      if (buttonState == HIGH) {
        //digitalWrite(PWM1,0);
        analogWrite(pwm1,0);
      } else{
        //digitalWrite(PWM1,255);
        analogWrite(pwm1,255);
        break;}
      }
  }
}

Your description of what you have and your code do not match up. Please supply a schematic of your hardware so we can see what is connected to what.

Here is a drawing of the systeem. linkhttps://postimg.org/image/tbntbdi7z/

Which limit switch is that, UP or DOWN?

micro switch v4t7yr1. the switch is someting like this https://media.conrad.com/medias/global/ce/2000_2999/2200/2290/2291/1094411_BB_00_FB.EPS_250.jpg

kaas6758: limit switch v4t7yr1

Do you only have one limit switch for both UP and DOWN directions?!?

no, i have two switches. one for up and down for down

Thanks for that.
However having no pin numbers or labels on the block marked L2g8n is not helping me know what you have connected up.

There is no indication of how the switch and motor interact, if indeed they do interact.

You say in your code comments:-

// if a w,s,a or d is pressed servos move with a step of
// 5 degrees.

You have not drawn a servo motor in that diagram. There is no way what you have drawn can move in 5o increments, or in any other increment for that matter.
All you can do with that is turn the motor on and off, which is what you are doing. You can not control any angle.

Do you know that there is no difference between writing

analogWrite(pwm1, 255);
        analogWrite(pwm2, 0);

And

digitalWrite(pwm1, HIGH);
        digitalWrite(pwm2, LOW);

There is no analogue control at all if all you use are those two numbers.

Case ‘u’ and case ‘i’ run the same piece of code and so will do the same thing.

when the motor is moving forward and reach the switch. the motor has to stop and can only move backward when I press the key i.
and the same goes when the motor is moving backward and reach the other switch. the motor has to stop and can only move forward when I press the key u.

and reach the other switch

What other switch, your diagram shows only one switch!

other drawing https://postimg.org/image/ini5uy33h/

update code

//input of limit switch
int button_pin=8;
int button_pin2=7;
int buttonState=0;
int pwm1=10;
int pwm2=11;

void setup() {
  //limit switch setup
  pinMode(button_pin, INPUT);
  pinMode(button_pin2, INPUT);
  analogWrite(pwm1,0);
  analogWrite(pwm2,0);


  //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx       algemeen    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  Serial.begin(9600); // in case of USB connection
  //Serial1.begin(115200); // in case of ethernet cable or WiFi


  // clear the input buffer
  //while (Serial.available())
  //   Serial.read();
  //while (Serial1.available())
     //Serial1.read();  

}

void loop() {
  //
  buttonState = digitalRead(button_pin);
  Serial.println(buttonState);

  if(Serial.available() > 0)
  //if(Serial1.available() > 0)
  {

    char Command = Serial.read();
    //char Command = Serial1.read();
    
    switch(Command)
    { 
      case 'u': // move forward
      analogWrite(pwm1,255);
      analogWrite(pwm2,0);
      break;

      case 'i': // move backward
      analogWrite(pwm1,0);
      analogWrite(pwm2,255);
      break;
        
      case 'o':// stop
      analogWrite(pwm1,0);
      analogWrite(pwm2,0);
      break;

      if (buttonState == HIGH) {
        //digitalWrite(PWM1,0);
        analogWrite(pwm1,0);
      } else{
        //digitalWrite(PWM1,255);
        analogWrite(pwm1,255);
        break;}
      }
  }
}

Do you see how you only look at that ONE button when you have received something from the serial port?

You need to look at both buttons every time round the loop. You need to stop the motor if either button is pressed. But you need to remember which button is pressed and only allow movement opposite to that which caused that button to be pressed. You have not included enough information to be more specific.

In the case statement use an if statement to only turn on the motor if the direction is opposite to that which caused the press.

you are right, but i don't know how to do that.

I would start by keeping track of what the motor was last told to do:

const byte MOTOR_STOPPED = 0;
const byte MOTOR_UP = 1;
const byte MOTOR_DOWN = 2;
byte MotorState = MOTOR_STOPPED;

Then you can put the limit switch checks in loop():

    if (digitalRead(UP_LIMIT_PIN) == HIGH && MotorState == MOTOR_UP)
        MotorStop();  // Stop because the limit was reached

The 'MotorStop()' function would stop the motor and set the MotorState to MOTOR_STOPPED. Similar functions for MotorUp() and MotorDown() would be useful elsewhere. Those two should also check to make sure the limit has not been reached before turning the motor on.