float switch loop with timing help needed with coding

Ok i have float1 being bottom float and float2 being a fail safe switch. the delay is to prevent clicking so the water level goes little above bottom switch which works. But i cant seem to get the 2nd timer (ontime2) working for when the my supply is empty (when pump runs longer than timer and float1 doesnt trip) maybe theres simpler loop just has me stumped. thanks

void topoff2()
 if (digitalRead(float1) == HIGH && digitalRead(float2) == LOW && toptoolong == FALSE){
          digitalWrite(Relay_4, RELAY_ON); 
          onTime = millis();	  	
          onTime2 = onTime;
 if((onTime > 0 && millis() - onTime > 30000) || digitalRead(float2) == HIGH ){     // runs  30sec after float rises
            digitalWrite(Relay_4, RELAY_OFF);
            onTime = 0;           
if (digitalRead(float1) == HIGH && (onTime2 > 0 && millis() - onTime2 > 40000)){    // if running longer than 40s topoff empty.
			digitalWrite(Relay_4, RELAY_OFF); 				
			toptoolong = TRUE;
 if (digitalRead(float1) == LOW && digitalRead(float2) == LOW){			//resets too long timer id float goes back up.
				toptoolong = FALSE;


One of the things that will hurt you here on the Forum is not reading Nick Gammon's first two notes on the proper way to post on this Forum. The regulars here forgive the first one or two posts, but 13 is too many for not having read the rules. Read those sticky notes, come back to this post (readers don't like double-posts), and resubmit your question with the proper format.

It would be better to post a complete sketch that demonstrates the problem, so we don't have to guess what else is going on and how your data is declared.

I think this logic would be simpler if you implemented it as a state machine. I suspect the state machine would only need two states (off and pumping) and you would check for float switch changes and timeouts appropriate to each state. I'm not sure what you intend to happen if the timers expire - perhaps you need a separate fault state where you turn everything off and wait for help.