Arduino倒计时计时器阻止程序的其余部分运行
Arduino countdown timer preventing rest of program running
我有一个程序,它检查是否读取了RFID标签,如果读取了,它会运行一些代码。
我还有一个倒计时功能(计时器),我想在后台运行。它在LCD的底部显示时钟。
我遇到的问题是,当我把timer();在主循环中,程序的其余部分被延迟倒计时的持续时间,这意味着没有RFID卡被读取。
计时器工作,但在timer()以下什么都没有;正在运行。
有人知道我该如何防止这种情况发生吗?非常感谢。
#include <SPI.h>
#include <MFRC522.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
int readflag;
int initstate = 1;
int nextred = 4;
int nextblue = 8;
uint8_t readCard[4];
int minutes = 1; //start min
int seconds = 0; //start seconds
MFRC522 mfrc522(10, 9);
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
///////////////////////////////////////// Setup ///////////////////////////////////
void setup() {
//Protocol Configuration
Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max);
lcd.begin(20, 4);
}
///////////////////////////////////////////////////////////////////////////////
// Main loop
///////////////////////////////////////////////////////////////////////////////
void loop () {
do {
readflag = checkread();
if (initstate == 0) {
timer(); //start timer - timer works but nothing below timer(); is running.
lcd.setCursor(0,0);
lcd.print("Red team next: ");
lcd.print(nextred);
lcd.setCursor(0,1);
lcd.print("Blue team next: ");
lcd.print(nextblue);
}
}
while (!readflag);
tone(8, 850, 15);
recordid();
updatenextteamnumbers();
}
///////////////////////////////////////////////////////////////////////////////
// Function to update team next numbers
///////////////////////////////////////////////////////////////////////////////
void updatenextteamnumbers() {
//If Blue Tag scanned show team info
if (*((uint32_t *)readCard) == 0xACB7D573) {
Serial.println("Blue Tag detected, showing team info");
initstate=0;
}
//Red Team (sequence 4, 5, 2, 1, 9)
else if (*((uint32_t *)readCard) == 0x29FDC9F5 && initstate == 0) {
Serial.println("CARD 4 detected");
nextred = 5;
}
else if (*((uint32_t *)readCard) == 0x3FCE7832 && initstate == 0) {
Serial.println("CARD 5 detected");
nextred = 2;
}
else if (*((uint32_t *)readCard) == 0x3FCCECE2 && initstate == 0) {
Serial.println("CARD 2 detected");
nextred = 1;
}
else if (*((uint32_t *)readCard) == 0x3F7AB752 && initstate == 0) {
Serial.println("CARD 1 detected");
nextred = 9;
//activateredrelay
}
else if (*((uint32_t *)readCard) == 0x00000000 && initstate == 0) {
Serial.println("CARD 9 detected");
redwins();
}
//Blue Team (sequence 8, 6, 3, 7, 10)
else if (*((uint32_t *)readCard) == 0x00000000 && initstate == 0) {
Serial.println("CARD 8 detected");
nextblue = 6;
}
else if (*((uint32_t *)readCard) == 0x00000000 && initstate == 0) {
Serial.println("CARD 6 detected");
nextblue = 3;
}
else if (*((uint32_t *)readCard) == 0x3F7ABA82 && initstate == 0) {
Serial.println("CARD 3 detected");
nextblue = 7;
}
else if (*((uint32_t *)readCard) == 0x00000000 && initstate == 0) {
Serial.println("CARD 7 detected");
nextblue = 10;
//activatebluerelay
}
else if (*((uint32_t *)readCard) == 0x00000000 && initstate == 0) {
Serial.println("CARD 10 detected");
bluewins();
}
}
///////////////////////////////////////////////////////////////////////////////
// Stores the ID of the card that's been detected in readCard byte array
///////////////////////////////////////////////////////////////////////////////
void recordid() {
mfrc522.PICC_IsNewCardPresent();
mfrc522.PICC_ReadCardSerial();
for (int i = 0; i < mfrc522.uid.size; i++) {
readCard[i] = mfrc522.uid.uidByte[i];
Serial.print(mfrc522.uid.uidByte[i], HEX);
}
Serial.println("");
mfrc522.PICC_HaltA();
}
/////////////////////////////////////////////
// Returns 1 if a card has been detected
/////////////////////////////////////////////
int checkread() {
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return 0; } //no card detected
mfrc522.PICC_HaltA();
return 1; } //card detected
/////////////////////////////////////////////
// Runs if red team wins
/////////////////////////////////////////////
void redwins() {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Red team wins!");
lcd.display();
Serial.println("Red team wins!");
delay(36000);
}
/////////////////////////////////////////////
// Runs if blue team wins
/////////////////////////////////////////////
void bluewins() {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Blue team wins!");
lcd.display();
Serial.println("Blue team wins!");
delay(36000);
}
/////////////////////////////////////////////
// Count down timer
/////////////////////////////////////////////
void timer() {
lcd.setCursor(0,3);
lcd.print("Time left:");
while (minutes > 0 || seconds >= 0) {
lcd.setCursor(15, 3);
(minutes < 10) ? lcd.print("0") : NULL;
lcd.print(minutes);
lcd.print(":");
(seconds < 10) ? lcd.print("0") : NULL;
lcd.print(seconds);
lcd.display();
decrement();
delay(1000);
}
}
/////////////////////////////////////////////
// Decrement timer
/////////////////////////////////////////////
void decrement() {
if (seconds > 0) {
seconds -= 1;
}
else {
if (minutes > 0) {
seconds = 59;
minutes -= 1;
}
else {
timeup();
}
}
}
/////////////////////////////////////////////
// Called when time expires
/////////////////////////////////////////////
void timeup() {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Time up!");
lcd.display();
}
这里的确凿证据如下:
while (minutes > 0 || seconds >= 0)
当这些变量的值都达到0时,decrement
函数将不再修改它们。这是一个问题,因为seconds >= 0
的计算结果仍然为true,因此会导致无限循环。
相关文章:
- 运行同一解决方案的另一个项目的项目
- CMake-按正确顺序将项目与C运行时对象文件链接
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- 代码在main()中运行,但在函数中出现错误
- 我在c++代码中生成了一个运行时#3异常
- 如何在linux终端中同时编译和运行c++代码
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 如何在运行中期切换GTK CSS style_context
- 如何在MS Visual Studio 2019中运行QT UI
- 如何通过cpp程序运行shell脚本
- IPC使用多个管道和分支进程来运行Python程序
- 删除指向指针的指针是运行时错误吗
- 如何用参数值调用函数(仅在运行时已知)
- 为什么即使使用-cudart-static进行编译,库用户仍然需要链接到cuda运行时
- 是否可以在编译时初始化数组,以便在运行时不会花费时间?
- c++中的指针和运行时错误
- 在C应用程序中运行C++(带有STL)函数
- 运行程序时出现问题
- 控制允许动态运行c++的并发操作数
- 无法获取菜单选择以运行函数.C++