simulating automated slide door in leds goes wrong

Hello
I'm trying to make a simple program using Arduino UNO that controls a slide door
It's very simple containing the following steps :
A sensor which should give 5v when motion detected then the door must open until receiving a 5v from another sensor called (endopen) then it will close until receiving a 5v from another sensor called (endclose)
But I'm having problems using While and If statements, the door must remain opening whenever (sensor) value is HIGH
The following code cause the both (opendoor) and (closedoor) remain HIGH even without (sensor) high value:

int opendoor = 2;
int closedoor = 3;
int endopen = 4;
int endclose = 5;
int manopen = 6;
int manclose = 7;
int sensor = 8;

void setup() 
{
  pinMode(opendoor, OUTPUT);
  pinMode(closedoor, OUTPUT);
  pinMode(endclose, INPUT);
  pinMode(endopen, INPUT);
  pinMode(sensor, INPUT);
  pinMode(manopen, INPUT);
  pinMode(manclose, INPUT);
  
  
  digitalWrite(opendoor,LOW);
  digitalWrite(closedoor,LOW);

  Serial.begin(9600); //begins serial communication
 
}

void loop() {
            digitalWrite(opendoor,LOW);
            digitalWrite(closedoor,LOW);
       if  (digitalRead(sensor) == HIGH ) {
             do
             {
                digitalWrite(opendoor,HIGH);
                digitalWrite(closedoor,LOW);
                Serial.print("Opening");
             }
              while ((endopen) == LOW);
            }
            else if (digitalRead(endopen) == HIGH)
            {
              do
              {
            digitalWrite(opendoor,LOW);
            digitalWrite(closedoor,HIGH);
            Serial.print("---Closing----");
              }
              while ((endclose) == LOW);
            }
             else if (digitalRead(endclose) == HIGH)
            {

            digitalWrite(opendoor,LOW);
            digitalWrite(closedoor,LOW);
            Serial.print("---Closing----");

            }
            else
            digitalWrite(opendoor,LOW);
            digitalWrite(closedoor,LOW);
            
}

I'm beginner at programming ... Excuse me
thanks in advance

A sensor which should give 5v when motion detected then the door must open until receiving a 5v from another sensor called (endopen)

So what are these sensors and how are they wired up?
Remember that an input pin connected to nothing will float and can read any value. The sensor must supply not only a 5V when triggered but also a 0V when not triggered.

Grumpy_Mike:
So what are these sensors and how are they wired up?
Remember that an input pin connected to nothing will float and can read any value. The sensor must supply not only a 5V when triggered but also a 0V when not triggered.

Thanks for your response...well indeed it won't be a sensor but something like that (a relay maybe) the goal is to tell us the door has completely opened or closed
I'm representing (open,close) with two Leds and for (endopen,endclose,sensor) I'm connecting pins to 5v manually just for test purpose

Grumpy_Mike:
The sensor must supply not only a 5V when triggered but also a 0V when not triggered.

zampoot:
I'm connecting pins to 5v manually just for test purpose

What are you connecting for test purposes when you are not connecting to 5V? As GM says, you have to connect something, 5V or 0V. I suspect from your comment that you are connecting 5V or nothing, not 5V or 0V.

I think you need to read this:-
http://www.thebox.myzen.co.uk/Tutorial/Inputs.html

In short enable the internal pullup resistors and switch by temporally connecting the input to ground.

Only once you have the hardware right can you even consider the software.

PerryBebbington:
What are you connecting for test purposes when you are not connecting to 5V? As GM says, you have to connect something, 5V or 0V. I suspect from your comment that you are connecting 5V or nothing, not 5V or 0V.

That's right thanks for the tip

Grumpy_Mike:
I think you need to read this:-
http://www.thebox.myzen.co.uk/Tutorial/Inputs.html

In short enable the internal pullup resistors and switch by temporally connecting the input to ground.

Only once you have the hardware right can you even consider the software.

Thanks
I have followed the example here:
https://www.arduino.cc/en/Tutorial/InputPullupSerial

