gpt4 book ai didi

c - Arduino 不等待每个模式(模式)结束我怎样才能重新启动循环?

转载 作者:太空宇宙 更新时间:2023-11-04 04:45:45 25 4
gpt4 key购买 nike

我目前正在使用 Arduino Uno 进行交通灯项目。当我按下外部开关时,我应该在不同模式之间切换。有 4 种模式,模式 0:所有 LED 熄灭,模式 1:红色 LED 闪烁 1 秒亮 1 秒灭,模式 2:黄色 LED 闪烁 1 秒亮 1 秒灭,但是在模式 3 中:我有电位器相关的延迟。 (最多 10 秒)

我的问题是,如何在按下开关并更改模式后立即停止模式,而无需等待当前模式完成。

// Change the speed with delay using potentiometer,
#include <EEPROM.h>

short rPin = 4; // red led connected to D4
short yPin = 5; // yellow led connected to D5
short gPin = 6; // green led connected to D6
short switchPin = 3; // where the switch is connected

short potPin = 2; // input pin for the PM
short val = 0; // variable to store the value coming from the PM

short timeOut = 300; // switch debounce noise handling,
volatile short last_change_time = 0;

static short dTime = 0; // Static Variable, that holds the last change, effects
// every function.
static short d = 0; // current d value.

static short mode = 0;

// EEPROM current address
int addr = 0;

void pressed() {
int difference = millis()-last_change_time;
if(difference > timeOut || last_change_time == 0)
{
if (mode >= 3){
mode = 0;
}
else {
mode = mode + 1;
}
}
else
{
//Serial.println("NO");
}
last_change_time = millis();
}

void setup()
{
pinMode(rPin, OUTPUT);
pinMode(yPin, OUTPUT);
pinMode(gPin, OUTPUT);
pinMode(switchPin, INPUT);
Serial.begin(9600);
attachInterrupt(1,pressed,FALLING); // digital pin 2 // pressed
}

void loop()
{
if (mode == 3) mode3();
if (mode == 2) mode2();
if (mode == 1) mode1();
if (mode == 0) mode0();
val = analogRead(potPin);
if (val < 170) {
d = 1;
}
else if ( (val >= 170) && (val < 340))
{
d = 2;
}
else if ((val >= 340)&&(val < 510))
{
d = 3;
}
else if ((val >= 510)&&(val < 680))
{
d = 4;
}
else if ((val >= 680) && (val < 850))
{
d = 5;
}
else if ((val >= 850) && (val < 1023))
{
d = 6;
}
}


void serialEvent() {
while (Serial.available()) {
int value = Serial.parseInt();
if (value == 1){
Serial.print("d: ");
Serial.print(d);
Serial.print(" mode: ");
Serial.print(mode);
Serial.print("\n");
}
}
}

void mode0() {
while(mode == 0){
digitalWrite(rPin, LOW);
digitalWrite(yPin, LOW);
digitalWrite(gPin, LOW);
}
}

void mode1() {
while(mode == 1){
digitalWrite(yPin, LOW);
digitalWrite(gPin, LOW);
digitalWrite(rPin, HIGH);
delay(1000);
digitalWrite(rPin, LOW);
delay(1000);
}
}

void mode2() {
while(mode == 2){
digitalWrite(rPin, LOW);
digitalWrite(gPin, LOW);
digitalWrite(yPin, HIGH);
delay(1000);
digitalWrite(yPin, LOW);
delay(1000);
}
}


void mode3(){
interrupts();
while(mode == 3){
digitalWrite(rPin, HIGH);
delay(9*d*1000);
digitalWrite(yPin, HIGH);
delay(d*1000);
digitalWrite(rPin, LOW);
digitalWrite(yPin, LOW);
digitalWrite(gPin, HIGH);
delay(10*d*1000);
digitalWrite(gPin, LOW);
delay((d)*1000);
digitalWrite(gPin, HIGH);
delay(d*1000);
digitalWrite(gPin, LOW);
delay(d*1000);
digitalWrite(yPin, HIGH);
delay(d*1000);
digitalWrite(yPin, LOW);
}
}

最佳答案

想一想:一旦进入“模式”循环之一,您就再也不会检查开关(电位器)的状态。合理? 有限状态机的建议很好,但为了保持简单,只需将检查开关和设置模式的代码拆分成它自己的函数,然后在每个模式循环中调用它。

void mode0() {
while(mode == 0){
digitalWrite(rPin, LOW);
digitalWrite(yPin, LOW);
digitalWrite(gPin, LOW);

checkSwitch(); // this will potentially change the 'mode' so the while loop with exit if the mode has changed.
}
}

更新:延迟 hack

不是很推荐,但我认为使用delay 方法并且不锁定所有交互的一种方法是将延迟降至最低并检查 for 循环中的输入 - 类似于:

void uglyDelayHack(int delayAmount, int lastMode){

for (int i = 0; i < delayAmount; i++){

delay(1); // 1 millisecond delay

// call to a function that checks for input and changes mode
checkForInput();

// get out of the fake delay if mode has changed
// assumes "mode" is a global variable
if (lastMode != mode) return;
}
}

关于c - Arduino 不等待每个模式(模式)结束我怎样才能重新启动循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21070078/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com