~ dc motor with limit switch & adding a manual override button ~

i plan on using a dc motor connected to a pulley to raise and lower a door until it hits an upper and lower limit switch, in repsonse to being activated by a photoresistor recieving a particular light level twice a day. this is the code i did i think it's probably okay hopefully

const int photocellPin = A0;     // the cell and 10K pulldown are connected to a0
int photocellReading;     // the analog reading from the sensor divider
boolean x = true;

const int in3 = 7;  // this is the pins for the h-bridge
const int in4 = 6;

const int toplimitswitch = 8;
const int bottomlimitswitch = 9;

void setup() {
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);

  pinMode(toplimitswitch, INPUT); // top limit switch
  pinMode(bottomlimitswitch, INPUT); // bottom limit switch

  Serial.begin(9600);
}

void loop() {
  photocellReading = analogRead(photocellPin);
  Serial.print("Analog reading = ");
  Serial.println(photocellReading);     // the raw analog reading
  delay(100);

  if ((photosensor << 300) && (x = true)) {
    digitalWrite(in3, HIGH);  // turn the motor in one direction (closes door)
    digitalWrite(in4, LOW);
    if (digitalRead(bottomlimitswitch) = HIGH) {
      digitalWrite(in3, LOW);  // turn the motor off
      digitalWrite(in4, LOW);
      x = false;
    }
  }

  else if ((photosensor >> 800) && (x = false)) {
    digitalWrite(in3, LOW);  // turn the motor in other direction (opens door)
    digitalWrite(in4, HIGH);
    if (digitalRead(toplimitswitch) = HIGH) {
      digitalWrite(in3, LOW);  // turn the motor off
      digitalWrite(in4, LOW);
      x = true;
    }
  }
}

i was wondering if i added a manual override switch to that what would it involve if it's not too difficult. from what i can gather i would require one button to switch it from automatic mode (using photo resistor) to manual and another to make the door go up and down manually

i found this on switches:

const byte switchPin = 8;
byte oldSwitchState = HIGH;  // assume switch open because of pull-up resistor
const unsigned long debounceTime = 10;  // milliseconds
unsigned long switchPressTime;  // when the switch last changed state

void setup ()
  {
  Serial.begin (115200);
  pinMode (switchPin, INPUT_PULLUP);
  }  // end of setup

void loop ()
  {
  // see if switch is open or closed
  byte switchState = digitalRead (switchPin);
  
  // has it changed since last time?
  if (switchState != oldSwitchState)
    {
    // debounce
    if (millis () - switchPressTime >= debounceTime)
       {
       switchPressTime = millis ();  // when we closed the switch 
       oldSwitchState =  switchState;  // remember for next time 
       if (switchState == LOW)
          {
          Serial.println ("Switch closed.");
          }  // end if switchState is LOW
       else
          {
          Serial.println ("Switch opened.");
          }  // end if switchState is HIGH
           
       }  // end if debounce time up
        
    }  // end of state change
     
  // other code here ...
   
  }  // end of loop

which i thought is quite easy i can just take my limit switch code and pop it in there, but i was wondering if i would need to use it twice once to go from automatic to manual and a second time for the door going up and down and whether i am going about it wrong, it seems it would have no way to go back from manual to automatic once it's in that function (is that the right word?) if that makes sense.

const int photocellPin = A0;     // the cell and 10K pulldown are connected to a0
int photocellReading;     // the analog reading from the sensor divider
boolean x = true;

const int in3 = 7;  // this is the pins for the h-bridge
const int in4 = 6;

const int toplimitswitch = 8;
const int bottomlimitswitch = 9;

const byte switchPin = 9;
byte oldSwitchState = HIGH;  // assume switch open because of pull-up resistor
const unsigned long debounceTime = 10;  // milliseconds
unsigned long switchPressTime;  // when the switch last changed state

void setup() {
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);

  pinMode(toplimitswitch, INPUT); // top limit switch
  pinMode(bottomlimitswitch, INPUT); // bottom limit switch

  Serial.begin(9600);

  Serial.begin (115200);   // is this needed at a higher number seems odd to have both
  pinMode (switchPin, INPUT_PULLUP);
}

