私はArduino UnoとATMega328Pを使用して、ユーザーが制御するスイッチで簡単にいくつかのLEDを制御しています。しかし、私のメインループ(約16秒)を通して約56回反復した後、私のプログラムはリセットされます。ウォッチドッグタイマーとは関係がありますが、wtd_disable()でディセーブルにしていてもそうです。私のセットアップでは、問題は解決しません。ユーザーがスイッチを反転した場合にのみ終了できるループがプログラムに入ります。助言がありますか?Arduinoプログラムリセットバグ
//Don't worry, I have all necessary libraries and variables set up.
void setup()
{
Serial.begin(9600); // start serial for output
Serial.println(i2c_init());
wdt_disable();
//pinMode(22,INPUT_PULLUP);
//pinMode(23,INPUT_PULLUP);
pinMode(wakePin, INPUT);
pinMode(ACPin, INPUT);
pinMode(PowerPin, INPUT);
pinMode(PowerLED, OUTPUT);
pinMode(ACLED, OUTPUT);
pinMode(Battery1LED, OUTPUT);
pinMode(Battery2LED, OUTPUT);
pinMode(WifiLED, OUTPUT);
pinMode(TesterLED, OUTPUT);
pinMode(EnableLED, OUTPUT);
attachInterrupt(0, wakeUpNow, LOW);
}
void loop()
{
digitalWrite(PowerLED, LOW);
// digitalWrite(ACLED, LOW); Exclude AC Power LED
digitalWrite(Battery1LED, LOW);
digitalWrite(Battery2LED, LOW);
digitalWrite(WifiLED, LOW);
digitalWrite(TesterLED, LOW);
digitalWrite(EnableLED, LOW);
Enable = 0;
Serial.println("Reset Complete");
int ACPower = digitalRead(ACPin);
digitalWrite(ACLED, ACPower);
int v1 = fetchWord(deviceAddress1, VOLTAGE);
int v2 = fetchWord(deviceAddress2, VOLTAGE);
int BatteryVoltage = max(v1,v2);
Serial.print("Highest Battery Voltage: ");
Serial.println(BatteryVoltage);
delay(250);
if((BatteryVoltage >= 7000) | (ACPower == 1)){
int PowerOK = digitalRead(PowerPin);
if (PowerOK == 0){
loop();
}else {
bulk();
}
}else{
loop();
}
}
//This is the main part of my code that is constantly looped through,
//and after 16 seconds, the program resets, going back to loop()
void bulk()
{
Enable = 1;
digitalWrite(EnableLED, HIGH);
int ACPower = digitalRead(ACPin);
digitalWrite(ACLED, ACPower);
//int Battery1State = BatteryState(deviceAddress1);
int Battery1State = 2; // Simulating low battery
switch (Battery1State){
case 1:
digitalWrite(Battery1LED, HIGH);
break;
case 2: //I can't run parallel code to control the blinking LED,
//so I toggle the LED every pass through. Case 2 blinks slowly
if(i >= 4){
toggleLED(Battery1LED);
i = 0;
} else {
i++;
}
break;
case 3: //case 3 blinks quickly
toggleLED(Battery1LED);
break;
}
int Battery2State = 3; // simulating a very low battery
switch (Battery2State){
case 1:
digitalWrite(Battery2LED, HIGH);
break;
case 2:
if(j >= 4){
toggleLED(Battery2LED);
j = 0;
} else {
j++;
}
break;
case 3:
toggleLED(Battery2LED);
break;
}
buttonState = digitalRead(wakePin); //button is HIGH by default
if(buttonState == HIGH){
Serial.println(count);
if(count == 0){
starttime = millis();
} else if (count == 54){
endtime = millis();
runtime = endtime - starttime;
Serial.print("System Run Time: ");
Serial.println(runtime);
}
count++;
int PowerOK = digitalRead(PowerPin);
digitalWrite(PowerLED, PowerOK);
delay(250);
//Repeat this code if power switch is on, restart if power is turned off
if(PowerOK == 0){
loop();
}else {
bulk();
}
...それが完了するたびに、ループが としては、これまで自分自身を呼び出すためのループの必要はありません述べた。..(繰り返し呼び出され、一度呼び出されます – cchapman