There are only two places in the code where the valve is turned off with digitalWrite(4,LOW).
First Instance
if ( solstate == true && doneitonce == true && timerRunning == true && millis() - initialTime >= openTime)
{
Serial.print("\tenteredloop\t");
Serial.print(solstate);
digitalWrite(4, LOW);
solstate = false;
Serial.print("OFF ");
Serial.println(solstate);
timerRunning = false;
}
Second Instance
if ( solstate == true && millis() - startTime >= interval)
{
digitalWrite(4, LOW);
solstate = false;
peakcounter = 0;
}
The second instance is activated before the first. The second instance is nested within this conditional which is wrongly written. Try fixing this and see if the valve doesn't stay open for the 5 seconds.
//if (doneitonce = true && solstate == false)
if (doneitonce == true && solstate == false)
If that doesn't fix it, then try change the condition on the valve shut off to accommodate the initial timer
if (timerRunning== false && solstate == true && millis() - startTime >= interval)
{
digitalWrite(4, LOW);
solstate = false;
peakcounter = 0;
}
Some of your cocditions are over specified and make the logic confusing. For example this could be simplified
//if ( solstate == true && doneitonce == true && timerRunning == true && millis() - initialTime >= openTime)
if (timerRunning == true && millis() - initialTime >= openTime)