Automated/Remote Kennel Door

Firstly, this bears remarkable resemblance to many chicken coop doors, the two main differences are the weight of the door to be lifted (which of course doesn't affect the code) and the fact that i want it half timed and half remote controlled (web/button).
Essentially i want it to open at 0700 every day, unless i manually use the web or physical button to open it prior. But then of a night time i want to operate it only when the dogs are settled inside already. I have a camera in there so i can see via the web if they're indoors, if i'm not physically present.

Parts:
AC corded drill
2 relay module
NodeMCU ESP8266 ESP-12 WeMos D1 Mini Dev board
Hi-Link HLK-PM03 (to supply 3.3V from 220V for the above)
physical switch
2 limit switches

Code so far, there is no time aspect to this yet and i have not even begun to think about web control.
This is just to operate the door using a physical momentary button.
i have two variables commented out, that i haven't firgured out a type for yet. I saw @Robin2 use the methodology of a variable to store whether the door was at the Top, Bottom, going Up or going Down on another thread but he info was a loose suggestion rather than final code snippet.

bool DoorOpenState;
/*DoorState = "T";*/
/*MotorState = */
#define OpenPin 6;
#define ClosePin 7;
const int ButtonPin = 6;
const int TopLimitPin = 7;
const int BottomLimitPin = 8;
unsigned long currentMillis;
unsigned long startMillis;
unsigned long previousButtonMillis;
int buttonInterval = 100;
int ButtonPressed;


void setup() {
  // put your setup code here, to run once:

}


  void ReadButton()
{
  {
    if (currentMillis - previousButtonMillis >= buttonInterval) {

      ButtonPressed = digitalRead(ButtonPin);
      if (ButtonPressed == LOW) { // assumes LOW when pressed
        if (MotorState == 'T')
        {
          MotorState = 'D';
        }
        if (MotorState == 'B')
        {
          MotorState = 'U'
        }
      }

    }
  }
  else
  {
    buttonState = HIGH;

  }
}

void ReadLimitSwitches()
{

}

void ReadWebInput()
{

}

void DoorMove()
{
  if (DoorState == 'U') {
    // code to move up one step
    TopLimitState = digitalRead(TopLimitPin);
    if (TopLimitState == LOW) {
      MotorState = 'T';
    }
  }
  if (DoorState == 'D') {
    // code to move up one step
    BottomLimitState = digitalRead(BottomLimitPin);
    if (BottomLimitState == LOW) {
      MotorState = 'B';
    }
  }
  // similarly for going down

}

void OpenDoor()
{
  digitalWrite(OpenPin, LOW);
  digitalWrite(ClosePin, HIGH);
}
void CloseDoor()
{
  digitalWrite(ClosePin, LOW);
  digitalWrite(OpenPin, HIGH);
}

void StopDoor()
{
  digitalWrite(ClosePin, LOW);
  digitalWrite(OpenPin, LOW);
}

}

void loop() {
  // put your main code here, to run repeatedly:
  ReadButton();
  ReadWebInput();
  ReadLimitSwitches():
  OpenDoor();
  CloseDoor();

  }

I would appreciate some insights in how i can proceed, with regards to the variables mentioned above, the general flow of the code i have, etc...
I will start to investigate the web control aspects when this stage works

If you have such doubts, the best approach is to break the project down into modular elements that can be developed and tested individually. For example, one such element would be "determining that it is 7:00". Another might be, "opening and closing the door"... and so on...

Really, your loop() function appears to be a kind of "gaslighting yourself", or filler. For example, as it stands, it would repeatedly open and close the door regardless of what ever else is going on. I think the correct high level structure would be more obvious to you if you coded the individual functions and tested them first as I suggest. You're trying to leap to the end of the book.

With limit switches in place, I don't know that you need those state variables. If you get a command to open the door, whether by time of day, pushbutton or web command, open it.

The open process should check the limit switch constantly and check it before turning the motor on. This way, your state effectively is the limit switches.

