Working on a gate opener motor driver

Hi @helijoc

welcome to the arduino-forum.

I describe this behaviour with my own words:

  • check if push-button is pressed
    • if push-button press is detected
      ===> start motor and make a note of the time when the motor is switched on

after that the code should do something different:

  • check if 10 seconds since motor switching on have passed by

    • if 10 seconds have passed by since motor on
      ====> stop motor
  • check if limit-switch is LOW

    • if limit switch IS low
      ====> stop motor
  • check if push-button is pressed (again)
    ====> stop motor

This can be seen as two different modes of operation.
In Programming such modes of operation are called "states"

state idling:

  • check if open-switch is triggered
    • if push-button-press is detected
      ===> start motor and make a note of the time when the motor is switched on
      ===> change to state openingGate

state openingGate:

  • check if 10 seconds since motor switching on have passed by

    • if 10 seconds have passed by since motor on
      ====> stop motor
  • check if limit-switch is LOW

    • if limit switch IS low
      ====> stop motor
  • check if push-button is pressed (again)
    ====> stop motor

The code should do either idling or openingGate

A very elegant solution for this is to use a state-machine by using the
switch-case-break;-statement

ATTENTION !

The break; is very very important !

The break; is that detail that makes code-execution mutually exclusive

remember:
you want to either

start the motor if switch triggered

or

stop the motor if switch is triggered

Which are opposite things start/stop
Further down the road you will have some more "states"

what shall happen if the door is completely opened and motor has stopped?
wait a fixed amount of time then closing it automatically ?
wait for switch to be triggered again closing the door?

With a state-machine it will be very easy to expand your code to add this behaviour

I have written a small tutorial about how state-machines are coded
take a look at it

best regards Stefan