# Relays just switch ON and OFF on delay(), not on millis()

I use fuzzy logic for the running time of relays, but the relays just switch ON and OFF at the interval of the `delay()` I put at the end of loop.

This is my code:

``````void loop () {
ph = -180/100.0;
hasilec = -1.2;
unsigned long now = millis();

Serial.print("pH    ");
Serial.print(ph);
Serial.print("  ");
Serial.print("EC ");
Serial.print(hasilec);
Serial.println("");
Serial.print(ph);
Serial.print(" ");
Serial.print(hasilec);
Serial.print(" ");
Serial.println(" ");

fuzzy->setInput(2, hasilec);
fuzzy->setInput(1, ph);
fuzzy->fuzzify();

float pompa01 = fuzzy->defuzzify(1); //pHDOWN
float pompa02 = fuzzy->defuzzify(2); //pHUP
float pompa03 = fuzzy->defuzzify(3); //ABMIX

long int pompa1 = pompa01 * 1000;
long int pompa2 = pompa02 * 1000;
long int pompa3 = pompa03 * 1000;

Serial.println("Result= ");
Serial.println("");
Serial.print(" Pompa1 pHDOWN: ");
Serial.println(pompa1);
Serial.print(" pompa2 phUP  : ");
Serial.println(pompa2);
Serial.print(" pompa3 ABMIX : ");
Serial.println(pompa3);
Serial.println("");
Serial.println("");
Serial.println("");

pump_periods[0] = pompa1;
pump_periods[1] = pompa2;
pump_periods[2] = pompa3;

for (int i = 0; i < jml_pump; i++) {
if (now - pump_last_actuations[i] >= pump_periods[i]) {
pump_states[i] = (pump_states[i] == LOW) ? HIGH : LOW;
digitalWrite(pump_pins[i], pump_states[i]);
pump_last_actuations[i] = now;
}
}
delay (20000);
}

``````

The relay just switch ON and OFF on this delay `delay (20000);`.

What should I do?

• Check the values of pompa1, pompa2, and pompa3 to see if they are always smaller than 20000. What is the delay for, anyway? It could well be messing up your timing of the toggling of the pump_states by delaying longer than the values in pompa1, pompa2, and pompa3. Dec 14, 2020 at 18:42
• i got it i just replace my delay after fuzzy, its work, let me debugging more for perfection my project thx Dec 14, 2020 at 19:08
• I will turn the comment into an answer; please accept it (if you accept it) so the question doesn't stay open. Dec 14, 2020 at 19:14

Check the values of `pompa1`, `pompa2`, and `pompa3` to see if they are always smaller than 20000 (I suspect they are).
The `delay(20000)` (why is it there anyway?) could well be messing up your timing of the toggling of the `pump_states` by always delaying longer than the values in `pompa1`, `pompa2`, and `pompa3`.