aarg:
If you have such doubts, the best approach is to break the project down into modular elements that can be developed and tested individually. For example, one such element would be "determining that it is 7:00". Another might be, "opening and closing the door"... and so on...

Really, your loop() function appears to be a kind of "gaslighting yourself", or filler. For example, as it stands, it would repeatedly open and close the door regardless of what ever else is going on. I think the correct high level structure would be more obvious to you if you coded the individual functions and tested them first as I suggest. You're trying to leap to the end of the book.

Thank you, i was chopping out code from another project that had this approach of using loop to go through the main functions that check states and act accordingly

wildbill:
With limit switches in place, I don't know that you need those state variables. If you get a command to open the door, whether by time of day, pushbutton or web command, open it.

The open process should check the limit switch constantly and check it before turning the motor on. This way, your state effectively is the limit switches.

Ok thankyou, that might simplify it a bit, i will try to rework it and repost it

Ok i've seen other similar projects putting the limit switches in the power connections to the motor, so they cut the power directly, rather than inform the arduino to do it based on logic.
This seems simpler, less to code.

I'm using an AC drill, a question i haven't considered is what power i need to supply to the motor, given i am removing the stock trigger and power/direction control. Am i dealing with mains voltage to the motor (which is what i assumed)? In which case, limit switches in line would be unsafe, although i could use them to switch further relays inside a suitable sealed enclosure

Ok, so if i use limit switches to just isolate power (with relays) rather than play any part in the logic, i assume the code will just be in one of two states 'opening' or 'closing', because it won't 'know' the door has reached it's limit.

Is that right and sensible?

So i'll check for button presses, web commands and time of day and run either OpenDoor() or CloseDoor()?

greenbeast:
I'm using an AC drill, a question i haven't considered is what power i need to supply to the motor, given i am removing the stock trigger and power/direction control. Am i dealing with mains voltage to the motor (which is what i assumed)? In which case, limit switches in line would be unsafe, although i could use them to switch further relays inside a suitable sealed enclosure

I'm sorry if this seems judgmental, it isn't intended as such - but based on many observations of a gap between what some experimenters think is safe and understood by them and what is actually the case, the fact that you are asking such questions, means that you shouldn't be doing this at all.

The problem is that the risks associated with line voltage application, are far too subtle and varied for a beginner or even intermediate electronics experimenter to fully comply with. These risks are mitigated almost completely by the understanding and application of standards and principles that have had many years to mature, and are implemented not only by individual engineers, but belong to a large body of practice that provides effective guidance to design and build devices that are safe "in the field". Not just safe for the experimenter, but also for the people around them who may unwittingly overlook precautions that the experimenter believes will protect them.

I suggest you abandon the idea of using this drill completely, and use some kind of low voltage motor and drive assembly. They are not difficult to source.

aarg:
I'm sorry if this seems judgmental, it isn't intended as such - but based on many observations of a gap between what some experimenters think is safe and understood by them and what is actually the case, the fact that you are asking such questions, means that you shouldn't be doing this at all.

The problem is that the risks associated with line voltage application, are far too subtle and varied for a beginner or even intermediate electronics experimenter to fully comply with. These risks are mitigated almost completely by the understanding and application of standards and principles that have had many years to mature, and are implemented not only by individual engineers, but belong to a large body of practice that provides effective guidance to design and build devices that are safe "in the field". Not just safe for the experimenter, but also for the people around them who may unwittingly overlook precautions that the experimenter believes will protect them.

I suggest you abandon the idea of using this drill completely, and use some kind of low voltage motor and drive assembly. They are not difficult to source.

That's fair, i just often start with what i have to hand.
If i were to stick with 12V equipment and run the limit switches in line, is my understanding about the code correct?

