I want to make 3 pins working simultaneously but with different duration.
This is the code that I've made:
unsigned long presentMillis = millis();
int a = 5000, b = 10000, c = 15000, flow = 100;
int pump1 = D0, pump2 = D1, pump3 = D2;
void setup(){
pinMode(pump1, OUTPUT);
pinMode(pump2, OUTPUT);
pinMode(pump3, OUTPUT);
}
void loop(){
digitalWrite(pump1, HIGH);
while(millis() - presentMillis <= (a * 60000 / flow)){
}
digitalWrite(pump2, HIGH);
while(millis() - presentMillis <= (b * 60000 / flow)){
}
digitalWrite(pump3, HIGH);
while(millis() - presentMillis <= (c * 60000 / flow)){
}
digitalWrite(pump1, LOW);
digitalWrite(pump2, LOW);
digitalWrite(pump3, LOW);
}
The code won't work like the way I wanted. How to fix this?
Those while loops block the code until their exit condition is met. Let the loop() function run freely and use if instead of while so that the code execution is not blocked
See Using millis() for timing. A beginners guide , Several things at the same time and the BlinkWithoutDelay example in the IDE
So by using while loop, the next while condition won't be able to work if the previous while haven't finished?
Do you have classic ESP32 Microcontroller Dev Board which offers interesting environment based on FreeRTOS Library for concurrent execution of mutiple tasks?
Sorry I don't really understood of what you said. But I guess I don't have
I was talking if you owned the following ESP32 MCU Dev Board.
Oh no, I'm using Arduino Uno or Wemos D1 R2 with Esp8266 built in
1 Like
system
Closed
April 25, 2024, 11:32am
11
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.