1 Button, 2 Reeds and a dream

I'm working on a little project for daughter, its basically a automatic window for her play house. The window is near completion, everything is complete except the "code"...
Basically I want the window to be controlled by a button, then have the the two reed sensors stop the window while going up or down.
I'm useing a 12v dc worm geared motor(8rpm) with a pulley to have the window go up or down, I followed the schematic's on the link below, to build the circuit so the motor's directions and on/off can be controlled.

Help with the code would be much appreciated.

12v dc worm geared motor

What happens if your daughter puts her hand in the path of the window?

Worm drives have produce a huge amount of force, have you put your hand the path of the and tested what happens to you hand,if the window crushes it against the end stop?

Aside from that, the code would be relatively simple, but I'd recommend you write it as a Finite State Machine

define states for the system (window)

e.g.

WINDOW_POSITION_UNKNOWN
WINDOW_AT_TOP
WINDOW_AT_BOTTOM
WINDOW_START_MOTION_DOWNWARDS
WINDOW_MOVING_DOWN
WINDOW_START_MOTION_UPS
WINDOW_MOVING_UP

Then work out under what conditions you system changes between these states, i.e by button press

Note, you may not need all these states, and you may need some additional states, its just a general idea of one way to approach the software design

And its a good idea to draw a flow chart before you begin programming

To try to prevent any injury Im modifying the window frame with a removable head and side jamb's, so If something does get stuck ill just remove the window from the frame.. I'm placing the bottom reed switch 4in above the window sill so no fingers/hands will get stuck(the window is real high up, about 6-7ft). The window is going to be connected to the motor with a pulley so if something does get stuck it won't have a lot if squeezing force... I only chose the worm geared motor because it doesn't use power to keep the window open
I feel like that should cover the safety aspect of this project... The code/program is where I need the most help.

To try to prevent any injury Im modifying the window frame with a removable head and side jamb's

No worries.. It just sounded like you were building something with some safety issues :wink:

just to get you started,.

This runs the motor for two seconds in one direction and then two seconds in the other direction.

.
#define MOTOR 4
#define DIR 5
void setup() 
{
  pinMode(MOTOR,OUTPUT);
  pinMode(DIR,OUTPUT);
  
  digitalWrite(MOTOR,HIGH);  //motor on
  delay(2000);
  digitalWrite(MOTOR,LOW); //motor off
  delay(10);  //wait for spikes to dissipate
  digitalWrite(DIR,HIGH); //reverse direction
  delay(10);  //wait for spikes to dissipate
  digitalWrite(MOTOR,HIGH);  //motor on
  delay(2000);
  digitalWrite(MOTOR,LOW); //motor off
  delay(10);  //wait for spikes to dissipate
  digitalWrite(DIR,LOW); //relay off
}

void loop() 
{}

this should allow you to make sure it goes up and down.

once you know it works, then the details of how and why it should move come in to play.

delay(10);  //wait for spikes to dissipate

I'd suggest waiting more than 10 milliseconds before reversing the direction, for the mechanics to stop moving.

At least 250ms probably longer depending on the motor

That's the code I was originally using... It runs off a timer. or was that code just to make sure the circuit is functioning correctly? If so I think the circuit is functioning correctly

The OP has another 4-page Thread on this subject and, for example, @rogerClark's concern about getting injured was already dealt with there.

Double posting just wastes people's time.

At the very least @ProjectNoWork might have mentioned the earlier Thread and provided a link to it.

...R

Thanks Robin2

I didn't see the other posting.

Sorry. Should my question been asked in the "Programming Questions" not "Project Guidance"... The OP(Original post) was to get started on how the layout of the window was going to be, now this thread is help with the code.
I was going to mention it but I didn't know how to link just the Threads that were being asked from the OP.
so please let's just-stick to the code,

There were code suggestions in the other Thread and by starting a new Thread someone is just going to waste time making the same suggestions again.

The problem is not where you have this Thread just that it would have allowed everyone to see all of the project if you had just continued in the other Thread.

Were any of the code suggestions in the other Thread useful ether because they helped or because they illustrate something you definitely don't want. If so, which ones?

...R

It is because I was attempting to help by writing the code, and I was having a hard time finding the time to continue. I suggested he start a new thread, so blame me.

There now seems to be a software discussion on your main Thread.

I suggest you click Report to Moderator on this Post (doesn't work from one of your own posts) and ask the moderator to merge this with your other Thread.

...R

I wouldn't merge it, as it covers ground already covered and will likely just confuse things.