Sketch runs for X number of loops then locks up

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();
  }

Simple - make all your time variables type "unsigned long". You are trying to store time values in an int. Consult the millis() documentation.

int running;
/...
  int running;
  running = millis() / 1000;

Notice also that your first variable "running" is out of the scope of the loop function where the second one is declared. That might be okay, but it is probably not what you intended.

void getCurrentIndex();

WTF? Why is there a function prototype in the middle of loop()?

Interesting. loop() calls doNothing(), which calls loop() which calls doNothing(), which calls loop() which calls doNothing(), which calls loop() which calls doNothing(), which calls loop() which calls doNothing(), which calls loop() which calls doNothing(), which calls loop() which calls doNothing(), which calls loop() which calls doNothing(), which calls loop() which …

I wonder where all of your memory is going. NEVER call loop()! Learn what return does, and how to use it.

As PaulS says,NEVER call loop().
So take out all the loop() calls in your code. Then let us know what happens.

JayStel:
I am not a programmer by trade, so please be kind about my ugly code.

I'm sympathetic to the extent that we have all been there. But it looks like you've gone too far without exploring the field. It seems like you can understand things, but you're not paying any attention to how the rest of the world programs.

OK. I've replaced the calls to loop() with return;

Watching Serial Monitor it's over 1400 loops.

EDITED to say "but wanted the behavior of"
Looks like there were more than one mistake. Earlier I tried using return; - but wanted the behavior of returning back to the beginning of void getCurrentIndex(); call.

Everything between void loop() and void getCurrentIndex(); was added for debugging.

Is it OK to return to void getCurrentIndex(); instead of void loop(); ??

Thanks for your patience.

Is it OK to return to void getCurrentIndex(); instead of void loop(); ??

When you return, you return to the statement after the call that you are returning from. If you return from getCurrentIndex(), and that was called by loop(), you return to loop().

If getCurrentIndex() calls foo(), which returns, it returns to getCurrentIndex().

PaulS:
When you return, you return to the statement after the call that you are returning from. If you return from getCurrentIndex(), and that was called by loop(), you return to loop().

If getCurrentIndex() calls foo(), which returns, it returns to getCurrentIndex().

Starting to make sense. In the function doNothing(); I commented out return; and added getCurrentIndex();. Compiling gives the error:
"AAADebugB.ino:5:21: error: 'getCurrentIndex' was not declared in this scope"

I'll study up on that.

With Return it does return to the end of getCurrentIndex(); and runs through everything below it. That is not the behavior I want. The desired behavior in my case is that once doNothing(); finishes go back to the "beginning" of getCurrentIndex(); --- This stops the DC motors from flip-flopping CW or CCW if the angle is not matched with the hour in the same index #.

BTW. This is a solar panel tracking mechanism I've installed here at home. It is working for many days without lockup now that the WDT was setup. But it's not really a "fix".

PaulS:
When you return, you return to the statement after the call that you are returning from. If you return from getCurrentIndex(), and that was called by loop(), you return to loop().

If getCurrentIndex() calls foo(), which returns, it returns to getCurrentIndex().

PaulS:
When you return, you return to the statement after the call that you are returning from. If you return from getCurrentIndex(), and that was called by loop(), you return to loop().

If getCurrentIndex() calls foo(), which returns, it returns to getCurrentIndex().

Paul - Your explanation on return made sense to me. Adding 'getCurrentIndex(); at the end of the doNothinng(); function gave an error and quick reading about it makes it seem that return is the only option. I'll have to figure out some error correction to overcome the possibility of a mismatch of index numbers for the time and angle arrays. MY brain is fried. Need a break.

I think that it is about time that you posted your program as it is now rather than us having to follow your descriptions of numerous changes to code that we cannot see.

UKHeliBob:
I think that it is about time that you posted your program as it is now rather than us having to follow your descriptions of numerous changes to code that we cannot see.

Good point, Sorry.

**************** Main Sketch *************************

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <LiquidCrystal.h>
#include <Arduino.h>
#include <avr/wdt.h>

unsigned long panelAngle;
unsigned long park;
unsigned long nowHour;
unsigned long nowTime;
unsigned long nowAngle;
unsigned long maintMode;

unsigned long indexTime;
unsigned long indexAngle;
unsigned long indexNum;
unsigned long k;
unsigned long l;
unsigned long running;

const int onboardLED = 13;
unsigned long timeMove[10] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
unsigned long 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();
  unsigned long running;
  running = millis() / 1000;
  delay(50);
  Serial.print("\t");

