Hey Guys,
for days im struggling with a quity simple if-condition in combination with the millis() function.
I would really appreciate it, if anybody can take a quick view on the code and help me out with solving this issue.
My goal is a code to control a pump, which is switched on by an transistor if an certain pressure is going under the target pressure. If the target pressure is reached, the pump should turn off.
For this part, the code is working without problems.
Now i need to ad an time-control for the situation, that the pump is switched on. If there are some leaks in the pump cycle, the pump probably would run for years - so we need to ad an time-control, which switches off the pump, after it runs more then 5 seconds.
With the current code, the red led ist blinking short and fast. the pump is turning on, if were under target pressure, but it does not turn off the pump after 5 seconds. So the failsafe-function does not work.
Does anybody can help out at this point?
Many thanks!! ![]()
best regards
Gustav
current code:
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); //initializes serial communication at set baud rate bits per second
pinMode(2, OUTPUT); // GREEN LED
pinMode(4, OUTPUT); // BLUE LED
pinMode(7, OUTPUT); // RED LED
pinMode(8,OUTPUT); // PUMP
}
void loop() {
// put your main code here, to run repeatedly:
long pumpenlaufzeit = -1; // -1 als Code für Pumpe ist nicht aktiv
int sensorVal = analogRead(A0); // read the value from sensor
Serial.print("sensor value:");
Serial.println(sensorVal); // print sensor value to screen
float voltage = (sensorVal*5.0)/1024.0;
Serial.print("Volts: ");
Serial.print(voltage);
// converting pressure
float pressure_pascal = (3.0*((float)voltage-0.47))*1000000.0;
float pressure_bar = pressure_pascal/10e5;
Serial.print("Pressure = ");
Serial.print(pressure_bar);
Serial.println("bars");
Serial.print("Pressure = ");
if ((pumpenlaufzeit = -1) && (pressure_bar < 0.03)){ //Pumpe ist im agestellten zustand und solldruck wird unterschritte -> pumpe geht an
pumpenlaufzeit = millis();
digitalWrite(2, HIGH); // turn green led on
digitalWrite(4, HIGH); // turn blue led on
digitalWrite(7, LOW); // turn red led off
digitalWrite(8, HIGH);// turn pump on
// triff einmalig zu, wenn pumpe aus UND der Druck NICHT im soll ist
}
if ((pumpenlaufzeit + 5000) < millis()){ // pumpe läuft länger als 5 sekunden, was auf undichtigkeit hindeutet. -> pumpe abstellen, fehler (rot) zeigen
digitalWrite(2, LOW);
digitalWrite(4, LOW);
digitalWrite(7, HIGH); //rote LED on
digitalWrite(8, LOW);
}
if ((pressure_bar < 0.03) == false) { // Standardfall dass der Druck wieder im Soll ist, pumpe abstellen
pumpenlaufzeit = -1;
digitalWrite (4,LOW); // blue LED off
digitalWrite (8,LOW); // pump off
digitalWrite(7,LOW); //red off
digitalWrite(2,HIGH); // green led on
}}