Need help programming limit switches.

I have some programming experience with c++/c so I'm not a complete noob, but there are some things that I don't understand in arduino IDE. I am moving a door with a motor, and reversing the direction on a timer. so basically I want the motor to run one way until it hits a limit switch and then I want the motor to turn off for twelve hours, run the opposite direction until it hits the other limit swith and turn off for twelve hours, over and over and over again. I have posted a picture of my circuit. I have two inputs (limit switches), then I have one pin controlling the off and on of the motor and one pin controlling the direction of the current to the motor.

any help with code would be much appreciated.

Here is a circuit for you to think about:

Edit: Maybe add a fuse also.

I guess I should have done more explaining. The limit switches are not shown in the schematic. The reason I have 4 relays is because one set of relays had a 5v coil that I could power with the arduino and the other relays have a 12v coil. So basically I could use only 2 relays if I had the right ones. again the limit switches are not shown in my diagram. I'm gonna be honest... I don't understand your schematic very much, you're flyin over my head lol.

On the left-hand side of the schematic is a switch S20, labeled Manual/Auto.
In the Manual position, the two momentary pushbutton switches S18 and S19 can open or close your door.
In the Auto position, the Arduino controls the door.
You do not have to use these three switches if you do not want the Manual option.

The Open_Limit and Closed_Limit switches are normally closed.
i.e. when the door is not at the Open or Closed positions.
These physical switches open when the door is at the extreme, therefore current to the motor is removed.

Let us say the top wire (connected to the Arduino) goes HIGH from your code by doing a digitalWrite to the Arduino pin. Let us assume the switch is in the Auto position with 2-1 of S20 closed.
This HIGH turns on Transistor Q42 which energizes K2. Therefore K2 contact 1-3 closes.
Current from +12 goes through 1-3 of K2, through the normally closed contact Open_Limit through contact 11-13 of K4 through the motor then through contact 4-6 of K4 then to ground. Motor operates.
When the motor is fully open the Open_Limit switch opens and motor current is interrupted.
Your software would be written so after a time out period, the HIGH on the top wire that goes to the Arduino is removed.

Now let’s say 12 hours later the Arduino makes the middle wire go HIGH.
This HIGH will turn on Q43 therefore closing contacts 1-3 of K3.
Current now goes from +12V through 1-3 of K3 to pin one of K4. Therefore energizing K4.
The contacts of K4 transfer so now 4-8 and 13-9 are now closed.
Back to 1-3 of K3, the current that flows through this contact also goes through the normally closed Close_Limit switch through the now closed 4-8 contact of K4 through the motor (now in the reverse direction) through 13-9 of K4 then to ground.
The door closes until the Close_Limit switch opens to remove current from the motor.
Your software would be written so after a time out period, the HIGH on the middle wire that goes to the Arduino is removed.

I see. so instead of using the limit switches to send a signal to the arduino to stop the motor, I simply use the limit switches to cut current? I also really like the idea of having a manual mode. It'll take me a while to interpret this circuit, but I may be back with questions.

It is far safer to have the limit switches remove the motor current directly rather than software do it.
Also if you keep the Manual/Auto option the limit switches have to be used as shown.
Note: the controller can be turned off and the manual Open/Close push buttons can still function.

Use a state machine.

limit switches state diagram.png

Attached is a PDF version you can print.
Also source for the MOV.

MOV

Root_1.pdf (16.1 KB)

this is a huge help. Thank you.

LarryD:
It is far safer to have the limit switches remove the motor current directly rather than software do it.

Also have the limit switches inform the Arduino that they have been triggered.

...R

Redundancy is good, also provides feedback for control and display purposes.
The N.O. contact that usually is incorporated in a limit switch could be used with a voltage divider to an Arduino input.

my next question is, what is the best way to solar charge a lead acid battery? I have a solar panel that outputs about 25v in direct sunlight. Do I need to make a overcharge protection circuit?

Best to start a new thread.
You can start with this:

hammerstorm:
Do I need to make a overcharge protection circuit?

If the max solar panel current is very low compared to the battery capacity you may not need to. But it would be safer to have a charge controller.

...R

This is what I have for a trailer charging system, has worked for the last 8 years:

Main page:

Schematic:
http://www.solorb.com/elect/solarcirc/scc3/

do you have a copy of the sketch ? Thank you very much

I missed this thread when it was originally posted.

The design question got wildly out of hand

The neat solution to the problem is to use two only SPDT relays, each switching one motor terminal from ground to the battery supply and that in each case via the corresponding limit switch.

You determine how long it takes - at the longest - for the action to occur, add half as much again, and program the Arduino to activate the corresponding relay for that length of time and then release it to save power.

The limit switches prevent the motor driving beyond the correct point. There is no point in informing the Arduino that the action has or has not completed unless you have a procedure for dealing with its failure to do so.