微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将延迟编写的代码转换为millis()

如何解决将延迟编写的代码转换为millis()

我有一个在 Arduino 上编写的基本代码,但是,我需要将延迟改为 Millis。

无论我做什么我都无法让它工作,它总是卡在红灯处,永远不会变绿。

我正在发布原始延迟代码,因为我使用 Millis 编写的代码似乎毫无用处,并且可能会混淆我正在尝试做的事情。

const int redPin = 2;
const int yellowPin = 3;
const int greenPin = 4;

int redDuration = 10000;
int greenDuration = 5000;

void setup() {
  pinMode(redPin,OUTPUT);
  pinMode(yellowPin,OUTPUT);
  pinMode(greenPin,OUTPUT);
}

void loop() {
  setTrafficLight(1,0);
  delay(redDuration);
  setTrafficLight(1,1,0);
  delay(2000);
  setTrafficLight(0,1);
  delay(greenDuration);
  setTrafficLight(0,0);
  delay(2000);
}

void setTrafficLight(int redState,int yellowState,int greenState) {
  digitalWrite(redPin,redState);
  digitalWrite(yellowPin,yellowState);
  digitalWrite(greenPin,greenState);
}

解决方法

  1. 节省开始等待的时间。
  2. 如果当前时间和开始时间的差异成为等待时间,则进入下一个状态。
const int redPin = 2;
const int yellowPin = 3;
const int greenPin = 4;

int redDuration = 10000;
int greenDuration = 5000;

unsigned long startTime;
int status = 0; // using enum may be better

void setup() {
  pinMode(redPin,OUTPUT);
  pinMode(yellowPin,OUTPUT);
  pinMode(greenPin,OUTPUT);
  startTime = millis();
  status = 0;
}

void loop() {
  unsigned long currentTime = millis();
  switch (status) {
    case 0: // initial state
      setTrafficLight(1,0);
      status = 1;
      break;
    case 1: // waiting instead of delay(redDuration)
      if (currentTime - startTime >= redDuration) {
        setTrafficLight(1,1,0);
        startTime = currentTime;
        status = 2;
      }
      break;
    case 2: // waiting instead of first delay(2000)
      if (currentTime - startTime >= 2000) {
        setTrafficLight(0,1);
        startTime = currentTime;
        status = 3;
      }
      break;
    case 3: // waiting instead if delay(greenDuration)
      if (currentTime - startTime >= greenDuration) {
        setTrafficLight(0,0);
        startTime = currentTime;
        status = 4;
      }
      break;
    case 4: // waiting instead of second delay(2000)
      if (currentTime - startTime >= 2000) {
        startTime = currentTime;
        status = 0;
      }
      break;
    default: // for in-case safety
      status = 0;
      break;
  }
}

void setTrafficLight(int redState,int yellowState,int greenState) {
  digitalWrite(redPin,redState);
  digitalWrite(yellowPin,yellowState);
  digitalWrite(greenPin,greenState);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。