问题
I am using an arduino uno, logic level converter, adafruit Bicolor LED Matrix, a Raspberry pi, and a button but am having some issues. My goal is that when the button is pushed on the pi it sends a signal to the arduino Uno which will change the animation on the LED backpack. The issue is that the button must be held down right when the loop restarts and if you click it any other time it wont work, I think this is because I am using delay() which pauses everything. Is there any way to pause without using delay()? I have heard of using Millis() but I am new to Arduino's and im not sure how I would implement it at all. Thanks in advance.
#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);
}
}
回答1:
You are right, you can replace delay()
with millis()
.
A common approach to that is to use a time comparing code pattern, that does not interrupt the main loop. This allows the Arduino to execute commands in specific frequencies or times.
The following example exemplify how you can execute a function every second second (and every fourth second) without using delay()
. I think this should solves your problem and enables a pseudo-parallel execution:
// 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...
}
In addition, you can already find some similar questions such Arduino Multitasking on stackoverflow.
来源:https://stackoverflow.com/questions/31792093/pause-without-delay-arduino