Model Railway Level Crossing

On the arduino, your setup() function runs once and your loop() function runs continuously, for ever, until you turn of the power or upload a new sketch or reset the arduino.

Suppose you want your level crossing to close for four seconds.
You could close the gates, ring the bell, delay for four seconds, and then open the gates.
Actually, the above sentence is what YOU SHOULD NOT DO.

What you should do is this:

When the button or reed switch or whatever gets triggered, you close the gate, ring the bell,
and remember somehow what the time will be, four seconds from now ( there are several ways
of doing this ). Then your loop function runs, every few milliseconds, and one of the things
it does each cycle, is check whether the four seconds is finished yet. When the four seconds
is finished, it opens the gate and stops ringing the bell. It can also check other things and do
other things while this is happening.

Look at the "blink without delay" example sketch for the recommended way of doing this,
there are other ways of doing this also.