Code for using micro switch on Arduino

Hey all,
I’m using Arduino adk for a project since a few months but I have some problems with my code.
The concept is simple: when I press on a main button, a DC-motor connected to an H-bridge starts to run for 5 seconds. Then the motor begins to move in the opposite direction (possible with H-bridge) for an indefinite amount of time till it presses by its own a micro switch. At that moment the engine has to stop and the micro switch stays pressed. When I press on the main button again the whole cyclus recommence; the engine has to move in the opposite direction again for 5 seconds and go back till it presses on the micro switch…
Of course I know I can set up an amount of time and no micro switch is needed, but I really need one for my project.

At this moment I can let my engine turn 5 seconds, and let him turn back. But I can’t figure out what code I have to insert when the engine reaches the micro switch; it has to stop immediately and it has to be ‘ready to use’ when I give a signal from the main button.

I hope someone can help me for the code.
Thank you a lot!

connect one one of microsw. to GND.. the other to pin 2 or 3 (UNO)
attach interrupt 0 (pin2) or 1 (pin3).
Now u can stop motor instantly when interupt is triggered.

knut_ny:
connect one one of microsw. to GND.. the other to pin 2 or 3 (UNO)
attach interrupt 0 (pin2) or 1 (pin3).
Now u can stop motor instantly when interupt is triggered.

If wired like above be sure to enable the internal pull-up resistor for the pin(s) you use

I wouldn't use interrupts at all for something this simple. Just connect the button and limit switch to digital inputs and write a sketch that waits for the button to be pressed, runs the motor forwards for five seconds, reverses the motor, waits until the limit switch is actuated, stops the motor.