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.
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.
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 ?
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.
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.
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?