void lostline() { digitalWrite(LEDPIN, false); motors.setMotorPower(pwmleft, pwmright);…
- User
void lost_line() { digitalWrite(LED_PIN, false); motors.setMotorPower(pwm_left, pwm_right); if (e_line == 0.01) { motors.setMotorPower(30, -30); } else if (e_line == 0.011) { motors.setMotorPower(-30, 30); } else if (e_line == 0.012 && stopTime == 0) { stopTime = millis() + 150; } else if (e_line == 0.012 && millis() < stopTime) { motors.setMotorPower(pwm_left, pwm_right); } else if (e_line == 0.012 && millis() >= stopTime) { motors.setMotorPower(0, 0); } }如何让这个函数的计时功能可以多次使用
- Assistant
要让这个函数的计时功能可以多次使用,可以在函数开始的时候将 stopTime 设置为初始值0,然后根据需要在每次使用之前重新设置 stopTime 的值。这样,在每次调用函数时,计时功能就会重新开始。
下面是修改后的代码示例:
void lost_line() { digitalWrite(LED_PIN, false); motors.setMotorPower(pwm_left, pwm_right); static unsigned long stopTime = 0; // 添加静态变量,记录计时停止的时间 if (e_line == 0.01) { motors.setMotorPower(30, -30); } else if (e_line == 0.011) { motors.setMotorPower(-30, 30); } else if (e_line == 0.012 && stopTime == 0) { stopTime = millis() + 150; // 重新设置计时停止的时间 } else if (e_line == 0.012 && millis() < stopTime) { motors.setMotorPower(pwm_left, pwm_right); } else if (e_line == 0.012 && millis() >= stopTime) { motors.setMotorPower(0, 0); stopTime = 0; // 计时结束后,重置 stopTime 的值为0,以便下次使用 } }这样,每次调用
lost_line()函数时,计时功能都会重新启动,并在设定的时间后停止。 - User
重置stoptime后如何防止再次进入循环
- Assistant
如果你希望在重新设置
stopTime后防止再次进入循环,可以使用一个额外的变量来控制进入循环的条件。下面是修改后的代码示例:
void lost_line() { digitalWrite(LED_PIN, false); motors.setMotorPower(pwm_left, pwm_right); static unsigned long stopTime = 0; // 添加静态变量,记录计时停止的时间 static bool isLooping = false; // 添加静态变量,记录是否正在循环中 if (e_line == 0.01) { motors.setMotorPower(30, -30); isLooping = false; // 如果进入了其他条件的循环,将isLooping标记为false } else if (e_line == 0.011) { motors.setMotorPower(-30, 30); isLooping = false; // 如果进入了其他条件的循环,将isLooping标记为false } else if (e_line == 0.012 && stopTime == 0) { stopTime = millis() + 150; // 重新设置计时停止的时间 isLooping = true; // 进入循环,将isLooping标记为true } else if (e_line == 0.012 && millis() < stopTime && isLooping) { motors.setMotorPower(pwm_left, pwm_right); } else if (e_line == 0.012 && millis() >= stopTime) { motors.setMotorPower(0, 0); stopTime = 0; // 计时结束后,重置 stopTime 的值为0,以便下次使用 isLooping = false; // 循环结束,将isLooping标记为false } }这样,如果在重置
stopTime后再次进入循环时,只有isLooping标记为true时才会进入循环。否则,即使条件满足,也不会再次进入循环。 - User
但是循环结束后会进入 else if (e_line == 0.012 && stopTime == 0)的情况,如何避免这一情况发生
- Assistant
要避免在循环结束后再次进入
else if (e_line == 0.012 && stopTime == 0)的情况,可以添加一个标志变量来判断循环是否已经结束。下面是修改后的代码示例:
void lost_line() { digitalWrite(LED_PIN, false); motors.setMotorPower(pwm_left, pwm_right); static unsigned long stopTime = 0; // 添加静态变量,记录计时停止的时间 static bool isLooping = false; // 添加静态变量,记录是否正在循环中 static bool isLoopEnd = false; // 添加静态变量,记录循环是否已经结束 if (e_line == 0.01) { motors.setMotorPower(30, -30); isLooping = false; // 如果进入了其他条件的循环,将isLooping标记为false isLoopEnd = false; // 如果进入了其他条件的循环,将isLoopEnd标记为false } else if (e_line == 0.011) { motors.setMotorPower(-30, 30); isLooping = false; // 如果进入了其他条件的循环,将isLooping标记为false isLoopEnd = false; // 如果进入了其他条件的循环,将isLoopEnd标记为false } else if (e_line == 0.012 && stopTime == 0 && !isLoopEnd) { stopTime = millis() + 150; // 重新设置计时停止的时间 isLooping = true; // 进入循环,将isLooping标记为true } else if (e_line == 0.012 && millis() < stopTime && isLooping) { motors.setMotorPower(pwm_left, pwm_right); } else if (e_line == 0.012 && millis() >= stopTime) { motors.setMotorPower(0, 0); stopTime = 0; // 计时结束后,重置 stopTime 的值为0,以便下次使用 isLooping = false; // 循环结束,将isLooping标记为false isLoopEnd = true; // 循环已经结束,将isLoopEnd标记为true } }