It works fine, but I want to reverse the conditions, So I've connected the button to 5v instead of GND
so in case we pressed the button it will pass 5v...But it seems not working
Is there any way to make (sensor) value = 0 by default unless the button is pressed ?

zampoot:
It works fine, but I want to reverse the conditions, So I've connected the button to 5v instead of GND

Bad idea.

It works fine, but I want to reverse the conditions

Reverse them in software, normal practice is button to ground, not button to 5V.

It works fine, but I want to reverse the conditions, So I've connected the button to 5v instead of GND

So you need a pull down resistor and that costs money as they are not built in. As the others have said it is a bad idea, which you would have known if you read my link fully.

If you have trouble thinking about what you think is “correct“ in your current state of knowledge then use these two lines at the start of your code.

#define CLOSED LOW
#define OPEN HIGH

And just use OPEN and CLOSED in your sketch.

Thanks
used a pull down resistor and now it's working fine using this code

int opendoor = 2;
int closedoor = 3;
int endopen = 4;
int endclose = 5;
int manopen = 6;
int manclose = 7;
int sensor = 8;
bool closed = false;
void setup() 
{
  pinMode(opendoor, OUTPUT);
  pinMode(closedoor, OUTPUT);
  pinMode(endclose, INPUT_PULLUP);
  pinMode(endopen, INPUT_PULLUP);
  pinMode(sensor, INPUT_PULLUP);
  pinMode(manopen, INPUT);
  pinMode(manclose, INPUT);
  
  
  digitalWrite(opendoor,LOW);
  digitalWrite(closedoor,LOW);

  Serial.begin(9600); //begins serial communication
 
}

void loop() {
            digitalWrite(opendoor,LOW);
            digitalWrite(closedoor,LOW);
            // digitalWrite(sensor,HIGH);
            digitalWrite(endopen,LOW);
            digitalWrite(endclose,LOW);
       if  (digitalRead(sensor) == HIGH ) {
          closed != closed;
                while  (digitalRead(endopen) == LOW)
                {
                digitalWrite(opendoor,HIGH);
                digitalWrite(closedoor,LOW);
                Serial.print("Opening");
                }
       }
        else if (digitalRead(endopen) == HIGH ) {
               while  (digitalRead(sensor) != HIGH & digitalRead(endclose) == LOW)
                {
                digitalWrite(closedoor,HIGH);
                digitalWrite(opendoor,LOW);
                Serial.print("--------------------clo----------------");
                }
        }
         else if (digitalRead(endopen) == LOW & digitalRead(endclose) == HIGH)
        {
                digitalWrite(closedoor,LOW);
                digitalWrite(opendoor,LOW);
                Serial.print("--------------------end close----------------");
        } 
           
            else
            digitalWrite(opendoor,LOW);
            digitalWrite(closedoor,LOW);
            
}
  
//void opening(){

  //if (digitalRead(endopen) == HIGH){
    //digitalWrite(opendoor,LOW);
  //  closing();
  //}
//}
//void closing(){
  //digitalWrite(closedoor,HIGH);
  //Serial.print("Closing");
  //if (digitalRead(endclose) == HIGH){
   // digitalWrite(closedoor,LOW);
    
  //}
//}

Any other notes are welcome

INPUT_PULLUP and a pull down resistor is double-bad.
Which means: pull the pin up internally and down externally when the switch is open.

Just wire the switch between pin and ground, and use INPUT_PULLUP. No resistors.

Remember that an open switch is now HIGH, and a closed switch is now LOW.
Use that in your code.

Didn't look too good at the sketch, but

if (digitalRead(sensor) == HIGH ) {
might have to be changed into
if (digitalRead(sensor) == LOW) {

Same with the other digitalRead(sensor) lines.
Leo..

zampoot:
Any other notes are welcome

If you ignore what we all say then why on earth do you bother asking a question? Do you think you know better? If you did then you would not have to ask the question in the first place. Stop being so pig headed.

Wow! Grumpy!

Well you, and Wawa and PerryBebbington and me all told him it was the wrong way to implement a switch. I even posted a link will a full description why this was a bad idea and as far as I could see we were all totally ignored on this point. So what do you expect?

Precisely that! :grinning: :grinning: :grinning: