暂停无延迟()arduino

Pause without Delay() arduino

我正在使用 arduino uno、逻辑电平转换器、adafruit 双色 LED 矩阵、Raspberry pi 和一个按钮,但遇到了一些问题。我的目标是,当在 pi 上按下按钮时,它会向 arduino Uno 发送信号,这将改变 LED 背包上的动画。问题是当循环重新开始时必须按住按钮,如果您在任何其他时间单击它都不会起作用,我认为这是因为我正在使用 delay() 暂停一切。有什么方法可以不使用 delay() 来暂停吗?我听说过使用 Millis() 但我是 Arduino 的新手,我完全不确定我将如何实现它。提前致谢。

#include <Adafruit_LEDBackpack.h>
#include <Adafruit_GFX.h>
#include <Wire.h>
#include "blinky.h"       // Animation for blinking and just chilling
#include "looking.h"    // This one looks forward when it hears or speaks

Adafruit_BicolorMatrix matrix = Adafruit_BicolorMatrix();  // We are using a bicolor Matrix

int piPin = 3;
char count = 0;

void setup() {
  matrix.begin(0x70);  // Turns on Matrix
  Serial.begin(9600);
  pinMode(piPin, INPUT);  // Tells the pin that it's an input
}

void loop() {
  int sense = digitalRead(piPin); //Reads the Pi pin

  while(sense == HIGH){ count + 1;}

  if(count >= 1) { //If the Raspberry Pi is sending input

    matrix.clear();
    matrix.drawBitmap(0, 0, looking_1, 8, 8, LED_YELLOW); // It will look forward
    matrix.writeDisplay();
    delay(3000);

    matrix.clear();
    matrix.drawBitmap(0, 0, looking_2, 8, 8, LED_YELLOW); // It will look forward
    matrix.writeDisplay();
    delay(200);

    matrix.clear();
    matrix.drawBitmap(0, 0, looking_3, 8, 8, LED_YELLOW); // It will look forward
    matrix.writeDisplay();
    delay(3000);

    matrix.clear();
    matrix.drawBitmap(0, 0, looking_2, 8, 8, LED_YELLOW);  //Makes it look back
    matrix.writeDisplay();
    delay(2000);

    count == 0;

  } else { 
    matrix.clear();
    matrix.drawBitmap(0, 0, blink_1, 8, 8, LED_GREEN); // It will just blink
    matrix.writeDisplay();
    delay(3000);

    matrix.clear();
    matrix.drawBitmap(0, 0, blink_2, 8, 8, LED_GREEN); // It will just blink
    matrix.writeDisplay();
    delay(100);

    matrix.clear();
    matrix.drawBitmap(0, 0, blink_3, 8, 8, LED_GREEN); // It will just blink
    matrix.writeDisplay();
    delay(100);

    matrix.clear();
    matrix.drawBitmap(0, 0, blink_4, 8, 8, LED_GREEN); // It will just blink
    matrix.writeDisplay();
    delay(100);

    matrix.clear();
    matrix.drawBitmap(0, 0, blink_5, 8, 8, LED_GREEN); // It will just blink
    matrix.writeDisplay();
    delay(100);

    matrix.clear();
    matrix.drawBitmap(0, 0, blink_6, 8, 8, LED_GREEN); // It will just blink
    matrix.writeDisplay();
    delay(100);

    matrix.clear();
    matrix.drawBitmap(0, 0, blink_7, 8, 8, LED_GREEN); // It will just blink
    matrix.writeDisplay();
    delay(100);

    matrix.clear();
    matrix.drawBitmap(0, 0, blink_8, 8, 8, LED_GREEN); // It will just blink
    matrix.writeDisplay();
    delay(100);

    matrix.clear();
    matrix.drawBitmap(0, 0, blink_9, 8, 8, LED_GREEN); // It will just blink
    matrix.writeDisplay();
    delay(100);

    matrix.clear();
    matrix.drawBitmap(0, 0, blink_8, 8, 8, LED_GREEN); // It will just blink
    matrix.writeDisplay();
    delay(100);

    matrix.clear();
    matrix.drawBitmap(0, 0, blink_7, 8, 8, LED_GREEN); // It will just blink
    matrix.writeDisplay();
    delay(100);

    matrix.clear();
    matrix.drawBitmap(0, 0, blink_6, 8, 8, LED_GREEN); // It will just blink
    matrix.writeDisplay();
    delay(100);

    matrix.clear();
    matrix.drawBitmap(0, 0, blink_5, 8, 8, LED_GREEN); // It will just blink
    matrix.writeDisplay();
    delay(100);

    matrix.clear();
    matrix.drawBitmap(0, 0, blink_4, 8, 8, LED_GREEN); // It will just blink
    matrix.writeDisplay();
    delay(100);

    matrix.clear();
    matrix.drawBitmap(0, 0, blink_3, 8, 8, LED_GREEN); // It will just blink
    matrix.writeDisplay();
    delay(100);

    matrix.clear();
    matrix.drawBitmap(0, 0, blink_2, 8, 8, LED_GREEN); // It will just blink
    matrix.writeDisplay();
    delay(100);

  }
}

你说得对,你可以把delay()换成millis()。 一种常见的方法是使用时间比较代码模式,它不会中断主循环。这允许 Arduino 以特定的频率或时间执行命令。

以下示例举例说明了如何在不使用 delay() 的情况下每秒(和每四秒)执行一个函数。我认为这应该可以解决您的问题并启用伪并行执行:

// 2 sec. frequency
unsigned long interval=2000;    // the time we need to wait
unsigned long previousMillis=0; // millis() returns an unsigned long.

// 4 sec. frequency  
unsigned long interval1=4000;    // the time we need to wait
unsigned long previousMillis1=0; // millis() returns an unsigned long.

void setup() {
   //...
}

void loop() {

 // other CMD's...

 if ((unsigned long)(millis() - previousMillis) >= interval) {
    previousMillis = millis();
    // every second second
    // ... 
 }

 // other CMD's...

 if ((unsigned long)(millis() - previousMillis1) >= interval1) {
    previousMillis1 = millis();
    // every fourth second
    // ... 
 }

 // other CMD's...
}

此外,您已经可以在 Whosebug 上找到一些类似的问题,例如 Arduino Multitasking