Noob: Programming 2 sensors to open and close curtains

I’m currently using the "existing code.ino" (attached) to open and close curtains with a stepper motor. But instead of determining the open and closed (or on and off) position by a precise amount of steps, I would like to achieve this with 2 magnetic sensors. So basically the curtains will continuously open or close until they touch the sensor.

This is the video where I’m basing my code from
DIY - Alexa Curtain Control System

This is a video where someone has already achieved it but didn’t reference the code.
Curtain opener explained

I can see this is the part of the code that needs to be changed but can’t get my head around it. The best I’ve managed to do is get one sensor working but not both by

Replacing this first line

       if (((millis()-TimerTravel) <= 7100UL) && (outstandingWork == true))  // Set the 'UL' figure to the tine it takes to open or close your curtain
      {
      Serial.println(F("Moving Curtain")); 
      moveCurtain();
      }
      else
      {
        outstandingWork = false;
        //Serial.println(F("Finished doing my work!"));
        //Serial.println(F("SLEEP HIGH"));
        digitalWrite(SLEEP,HIGH);
      }

With this first line

      if (digitalRead(SENSORB) == HIGH) // move curtains if sensor pin voltage is high

      {
      Serial.println(F("Moving Curtain")); 
      moveCurtain();
      }
      //else
      {
        //outstandingWork = false;
        Serial.println(F("Finished doing my work!"));
        Serial.println(F("SLEEP HIGH"));
          digitalWrite(SLEEP,HIGH);
      }

I've also attached "sample.ino" which is my modified version declaring the sensor pins and making one sensor work.

I expect this is a simple task for most people but I'm an idiot so if you can shed any light i'd be very grateful.

Regards

Dave

existing code.ino (4.4 KB)

sample.ino (4.73 KB)

//sensors ------------------

pinMode (SENSORA, INPUT);
pinMode (SENSORB, INPUT);

//end sensor --------------

You opened a catalog, and found the index, and located senors, and flipped to the appropriate page, and ordered the first two sensors on that page, right?

Of course not. You ordered very specific sensors. The names should reflect the kind of sensors you are actually using.

      if (digitalRead(SENSORB) == HIGH) // Set the 'UL' figure to the tine it takes to open or close your curtain

The comment might as well read "Check that Romeo was not cheating on Juliet" for all the relevance it has to the code.

The moveCurtain() function causes the stepper motor to step some number of times, in some undefined direction, regardless of whether the curtains are fully opened, fully closed, or somewhere in between. That function needs to cause the stepper motor to step ONCE and then read the appropriate sensor to determine if stepping again is required.

I've figured it out myself now by doing the following 8)

  if ((windowNotOpen == true) && (digitalRead(openSensor) == HIGH) || ((windowNotClosed == true) && (digitalRead(closedSensor) == HIGH)))
      {
      Serial.println(F("Moving Curtain")); 
      moveCurtain();
      }
      else
      {
        outstandingWork = false;
        windowNotOpen = false;
        windowNotClosed = false;
        //Serial.println(F("Finished doing my work!"));
        //Serial.println(F("SLEEP HIGH"));
        //digitalWrite(SLEEP,HIGH);

this could be seen as a state machine problem.
1)Receive a request to do a process (open or close - move motor in a direction)
2)Check if sensor has operated (or wait until sensor operates and initiates an interrupt)
3)respond to sensor state (stop motor or continue)
4)go to 1.

You have two sensors, one at each end of the curtain run (I guess). Therefore you may need to know if the curtain is at one end, at the other end or in the middle. If it's in the middle do you want it to close or to open?

In programming electric gates, we always assume that if you send a request while the gate is closing, we will reverse the gate and open it. If you send a request while opening we stop the gate. the next request always closes the gate.

You would benefit from determining what you want to happen in each case - or you could use these examples.

In your case at each end of the run, either one or the other sensor will be open and the other closed. That makes determining at which end the curtain is, easy. Next, what if it is in the middle - that can be indicated by which sensor last operated. That means understanding the state before the current operation and that is what state machines do.

I'm happy to help develop a state machine solution if you would like. (People get confused by what a state machine is - it's not a physical implementation of a machine, it is a way of designing programming steps, before beginning coding them).

A simple state chart is attached for a starting point.

Curtain driver state chart.doc (50.5 KB)