Arduino 使用几分钟后停止工作
Posted
技术标签:
【中文标题】Arduino 使用几分钟后停止工作【英文标题】:Arduino stops working after being just minutes in use 【发布时间】:2020-02-27 22:53:30 【问题描述】:当我出于某种原因在我的 arduino nano 上运行这段代码时,它似乎停止了一段时间。我试过启动它并等待 5 分钟,但这似乎还没有冻结它。
有谁知道 waitforkey() 是否可以在等待太久之后停止处理器运行?还是我可能有某种内存泄漏?
#include<Keypad.h>
const byte ROWS = 4;
const byte COLS = 3;
char keys[ROWS][COLS] =
'1', '2', '3',
'4', '5', '6',
'7', '8', '9',
'0', 'E', 'C'
;
byte rowPins[ROWS] = 8, 7, 6, 9;
byte colPins[COLS] = 2, 3, 4;
char command[4];
char p0;
char p1;
char p2;
char p3;
int count = 0;
int relayPin = 10;
bool solved = false;
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup()
keypad.setHoldTime(0); // Default is 1000mS
keypad.setDebounceTime(0); // Default is 50mS
pinMode(relayPin, OUTPUT);
pinMode(A0, OUTPUT);
pinMode(A1, OUTPUT);
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
void loop()
ledCompare(count);
if (solved == true)
digitalWrite(relayPin, HIGH);
digitalWrite(A0, HIGH);
command[3] = 'E';//makes statement untrue
char key = keypad.waitForKey();
count = 0;
solved = false;
ledsOff();
char key = keypad.waitForKey();
if (key == 'C')
count = 0;
else
command[count] = key;
count++;
if (count == 4)
if (command[0] == p0 && command[1] == p1 && command[2] == p2 && command[3] == p3)
solved = true;
else if(command[0] == p0 && command[1] == p1 && command[2] == p2 && command[3] == p3)
reprogram();
else
ledsOff();
digitalWrite(relayPin, LOW);
solved = false;
count = 0;
void ledsOn()
digitalWrite(A0, HIGH);//Meest rechter
digitalWrite(A1, HIGH);
digitalWrite(A2, HIGH);
digitalWrite(A3, HIGH);
void ledsOff()
digitalWrite(A0, LOW);//Meest rechter
digitalWrite(A1, LOW);
digitalWrite(A2, LOW);
digitalWrite(A3, LOW);
void ledCompare(int x)
if (count >= 1)
digitalWrite(A3, HIGH);
if (count >= 2)
digitalWrite(A2, HIGH);
if (count >= 3)
digitalWrite(A1, HIGH);
void reprogram()
ledsOff();
char key = keypad.waitForKey();
if(key != 'C')
p0 = key;
digitalWrite(A3, HIGH);
else
reprogram();
key = keypad.waitForKey();
if(key != 'C')
p0 = key;
digitalWrite(A2, HIGH);
else
reprogram();
key = keypad.waitForKey();
if(key != 'C')
p0 = key;
digitalWrite(A1, HIGH);
else
reprogram();
key = keypad.waitForKey();
if(key != 'C')
p0 = key;
digitalWrite(A0, HIGH);
solved = true;
else
reprogram();
【问题讨论】:
幸运的是,reprogram()
永远不会被调用,无论未定义的 p1、p2、p3 具有哪些值。
【参考方案1】:
由于递归 reprogram() 调用,您很可能会耗尽内存。
我什至不明白为什么您首先需要递归调用。在内存如此有限的设备上,递归调用几乎总是一个坏主意。
另见:
https://arduino.stackexchange.com/questions/355/how-much-can-i-recurse-how-much-can-i-recurse-how-much-caqfsdrfw
【讨论】:
reprogram();
永远不应该被调用,因为那个愚蠢的if (cond) else if (cond) /* impossible to reach */
以上是关于Arduino 使用几分钟后停止工作的主要内容,如果未能解决你的问题,请参考以下文章
Twilio WebRTC TURN 中继在几分钟后随机停止工作
Worklight iOS Geofence Native API 在几分钟后停止在后台工作