Hello everyone , good morning, I'm working on a project involving an electric roller blind. When a button is pressed, it should raise until the top infrared sensor no longer detects the blind (indicating it can't go up further), then wait for a few seconds before lowering until the bottom sensor detects the blind and stops. This part is already implemented and working correctly. However, I want to incorporate another emergency sensor, which, upon detecting an object below the blind, stops it, raises it back up, waits, and then lowers it again once there are no objects in the way.
The code is attached below:
int in1 = 13;
int in2 = 12;
int sensora = 2;
//lower botton
int sensorb = 3;
//upper botton
int emergencysensor = 5;
int button1 = 4;
// Variables para almacenar estados
int var_sensora = 0;
int var_sensorb = 0;
int var_sensoremergency = 0;
bool var_button1 = 0;
void setup() {
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(sensora, INPUT);
pinMode(sensorb, INPUT);
}
void loop() {
autom();
}
void autom() {
if (digitalRead(button3) == HIGH) {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
while (digitalRead(sensorb) == LOW) {
}
digitalWrite(in1, LOW);
digitalWrite(in2, 0);
delay(2000);
digitalWrite(in1, LOW);
digitalWrite(in2, 1);
while (digitalRead(sensora) == 1) {
}
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
}
}
Thank you for the welcome, I'm trying to implement another sensor in my project; however, when programming and adding a while loop, Arduino first reads one while loop and then the other. I want it to be either of the two.
if (digitalRead(button3) == HIGH) {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
while (digitalRead(sensorb) == LOW) {
}
digitalWrite(in1, LOW);
digitalWrite(in2, 0);
delay(2000);
digitalWrite(in1, LOW);
digitalWrite(in2, 1);
while (digitalRead(sensora) == 0) {
}
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
while (digitalRead(emergencysensor) == 0) {
}
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
delay(3000)
digitalWrite(in1, 1);
digitalWrite(in2, LOW);
// the problem is here, cause I want both while loops to execute regardless of the order,
// but it seems like only the first one is being read."
}