Oh, okay. I don't see a relay detailed in the schematic. It's quite confusing. Are you sure the 24v is running through the contact side of the relay? Why would the relay contact be labelled com4? In your sketch you're using pin 4 for the relay. Is it a relay shield? Which one it is would be helpful.
So the relay turns on fine but won't turn off? Try some code without the switch. In loop just set the relay pin high then delay then set it low then delay again. That should have it switching on and off regularly. If that doesn't work then your problem isn't in your sketch.