Programming SSR & Momentary Push Button for CNC

I've been looking on the forums for examples of sketches using a momentary button to turn on a solid state relay and leave it activated until the button is pressed again but I haven't found any info. Here's my project. I have a momentary push button connected to a digital pin on the arduino. I also have a SSR connected to another digital pin. The relay will be powering a 24v double acting air solenoid. What I want to happen is when I push the button I want it to power the relay which will turn on the solenoid and keep it powered until the button is pushed again. I also was wondering if it's possible to have the program check another pin when the button is pressed and if the pin has no voltage input to it then power the relay but if the pin has 5v then don't power the relay. I hope this makes sense. I'm trying to build this for my CNC machine's Power Drawbar. So basically It will work like this: When I push the momentary button mounted on my machine it will check and make sure there's no voltage on the one pin which is my VFD's way of saying the spindle is off. If the spindles off then the relay will power the air solenoid which will release the tool. Once I install the new tool I push the button which removes power from the relay/air solenoid. The most important part of the programming right now is just powering the relay with the button. If anybody could help me write a sketch I would appreciate it. Thanks

boolean detected_push = false ;
boolean state = false ;
unsigned long time_pushed = 0L ;
boolean previous_button_state = digitalRead (button) ;

void loop ()
  if (digitalRead (button) == LOW && previous_button_state == HIGH)  // button pressed
    if (!detected_push && millis () - time_pushed > 20)  // debounce check
      detected_push = true ;   // prevent more toggling till button released again
      state = !state ;
      digitalWrite (relay, state) ;
      time_pushed = millis () ;
  if (detected_push && digitalRead (button) == HIGH)
    detected_push = false ;

Thanks for the quick response Mark. I'm still new to all this so could you break the sketch down for me? Is this sketch looking for the zero volts on the one pin? If so could you write me a sketch with and without that for right now? Also all the other sketches that I've seen have the button pin defined. Could you write the code so my button pin is digital pin 5 and my relay pin is 8. Also should an analog pin be used to detect the voltage? Thanks so much

I have a quick question. In the code that MarkT posted for me, wherever their is (button) do I replace that with the digital pin number like this?

boolean previous_button_state = digitalRead (5) ;

Also where it says (relay, state) do I assume I enter the relay pin but what should I replace state with high or low?


do I replace that with the digital pin number


Also where it says (relay, state) do I assume I enter the relay pin but what should I replace state with high or low?

Yes, or you could try Nebraska or Kansas. They are states, too.