I'm having issues trying to convert my code to non-blocking. I was able to do so in some parts, but the rest aren't the same.
When I try to replace delay with an if statement like the spinning and sensors debounce it doesn't work at the right time, because the variable I subtract from cur_ms is not updated.
What I'm trying to do is to always switch_dir() and update_code(), and when second_btn.released() I want the other two functions to keep running but also to run a sequence of actions (those with the delay) without blocking.
I would love to get your help.
Thanks in advance :)
void loop()
{
uint32_t cur_ms = millis();
if (cur_ms - spin_ms >= SPINNING_MS) {
switch_dir();
spin_ms = cur_ms;
}
if (cur_ms - read_ms >= SENSORS_DEBOUNCE_MS) {
update_code(get_sensors_state());
read_ms = cur_ms;
}
if (second_btn.released()) {
ball_servo.write(BALL_SERVO_MAX); // drop the ball
digitalWrite(BLOWER_PIN, LOW); // blower on
delay(BALL_DOOR_MS);
ball_servo.write(BALL_SERVO_MIN); // close ball door
delay(BLOWER_MS);
digitalWrite(BLOWER_PIN, HIGH); // blower off
}
}
if (second_btn.released()) {
should only run the first two commands, store ball_door_ms and set a state variable, for examplestate = 2
........ new if statementif (state == 2 and cur_ms - ball_door_ms >= BALL_DOOR_MS) {
would close ball door, store blower-ms and setstate = 3
.... an so on