我正在使用arduino uno,逻辑电平转换器,adafruit Bicolor LED矩阵,Raspberry pi和按钮,但我遇到了一些问题。我的目标是当按下按钮时,它会向arduino Uno发送一个信号,这将改变LED背包上的动画。问题是当循环重新启动时必须按下按钮,如果你在任何其他时间点击它都不会工作,我认为这是因为我使用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);
}
}
答案 0 :(得分:1)
您是对的,您可以将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...
}
此外,您已经可以在stackoverflow上找到一些类似问题,例如Arduino Multitasking。