Limit switches stop a DC motor permanently

Hello
Could you support with sketch
There is IR sensor that sucsessfully starts/stops DC motor in different directions to open/close sliding door.
2 limit switches stop DC motor when the door reaches these limites.
The problem is that each limit switch stops DC motor permanently and IR sensor doesn`t start/stop a motor anymore. Can you suggest how to continie starting/stoping DC motor via IR sensor while limit switch is pressed (signal is HIGH)?
Thank you in advance.

boolean oldState = LOW;
boolean newState = LOW;
byte state = 1;

void setup() {

  pinMode(10, OUTPUT); // outputs for motor
  pinMode(12, OUTPUT);
  pinMode(5, INPUT); //IR sensor
  pinMode(6, INPUT); //limit switch 1
  pinMode(7, INPUT); //limit switch 2
}

void loop()
{
  int stopButtonState1 = digitalRead(6);
  int stopButtonState2 = digitalRead(7);

  if (stopButtonState1 == HIGH) { //limit switch 1 is pressed
    digitalWrite(10, LOW); //motor is stopped
    digitalWrite(12, LOW);

  }
  if (stopButtonState2 == HIGH) { //limit switch 2 is pressed
    digitalWrite(10, LOW); //motor is stopped
    digitalWrite(12, LOW);
  }

  if (newState != oldState)
  {
    if (newState == LOW) {
      state++;
      if (state > 1) {
        state = 0;
      }
      digitalWrite(10, LOW); digitalWrite(12, LOW);

      if (state == 0) {
        digitalWrite(10, HIGH);
        digitalWrite(12, LOW);

      }

      if (state == 1) {
        digitalWrite(12, HIGH);
        digitalWrite(10, LOW);

      }

    }
  }

  oldState = newState;

}
  pinMode(5, INPUT); //IR sensor
  pinMode(6, INPUT); //limit switch 1
  pinMode(7, INPUT); //limit switch 2

Rather than writing comments that have to be referred back-to, why not give the pins meaningful names?

You realize your code never reads the IR sensor and that newState != oldState is never true?

  if (newState != oldState)
  {
    if (newState == LOW) {
      state++;
      if (state > 1) {
        state = 0;
      }
      digitalWrite(10, LOW); digitalWrite(12, LOW);

      if (state == 0) {
        digitalWrite(10, HIGH);
        digitalWrite(12, LOW);

      }

      if (state == 1) {
        digitalWrite(12, HIGH);
        digitalWrite(10, LOW);

      }

    }
  }

ToddL1962:
You realize your code never reads the IR sensor and that newState != oldState is never true?

  if (newState != oldState)

{
   if (newState == LOW) {
     state++;
     if (state > 1) {
       state = 0;
     }
     digitalWrite(10, LOW); digitalWrite(12, LOW);

if (state == 0) {
       digitalWrite(10, HIGH);
       digitalWrite(12, LOW);

}

if (state == 1) {
       digitalWrite(12, HIGH);
       digitalWrite(10, LOW);

}

}
 }

As I understand it depends on the sensor type. I use E18-D80NK and it looks it has LOW signal when object is found... Thank you.

TheMemberFormerlyKnownAsAWOL:

  pinMode(5, INPUT); //IR sensor

pinMode(6, INPUT); //limit switch 1
 pinMode(7, INPUT); //limit switch 2



Rather than writing comments that have to be referred back-to, why not give the pins meaningful names?

This is much better. You are absolutely right. Thanks.

...and what about the other comment, that you're not even reading the sensor? Did you read that?

aarg:
...and what about the other comment, that you're not even reading the sensor? Did you read that?

aarg:
Yes, I really missed that in the code, sorry

boolean oldState = LOW;

boolean newState = LOW;
byte state = 0;

void setup() {

pinMode(10, OUTPUT); // outputs for motor
  pinMode(12, OUTPUT);
  pinMode(5, INPUT); //IR sensor
  pinMode(6, INPUT); //limit switch 1
  pinMode(7, INPUT); //limit switch 2
}

void loop()
{
  int stopButtonState1 = digitalRead(6);
  int stopButtonState2 = digitalRead(7);

if (stopButtonState1 == HIGH) { //limit switch 1 is pressed
    digitalWrite(10, LOW); //motor is stopped
    digitalWrite(12, LOW);

}
  if (stopButtonState2 == HIGH) { //limit switch 2 is pressed
    digitalWrite(10, LOW); //motor is stopped
    digitalWrite(12, LOW);
  }

newState = digitalRead(5);
  if (newState != oldState)
  {
    if (newState == LOW) {
      state++;
      if (state > 1) {
        state = 0;
      }
      digitalWrite(10, LOW); digitalWrite(12, LOW);

if (state == 0) {
        digitalWrite(10, HIGH);
        digitalWrite(12, LOW);

}

if (state == 1) {
        digitalWrite(12, HIGH);
        digitalWrite(10, LOW);

}

}
  }

oldState = newState;

}

...and what was the result of fixing that?

aarg:
...and what was the result of fixing that?

We read sensor status (from 5 pin) and it allows to rotate motor in both directions but limit switch stops it forever

artu:
We read sensor status (from 5 pin) and it allows to rotate motor in both directions but limit switch stops it forever

That is what you said in your original post. So, does that mean that actually reading the sensor did not fix the problem?

artu:
We read sensor status (from 5 pin) and it allows to rotate motor in both directions but limit switch stops it forever

Change your code so that it only checks for LOW to HIGH transitions on limit switch inputs. That way it will stop the motor when the transition occurs but allow it to move for the sensor even though the input remains HIGH.

aarg:
That is what you said in your original post. So, does that mean that actually reading the sensor did not fix the problem?

Absolutely

We need to see code.

Otherwise, all we can say is, "it doesn't work because you made a mistake".

TheMemberFormerlyKnownAsAWOL:
We need to see code.

Sorry, what code?
The code below works, but is stopped when reached any limit switch

boolean oldState = LOW;
boolean newState = LOW;
byte state = 0;

void setup() {

  pinMode(10, OUTPUT); // outputs for motor
  pinMode(12, OUTPUT);
  pinMode(5, INPUT); //IR sensor
  pinMode(6, INPUT); //limit switch 1
  pinMode(7, INPUT); //limit switch 2
}

void loop()
{
  int stopButtonState1 = digitalRead(6);
  int stopButtonState2 = digitalRead(7);

  if (stopButtonState1 == HIGH) { //limit switch 1 is pressed
    digitalWrite(10, LOW); //motor is stopped
    digitalWrite(12, LOW);

  }
  if (stopButtonState2 == HIGH) { //limit switch 2 is pressed
    digitalWrite(10, LOW); //motor is stopped
    digitalWrite(12, LOW);
  }

  newState = digitalRead(5);
  if (newState != oldState)
  {
    if (newState == LOW) {
      state++;
      if (state > 1) {
        state = 0;
      }
      digitalWrite(10, LOW); digitalWrite(12, LOW);

      if (state == 0) {
        digitalWrite(10, HIGH);
        digitalWrite(12, LOW);

      }

      if (state == 1) {
        digitalWrite(12, HIGH);
        digitalWrite(10, LOW);

      }

    }
  }

  oldState = newState;

}

aarg:
Otherwise, all we can say is, "it doesn't work because you made a mistake".

it doesn't work because you made a mistake when you started to think that it would work :slight_smile:

I don't see any variable to store the motor direction. If you hit a limit switch, it should stop but if you want it to move again, it should only move away from the limit. For that, you need to allow movement in one direction but not the other.

Also, by this:

    if (newState == LOW) {
      state++;
      if (state > 1) {
        state = 0;
      }

Did you mean,

if (newState == LOW)
      state = not state;

and while we're on the subject, what does State represent? The state of what?

aarg:
I don't see any variable to store the motor direction. If you hit a limit switch, it should stop but if you want it to move again, it should only move away from the limit. For that, you need to allow movement in one direction but not the other.

Also, by this:

    if (newState == LOW) {

state++;
     if (state > 1) {
       state = 0;
     }




Did you mean,


if (newState == LOW)
     state = not state;





and while we're on the subject, what does State represent? The state of what?

Yes, if it releases the switch button it can moves again but if it move away from the limit it open a door a little bit...
State means direction of motor rotation (opening or closing the door)

artu:
Yes, if it releases the switch button it can moves again but if it move away from the limit it open a door a little bit...
State means direction of motor rotation (opening or closing the door)

Then why not eliminate a lot of confusion, and rename the variable "direction"?

I think nobody can help you until you describe the operation of your door in more detail. When should it move? In what direction? When should it stop? etc...

aarg:
Then why not eliminate a lot of confusion, and rename the variable "direction"?

I think nobody can help you until you describe the operation of your door in more detail. When should it move? In what direction? When should it stop? etc...

The logic is simple: I move my hand, sensor detects an object (my hand), starts to rotate motor in one direction and opens the door, limit switch stops motor. I move my hand again, sensor detects an object, starts to rotate motor in other direction and close the door untill limit switch stops that. Cycle is finish.
The original code works in part to detect and start motor in one direction.When limit switch is pressed the code stops. It must be started again in the other direction when the sensor detects an object, but the limit switch is pressed...