Intiate function as soon as button is pressed, not loop it over and over.

You should keep posting your latest code not updating that first post.

You have

           solenoid(IN);

That can never be reached because there is a break before and after it.