学习Arduino多任务基础——如何在Arduino代码中使用millis()函数

原文https://www.yiboard.com/thread-1178-1-1.html,第一次写博客,如果有任何关于转载的问题,请联系我

用Arduino实现一定程度上的多任务系统,实现同时操控三组led灯的开关情况,在我的测试中,黄灯以200ms为间隔闪烁,蓝灯以1s为间隔闪烁,而红灯通过按键控制开关,并且相互发生的情况并不受干扰。


下面是电路连接


下面是实现代码

/* Arduino Multitasking

    Author : CircuitDigest (circuitdigest.com)

*/

int led1 =  6;      // led1 connected at pin 6

int led2 =  7;      // led1 connected at pin 7

int toggleLed = 5;    // push button controlled led connected at pin 5

int pushButton = 2;    // push butoon connected at pin 2 which is also interrupt pin

int ledState1 = LOW;  // to determine the states of led1 and led2

int ledState2 = LOW;

unsigned long previousMillis1 = 0;  //store last time LED1 was blinked

const long period1 = 1000;        // period at which led1 blinks in ms

unsigned long previousMillis2 = 0;  //store last time LED2 was blinked

const long period2 = 200;            // period at which led1 blinks in ms

int debouncePeriod = 20;            // debounce delay of 20ms

int debounceMillis = 0;            // similar to previousMillis

bool buttonPushed = false;    // interrupt routine button status

int ledChange = LOW;      // to track the led status last

int lastState = HIGH;    // to track last button state

void setup() {

  pinMode(led1, OUTPUT);              // define pins as input or output

  pinMode(led2, OUTPUT);

  pinMode(toggleLed, OUTPUT);

  pinMode(pushButton, INPUT);

  attachInterrupt(digitalPinToInterrupt(pushButton), pushButton_ISR, CHANGE);  // use interrupt pin2

}

void pushButton_ISR()

{

  buttonPushed = true;  // ISR should be as short as possible

}

void loop() {

//控制蓝灯闪烁

  unsigned long currentMillis = millis(); // store the current time

  if (currentMillis - previousMillis1 >= period1) {    // check if 1000ms passed

    previousMillis1 = currentMillis;  // save the last time you blinked the LED

    if (ledState1 == LOW) {  // if the LED is off turn it on and vice-versa

      ledState1 = HIGH;  //change led state for next iteration

    } else {

      ledState1 = LOW;

    }

    digitalWrite(led1, ledState1);    //set LED with ledState to blink again

  }

//控制黄灯闪烁

  if (currentMillis - previousMillis2 >= period2) { // check if 200ms passed

    previousMillis2 = currentMillis;  // save the last time you blinked the LED

    if (ledState2 == LOW) { // if the LED is off turn it on and vice-versa

      ledState2 = HIGH;

    } else {

      ledState2 = LOW;

    }

    digitalWrite(led2, ledState2);//set LED with ledState to blink again

  }

  if (buttonPushed = true)    // check if ISR is called

  {

    if ((currentMillis - debounceMillis) > debouncePeriod && buttonPushed)  // generate 20ms debounce delay to avoid multiple presses

    {

      debounceMillis = currentMillis;      // save the last debounce delay time

//开关控制红灯

      if (digitalRead(pushButton) == LOW && lastState == HIGH)    // change the led after push button is pressed

      {

        ledChange = ! ledChange;

        digitalWrite(toggleLed, ledChange);   

        lastState = LOW;

      }

      else if (digitalRead(pushButton) == HIGH && lastState == LOW)   

      {

        lastState = HIGH;

      }

      buttonPushed = false;

    }

  }

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容