void start();{
}

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("IndexNum:  ");Serial.print(  indexNum);Serial.print("  nowHour: ");Serial.println(nowHour);
  
  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 2nd Tab of IDE*******************

  void doNothing ()
  {
    Serial.println("DEBUG Do Nothing");
    delay(50);
//  start();
    return;
  }
  void west ()
  {
    Serial.println("DEBUG west");
    digitalWrite(4, HIGH);
    digitalWrite(5, LOW);
    digitalWrite(6, HIGH);
    digitalWrite(7, LOW);
    delay(50);
    return;
  }
  void east ()
  {
    Serial.println("DEBUG east");
    digitalWrite(4, LOW);
    digitalWrite(5, HIGH);
    digitalWrite(6, LOW);
    digitalWrite(7, HIGH);
    delay(50);
    return;
  }
  void halt ()
  { 
    Serial.println("DEBUG halt");
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);    
    delay(2000 );
    return;
  }
void start();{
}

void getCurrentIndex();
{

Why is this nonsense in the middle of loop()? You are neither defining a function or calling a function. Get rid of this nonsense.

  if (panelAngle == (angleArray[indexNum]))

(You) (need) (to) (learn) (when) (parentheses) (are) (needed) (and) (when) (they) (aren't).

There are limited conditions where you call east() or west(). Under most other conditions, you call halt(). You need to revise that structure. Deal with when to call east() and west() and when to do nothing. Under all other conditions, call halt().

At the end of a function, return is automatic. You only really need return if the function actually return a value or if you want to return before you get to the end of a function.

 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;
  }

So you are comparing nowHour to an element in the timeMove array so you can find the index. You already know the index. This is what loops are for.

byte indexNum = 0;
for (indexNum; indexNum < 10; indexNum++)
{
  if (nowHour == timeMove[indexNum])
  {
    break;
  }
}

You really do need to watch the syntax. It’s really important to understand what the semi colon ; does. Same with curly braces {}

PaulS:

void start();{

}

void getCurrentIndex();
{



Why is this nonsense in the middle of loop()? You are neither defining a function or calling a function. Get rid of this nonsense.



if (panelAngle == (angleArray[indexNum]))



(You) (need) (to) (learn) (when) (parentheses) (are) (needed) (and) (when) (they) (aren't).

There are limited conditions where you call east() or west(). Under most other conditions, you call halt(). You need to revise that structure. Deal with when to call east() and west() and when to do nothing. Under all other conditions, call halt().

At the end of a function, return is automatic. You only really need return if the function actually return a value or if you want to return before you get to the end of a function.

Paul - So what you're saying is I need to learn how to structure programs. (agreed)

Jimmy - Originally I tried something "similar" to your example but could not get it to work. That's when I resorted to a list of if - else if's. Thanks for the example. It makes sense even though I've not yet fully grasped the braces, curly braces. More study follows.

I'll rebuild the whole thing as you advise. Much has been learned since this project begun. When finished, I'll post the code and performance as a comment on this thread. You each make good and valid points. Most of my time writing these sketches deal with debugging formatting errors. But I'm learning.

Thanks all for the guidance.

@JayStel,
Rome wasn't built in a day. Understanding arduino programming is no different. Hang in there.You have a good start so far.

The curly braces don’t do much in my example except make it easier to read. It could also be written like this:

byte indexNum = 0;
for (indexNum; indexNum < 10; indexNum++)
  if (nowHour == timeMove[indexNum])
    break;

Jimmy60:
The curly braces don’t do much in my example except make it easier to read. It could also be written like this:

byte indexNum = 0;

for (indexNum; indexNum < 10; indexNum++)
  if (nowHour == timeMove[indexNum])
    break;

I would advise using the braces every time even if there is only one line of code in the code block that they enclose. Not only does it make it easier to read and understand it makes it easier to add debugging statement and extra lines of code when necessary with no confusion as to when and why which lines of code will be executed.

Here is what I come up with applying advice given in this thread.

Side Note: I do find (as Jimmy says) that the curly braces help me to visualize unique instructions independently. Stated differently, The curly braces help my mind break the code into pieces/parts…

I did modify the for loop to identify correct current indexNum. The index numbers in an array start at 0 (zero) so I had to make this change. Also got rid of the call to doNothing. Just left no instruction if true.

Installed a few minutes ago and the panels went to the angle described for 10:00 - 10:59 which is -65 degrees.

Here is the full code with changes as it is running right now. The doNothing(); function is still in the code but is not called. Will delete it after some confidence is earned.

************* Main Sketch ***********************

#include <Wire.h>
#include "RTClib.h"
#include <Adafruit_MMA8451.h>
#include <Adafruit_Sensor.h>
#include <Arduino.h>
#include <avr/wdt.h>

//LiquidCrystal lcd(49, 45, 35, 33, 31, 29); // for MEGA pins lined up
Adafruit_MMA8451 mma = Adafruit_MMA8451();

RTC_DS1307 RTC;
long panelAngle;
unsigned long park;
long nowHour;
long nowTime;
unsigned long nowAngle;
unsigned long maintMode;
unsigned long indexTime;
unsigned long indexAngle;
unsigned long indexNum;
unsigned long k;
unsigned long l;
unsigned long running;

const unsigned long onboardLED = 13;
unsigned long timeMove[10] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
long 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();
  RTC.begin();

  if (! RTC.isrunning()) //{
    Serial.println("RTC is NOT running!");
    Serial.println("Adafruit MMA8451 test!");

  if (! mma.begin()) //{
    Serial.println("Couldnt start");
//    while (1);
 // }
  Serial.println("MMA8451 found!");

  mma.setRange(MMA8451_RANGE_2_G);
  mma.read();
  sensors_event_t event;
  mma.getEvent(&event);
  panelAngle = (event.acceleration.x * 100);
  Serial.print("Range = "); Serial.print(2 << mma.getRange());Serial.println("G");
  
      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();
  unsigned long running;
  running = millis() / 1000;
  delay(50);
  DateTime now = RTC.now();
  mma.read();
  sensors_event_t event;
  mma.getEvent(&event);
//  panelAngle = 65;
  panelAngle = (event.acceleration.x * 100);
//  nowHour = 18;
  nowHour = (now.hour());
byte indexNum = 0;
for (indexNum; indexNum <= 9; indexNum++)
{
  Serial.print("Current Hour is ");Serial.println(nowHour);
  Serial.print("Debug indexNum  ");Serial.print(indexNum);Serial.println();
  Serial.print("Debug timeMove[indexNum]  ");Serial.println(timeMove[indexNum]);
  Serial.print("Debug panelAngle  ");Serial.println(panelAngle);

  if (nowHour == timeMove[indexNum])
  {
    break;
  }
}
  if ((nowHour < (timeMove[0])) || (nowHour > (timeMove[9])))
  {
  }
  else 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();
  }
}
//Serial.print("Debug indexNum  ");Serial.print(indexNum);Serial.println();
//Serial.print("Debug timeMove[indexNum]  ");Serial.println(timeMove[indexNum]);

**************** Functions inside Tab B *************************

  void doNothing ()
  {
    Serial.println("DEBUG Do Nothing");
    delay(50);
  }
  void west ()
  {
    Serial.println("DEBUG west");
    digitalWrite(4, HIGH);
    digitalWrite(5, LOW);
    digitalWrite(6, HIGH);
    digitalWrite(7, LOW);
    delay(100);
  }
  void east ()
  {
    Serial.println("DEBUG east");
    digitalWrite(4, LOW);
    digitalWrite(5, HIGH);
    digitalWrite(6, LOW);
    digitalWrite(7, HIGH);
    delay(100);
  }
  void halt ()
  { 
    Serial.println("DEBUG halt");
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);    
    delay(100 );
  }

