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?