0

I want to provide a delay in the following iteration reading so that the sensor can read the condition after the pump starts.

void loop() {
  //-------------Fungsi Read sensor----------- 
  // sensorAll();

  hasilec = (ppmcensor - inputec);
  ph = (phcensor - inputph);
  
  //  hasilec = random(-200, 0) / 100.0;
  //  ph = random(-200, 200) / 100.0;

  //  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(" ");

  //  delay (500000);
  
  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
  
  Serial.println("Result= ");
  Serial.println("");
  Serial.print(" Pompa1 pHDOWN: ");
  Serial.println(pompa01);
  Serial.print(" pompa2 phUP  : ");
  Serial.println(pompa02);
  Serial.print(" pompa3 ABMIX : ");
  Serial.println(pompa03);
  Serial.println("");
  Serial.println("");
  Serial.println("");
  
  delay (2000);
  
  long int pompa1 = pompa01 * 1000;
  long int pompa2 = pompa02 * 1000;
  long int pompa3 = pompa03 * 1000;
  
  pump_periods[0] = pompa1; //PHdown
  pump_periods[1] = pompa2; //PHup
  pump_periods[2] = pompa3; //ABMIX
  
  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;
    }    
  }   
}

If I put the delay after millis iteration it causes the pump to only turn on based on the delay, not the timer millis. What should I do?

7
  • Sorry, I don't understand your question. What exactly keeps looping? The loop() function always loops, that is what is is meant for. Or what do you mean? And what does "not making space for my environment getting stabilize" mean?
    – chrisl
    Dec 16 '20 at 23:52
  • why are you using fuzzy logic?
    – jsotola
    Dec 17 '20 at 1:35
  • I mean let the wave pump mix all Ph and AB mix, and fet stabilize on set point, because im trying using fuzzy for it
    – daffa faiz
    Dec 17 '20 at 3:10
  • 1
    sorry, i have no idea what you said
    – jsotola
    Dec 17 '20 at 4:26
  • 1
    Please edit you question and provide some context. If would help if you translate your variables and texts, too. Dec 17 '20 at 6:52
0

If all the pumps have to work for pump_periods[i] time on every loop, and you will have a long delay() anyway, you could replace the for loop with:

for (int i = 0; i < jml_pump; i++) { // For every pump:
  digitalWrite(pump_pins[i], 1);     // Start pump i
  delay(pump_periods[i]);            // Wait for pump_periods[i] ms
  digitalWrite(pump_pins[i], 0);     // Stop pump i
}
delay(120000);                       // Wait 2 minutes until mixed

This will block the loop, but you will have a long delay anyway. The pumps will work sequentially, and not at the same time.

Is that what you want?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.