void loop() {
  // see if switch is open or closed
  byte switchState = digitalRead (switchPin);

  // has it changed since last time?
  if (switchState != oldSwitchState)
  {
    // debounce
    if (millis () - switchPressTime >= debounceTime)
    {
      switchPressTime = millis ();  // when we closed the switch
      oldSwitchState =  switchState;  // remember for next time
      if (switchState == LOW)
      {
        Serial.println ("Switch closed.");
        photocellReading = analogRead(photocellPin);
        Serial.print("Analog reading = ");
        Serial.println(photocellReading);     // the raw analog reading
        delay(100);

        if ((photosensor << 300) && (x = true)) {
          digitalWrite(in3, HIGH);  // turn the motor in one direction (closes door)
          digitalWrite(in4, LOW);
          if (digitalRead(bottomlimitswitch) = HIGH) {
            digitalWrite(in3, LOW);  // turn the motor off
            digitalWrite(in4, LOW);
            x = false;
          }
        } // ?

        else if ((photosensor >> 800) && (x = false)) {
          digitalWrite(in3, LOW);  // turn the motor in other direction (opens door)
          digitalWrite(in4, HIGH);
          if (digitalRead(toplimitswitch) = HIGH) {
            digitalWrite(in3, LOW);  // turn the motor off
            digitalWrite(in4, LOW);
            x = true;
          }
        }
      }  // end if switchState is LOW
      else
      {
        Serial.println ("Switch opened.");
                 
              //HERE I WOULD BE REPEATING THE SWITCH CODE for manual up and down?????????????
        
      }  // end if switchState is HIGH
    }  // end if debounce time up
  }  // end of state change
}

am i on the right track? thanks for any help

the short answer is to check the override switch in addition to your photo sensor. In other words, if photoSensor OR overrideSwitch. But you presumably don't want to hold the switch down until the door has completely opened or closed.

a more professional approach is to recognize events and perform actions based on those events.

the events are the photoSensor is above some level, below some level and either limit switch has become active. In addition, the manual override switch has been pressed.

there's a distinction between a switch "is" pressed and "was" pressed. In other words, recognizing that a switch has become active is the event. Its' the change in state that is the event.

the actions are simply: stop the motor, start it in one direction and start it in the opposite direction. And the states are Stopped, Up and Down

so if the photoSensor is less than some level AND the state is Up, start the motor in one direction and change the state to . By testing if the state is Up, the code is not constantly starting the motor.

if either limit switch is pressed (again, changes state), stop the motor. No need to constantly stop the if a limit switch is pressed.

and is an override switch is hit, there are several actions. If the state is Up or Down, start the motor moving in the appropriate direction. If it's moving, you may want to stop it and if you keep track of the previous state, pressing the override switch again can make it move in the opposite direction.

I would recommend using sub-functions to stop and start the motor in either direction and setting a state variable. loop() can check for changes in switch inputs (possibly debounce) and process events conditional on state. separating the actions, processing of inputs and events will make the code easier to develop, understand and maintain.

gciurpita:
the short answer is to check the override switch in addition to your photo sensor. In other words, if photoSensor OR overrideSwitch. But you presumably don't want to hold the switch down until the door has completely opened or closed.

a more professional approach is to recognize events and perform actions based on those events.

the events are the photoSensor is above some level, below some level and either limit switch has become active. In addition, the manual override switch has been pressed.

there's a distinction between a switch "is" pressed and "was" pressed. In other words, recognizing that a switch has become active is the event. Its' the change in state that is the event.

the actions are simply: stop the motor, start it in one direction and start it in the opposite direction. And the states are Stopped, Up and Down

so if the photoSensor is less than some level AND the state is Up, start the motor in one direction and change the state to . By testing if the state is Up, the code is not constantly starting the motor.

if either limit switch is pressed (again, changes state), stop the motor. No need to constantly stop the if a limit switch is pressed.

and is an override switch is hit, there are several actions. If the state is Up or Down, start the motor moving in the appropriate direction. If it's moving, you may want to stop it and if you keep track of the previous state, pressing the override switch again can make it move in the opposite direction.

I would recommend using sub-functions to stop and start the motor in either direction and setting a state variable. loop() can check for changes in switch inputs (possibly debounce) and process events conditional on state. separating the actions, processing of inputs and events will make the code easier to develop, understand and maintain.

thanks for reply, i thought i was close so this was bad to read but i appreciate how you explained it in terms of recognising the change of state and events which i will look into. im going to just do the limit switches first then add the button later as it seems like it will take me too long to do it all in one go. but thanks again as you were the only reply also