Stated differently, The curly braces help my mind break the code into pieces/parts…

void setup () {
      for (k = 1; k <= 3; k = k + 1) {
  void doNothing ()
  {

Consistent placement, and the use of Tools + Auto Format would help.

  if (! mma.begin()) //{
    Serial.println("Couldnt start");
//    while (1);
 // }
  Serial.println("MMA8451 found!");

Getting your delete key fixed would help, too. You can’t claim that the curly braces in that mess make anything clearer.

  l = l + 1;

Have you ever wondered why the language we use was not called C=C+1?

  nowHour = (now.hour());

While curly braces are useful, extraneous parentheses are not.

  if ((nowHour < (timeMove[0])) || (nowHour > (timeMove[9])))
  {
  }

Useless tests aren’t, either.

PaulS:

void setup () {

for (k = 1; k <= 3; k = k + 1) {
  void doNothing ()
  {



Consistent placement, and the use of Tools + Auto Format would help.



if (! mma.begin()) //{
    Serial.println(“Couldnt start”);
//    while (1);
// }
  Serial.println(“MMA8451 found!”);



Getting your delete key fixed would help, too. You can't claim that the curly braces in that mess make anything clearer.






l = l + 1;



Have you ever wondered why the language we use was not called C=C+1?



nowHour = (now.hour());



While curly braces are useful, extraneous parentheses are not.



if ((nowHour < (timeMove[0])) || (nowHour > (timeMove[9])))
  {
  }



Useless tests aren't, either.

PaulS - Actually, I can lay claim that a thing makes it easier for me and there be absolute truth in that claim. Thank you for your critique. I intend to further study the correct way of doing this new thing I’m trying to teach myself. I suggest you do some study on social graces. You come across as arrogant and demeaning to those seeking help. Not sure if that is your intent, or not. But the technical info you present is useful in my endeavor to learn. Thank you for that.