2

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?

ocrdu
  • 1,795
  • 3
  • 12
  • 24
daffa faiz
  • 91
  • 6

1 Answers1

1

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.

This would make the relays toggle every 20 s (on every loop).

ocrdu
  • 1,795
  • 3
  • 12
  • 24