SOLVED - Never call loop(); as a function. It eats memory alive. Find other ways to bypass functions you don’t want to run in certain circumstances. In the Arduino IDE use Tools - Auto-Formatter to help learn syntax.
Greetings - My sketch is locking up at a predictable number of loops. When loaded in a MEGA 2560 it locks up on loop #1505. In the UNO it locks up on loop#350. At first it took a power cycle to bring the board back to life, but a few seconds later… Dead.
I added the watchdog timer and a variable to increase +1 every time void loop() starts. With the watchdog timer (WDT) at least the Arduino restart instead of locking up.
It seems that some part of memory is filling up and causing the lockup. I say this because as I comment out (remove) certain functions and libraries the number of loops before lockup/reboot increases. The same behavior is demonstrated on the MEGA 2560 and UNO R3 (authentic Arduino’s both). I am using Arduino IDE 1.6.1 on a Macbook Pro OS/X 10.9.5.
To reduce complexity I’ve deleted much of the code in the example loaded below. The example below locks up on UNO R3 at 350 loops. On MEGA 2560 it locks up on 1505.
The purpose of the program is to move a DC motor according to the position of a 3Axis accelerometer (X) and the current hour as read from a Real Time Clock. Both the hour and angle are stored in their separate arrays.
As seen in the attached sketch the hour is outside the range I care about so the function call void doNothing is called, which only prints a debug message a short delay and then calls void loop(). Changing the delay values in the sketch does makes each loop take longer but does not effect the number of loop cycles before locking up and then reboot after the WDT expires.
I am not a programmer by trade, so please be kind about my ugly code.
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <LiquidCrystal.h>
#include <Arduino.h>
#include <avr/wdt.h>
int panelAngle;
int park;
long nowHour;
long nowTime;
int nowAngle;
int maintMode;
int indexTime;
int indexAngle;
int indexNum;
int k;
int l;
int running;
const int onboardLED = 13;
int timeMove[10] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
int angleArray[10] = {-65, -55, -40, -20, 0, 20, 40, 55, 65, -65 };
void setup () {
wdt_disable();
delay(2L * 1000L);
wdt_enable(WDTO_8S);
pinMode(onboardLED, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
Serial.begin(57600);
Wire.begin();
sensors_event_t event;
for (k = 1; k <= 3; k = k + 1) {
digitalWrite(onboardLED, HIGH);
delay(250L);
digitalWrite(onboardLED, LOW);
delay(250L);
}
Serial.println("******************** REBOOTED *********************************");
}
void loop ()
{
l = l + 1;
Serial.println(l);
wdt_reset();
int running;
running = millis() / 1000;
delay(50);
Serial.print("\t");
void getCurrentIndex();
{
if ((nowHour < (timeMove[0])) || (nowHour > (timeMove[9])))
{
doNothing();
}
else if ((nowHour == timeMove[0]))
{
indexNum = 0;
}
else if ((nowHour == timeMove[1]))
{
indexNum = 1;
}
else if ((nowHour == timeMove[2]))
{
indexNum = 2;
}
else if ((nowHour == timeMove[3]))
{
indexNum = 3;
}
else if ((nowHour == timeMove[4]))
{
indexNum = 4;
}
else if ((nowHour == timeMove[5]))
{
indexNum = 5;
}
else if ((nowHour == timeMove[6]))
{
indexNum = 6;
}
else if ((nowHour == timeMove[7]))
{
indexNum = 7;
}
else if ((nowHour == timeMove[8]))
{
indexNum = 8;
}
else if ((nowHour == timeMove[9]))
{
indexNum = 9;
}
}
Serial.print(" Debug Print indexNum ");Serial.print(indexNum);Serial.println();
Serial.print(" Debug Print timeMove[indexNum]");Serial.println(timeMove[indexNum]);
if (panelAngle == (angleArray[indexNum]))
{
halt();
}
else if (panelAngle == (angleArray[indexNum]) -1)
{
halt();
}
else if (panelAngle == (angleArray[indexNum]) -2)
{
halt();
}
else if (panelAngle == (angleArray[indexNum]) -3)
{
halt();
}
else if (panelAngle == (angleArray[indexNum]) -4)
{
halt();
}
else if (panelAngle == (angleArray[indexNum]) -5)
{
halt();
}
else if (panelAngle == (angleArray[indexNum]) +1)
{
halt();
}
else if (panelAngle == (angleArray[indexNum]) +2)
{
halt();
}
else if (panelAngle == (angleArray[indexNum]) +3)
{
halt();
}
else if (panelAngle == (angleArray[indexNum]) +4)
{
halt();
}
else if (panelAngle == (angleArray[indexNum]) +5)
{
halt();
}
else if (panelAngle < (angleArray[indexNum]))
{
west();
}
else if (panelAngle > (angleArray[indexNum]))
{
east();
}
}
************ Below are funtions in a 2nd tab of the IDE *******************
//******************** code in a 2nd tab of the IDE *******************
void doNothing ()
{
// Serial.println("DEBUG Do Nothing");
delay(50);
loop();
}
void west ()
{
Serial.println("DEBUG west");
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
digitalWrite(7, LOW);
delay(50);
loop();
}
void east ()
{
Serial.println("DEBUG east");
digitalWrite(4, LOW);
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
digitalWrite(7, HIGH);
delay(50);
loop();
}
void halt ()
{
Serial.println("DEBUG halt");
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
delay(2000 );
loop();
}