Stop a motor with a sensor

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);
  }
}

Welcome to the forum, and thanks for posting code correctly on your first attempt!

However, it is not clear what the question is.

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."
  }

You will need to introduce some mechanism to choose, perhaps a state variable, and an if ... else set of statements.

if (moving_up) {
do_one_thing();
}
else {
do_the_other_thing();
}

There are fundamental errors in your code with brace placement.

The paired braces "{}" below should enclose some statements. I don't know what the intent is here:

    while (digitalRead(sensora) == 0) {
    }
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);

Thank you so much, I'm going to implement this in my code.

Hi @frank0707 ,

feel free to read this tutorial

https://forum.arduino.cc/t/yet-another-finite-state-machine-introduction/1198997

created by @J-M-L where you'll also find a link to this example

https://wokwi.com/projects/383745078536243201

Using a state machine makes applications like yours much easier to handle ...

Good luck!
ec2021

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.