BEGINEER

hi guys i am begineer to arduino i want code for depends on following condition

once a contact(switch) is made by weight(mechanical) , motor starts to run after releasing the switch motor runs upto 10 seconds and then stops this loop continues

please help me

Edited:

If button pressed, loop here until released Turn on motor Delay 10 sec Turn off motor

Loop back to start

Make sure you do not run the motor directly from Arduino pin. Use a transistor or logic level MOSFET and put a reverse connected diode across the motor coil.

The button needs to be connected between a digital input and Gnd. It also needs a 10k resistor from the input to 5v.

Weedpharma

Well, it's not 100% clear but I read it that he wanted the 10sec delay after the button was released... Also there is the case of the button being pushed during the 10sec wait that is undefined (so far)

I think a state machine approach might be the eventual winner ?

OP might do well to realise the helpful distinction between say 'systemState' - i.e. digitalRead(button) and motor on/off

Also learn to work with mills() ?

1:1: I think a state machine approach might be the eventual winner ?

Always....

I'd agree, but I wonder how often after going to the wikipedia article on FSM's people (kids esp.) just switch off and think electronics is too complex...

There has been some nice examples made here in the forums I guess, gotta keep it simple - what's the most basic yet informative example? A momentary switch that toggles an LED ?

1:1: A momentary switch that toggles an LED ?

I was thinking exactly that earlier today: a simplified version of this one which counts the presses.

I've just written a simple sketch that toggles the program between two modes which I've called "automatic" and "manual" on the repeated push of a button. In each mode, there's an empty function (well, it has a serial print to prove it got there) where the "real" code for the respective modes would be added.

It also holds the sketch at the bottom of setup() with a while(), which await the press of a button.

Idea is that setup() could run some kind of calibration and then await the operator's say-so to move into loop() where the process is run in either automatic or manual mode, with the operator able to switch between those modes.

I'll post it soon in a new thread... just need to comment it 8) and do a state diagram. I'll explain it in the thread where I post it.