edit to add: just wanted to back track a little. I am comfortable working with mains voltage, i have installed and wired 3 phase distro boards for both my own workshop as well as a meat cutting plant, which including wiring the entire building for lights and single/3 phase sockets as well as walk in freezer/chiller rooms (control circuitry), only calling an engineer in to do the fridge copper plumbing and the gassing up.
My confusion was on what motors these things actually have in them. I saw talk about them being DC and therefore once you strip on the drill controls you need DC voltages, not AC. Hence needing clarification on what i need to feed it

I suppose if I were doing this, I would hard wire the limit switches, and concoct some way to read them also. I don't have a ready solution at hand, but it would be better to get direct feedback than to try to estimate the travel time and just wait until it's probably exceeded. One easy and versatile way would be with DPDT contacts on the switch. One set of switch contacts for the motor power, the other for the MCU to read. Hard wired switches will protect the actuator in case of MCU failure.

Thanks, i'm interested in your logic on that approach?
It adds more code (admittedly this is more of a hurdle for my understanding than yours), when the switches and relays will stop the motor anyway. Is there any need to 'know' it's reached top or bottom

More parts have arrived and i realised i can't use the limit switch/relay arrangement to isolate the motor because that system use a diode to allow current to still flow the other way when the limit switch is opened, and of course i cannot do that with AC.
So i might be on the hunt for a 12V drill after all

Could you check this schematic for me please?
I've gone with purely electrical limiting at the moment (no logic based limits)

The momentary switch is to switch states from Open to Close.

What value diodes do i need, i'll be using a 12V drill

Thanks

Sure, if you repost it as something bigger than a postage stamp.

greenbeast:
I am comfortable working with mains voltage, i have installed and wired 3 phase distro boards for both my own workshop as well as a meat cutting plant, which including wiring the entire building for lights and single/3 phase sockets as well as walk in freezer/chiller rooms (control circuitry), only calling an engineer in to do the fridge copper plumbing and the gassing up.

I post this reply not to you, as you seem to have absorbed the cautionary advice, but to other new experimenters that might read this. The experience above does not translate directly into HV safety on the experimenters bench. All the stuff you mention there, is already designed and built for safety, both the parts and the system. The ESA and/or other standards systems have been applied to every component that you used, and you have followed existing installation practices (I hope anyway). In no way, does the ability to do something like that, qualify someone to experiment with high voltages in an open, experimental environment.

aarg:
Sure, if you repost it as something bigger than a postage stamp.

Ha ha, i posted it raw and it seemed massive, so i reduced the size. Original coming up!

aarg:
I post this reply not to you, as you seem to have absorbed the cautionary advice, but to other new experimenters that might read this. The experience above does not translate directly into HV safety on the experimenters bench. All the stuff you mention there, is already designed and built for safety, both the parts and the system. The ESA and/or other standards systems have been applied to every component that you used, and you have followed existing installation practices (I hope anyway). In no way, does the ability to do something like that, qualify someone to experiment with high voltages in an open, experimental environment.

Ok i take this point also, and i agree that fair warning should be there for posterity and anyone viewing the topic in future

Larger version of the schematic as promised, this is the original, and the one posted above is 1/4 size (approx).

I have never used an ESP12 or the like, only fully fledged arduinos, so i don't know if my pin allocation is appropriate, i'm more concerned about the relay/limit switch layout and the diode type i need, the relays are probably not the correct or exact ones i'll use, but they sufficed to get things moving.

The coil voltage of your relay is 12V. Even if it was 3V, driving it directly from the ESP would be problematic at best.

When i searched for SPDT relays in KiCAD they just seemed to fit the bill initially, i'll hunt the list for a better alternative.

I'm switching 12V, but figured 3.3V would be adequate for energising the coil, i have an SSR that says 3-24V (i think!)

Since you have 12V available, the best move would be to drive the relays from 12V, but using a transistor driver between the ESP and the relay.

aarg:
Since you have 12V available, the best move would be to drive the relays from 12V, but using a transistor driver between the ESP and the relay.

Ok, thank you, i actually have a 2 relay module available to me, a 5V one though (edit to add, the specs say pickup voltage is 3.8V)
What do i need for diodes?