Issue with timer and not running correctly

For my project I "elapsedMillis" as a timer. In my "loop" section I start with an "if" statement because I don't want to run the loop constantly but only every 10 minutes or longer.

void loop(void)
  if (timer0 > interval) {
    timer0 -= interval; //reset the timer

   // do stuff

  }  //end interval
} // void loop

As long as I keep my project hooked up to my pc with a USB cable and have the serial monitor open, the project does exactly what It needs to do. It goes into the "loop", runs, and then waits for the timer to finish and start over again.

When I let the project run standalone, using a 9v battery, I know it runs once but seems to stop after that.

When I let the project run standalone, using a 9v battery, I know it runs once but seems to stop after that.

Then, I suggest that you need to either fix your code or use a real battery.

Posting your full program would help.

...R

void loop(void)
  if (timer0 > interval) {
    timer0 -= interval; //reset the timer

   // do stuff

  }  //end interval
} // void loop

Output:

sketch_oct31b:2: error: expected initializer before ‘if’
sketch_oct31b:8: error: expected declaration before ‘}’ token

I'm a little surprised it runs at all.

http://snippets-r-us.com/

The code I used was used as an example. Below is an extract of my actual code. I left out all the nits and pieces which are not relevant.
Like I stated in my original post: the code runs fine and loops every (in this case) 5 minutes but only if I run it while connected with USB.

// Load required libraries.
//-------------------------------------------------------------------------
#include <elapsedMillis.h>
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <string.h>
#include "utility/debug.h"

// Setup timer and interval.
//-------------------------------------------------------------------------
elapsedMillis timer0;
#define interval 300000

void setup(void)
  // Initialise (output to) serial monitor.
  Serial.begin(115200);

  timer0 = interval + 1; // Make sure loop commands are executed at least once at start of interval cycle.
}


void loop(void)
{
  if (timer0 > interval) {
    timer0 -= interval; //reset the timer

	// Do other stuff
	
  }
} // void loop

Like I stated in my original post: the code runs fine

But you have not posted the code, or anything but useless snippets, so you are on your own to solve the problem. Good luck.

I’ve had a quick look at your code. The first issue I find is that you’re including debug.h I don’t have a clue what’s in that.

Secondly, when I try to compile it I get

sketch_nov03a:5: error: ‘elapsedMillis’ does not name a type
sketch_nov03a:10: error: expected initializer before ‘Serial’
sketch_nov03a:12: error: expected constructor, destructor, or type conversion before ‘=’ token
sketch_nov03a:13: error: expected declaration before ‘}’ token

Can you please post some code that will actually compile. We’re used to looking at warts and all.

If the problem is exactly where you expect it to be, you’d have likely found it by now. If you had found it, you would have sorted it out.

Since you haven’t sorted it out, the chances are it’s some place you’re NOT looking. We can’t find what you don’t allow us to see.

POST THE WHOLE SKETCH!

Akubra: As long as I keep my project hooked up to my pc with a USB cable and have the serial monitor open, the project does exactly what It needs to do.

Which Arduino?

How to use this forum

What code?

http://snippets-r-us.com/

Akubra: The code I used was used as an example. Below is an extract of my actual code. I left out all the nits and pieces which are not relevant.

void loop(void)
{
  if (timer0 > interval) {
    timer0 -= interval; //reset the timer
// Do other stuff

  } } // void loop

You left out where the variable timer0 changes, for one thing.

Akubra: As long as I keep my project hooked up to my pc with a USB cable and have the serial monitor open, the project does exactly what It needs to do. ... When I let the project run standalone, using a 9v battery, I know it runs once but seems to stop after that.

Surely this indicates that the problem is something to do with serial comms, and absolutely nothing to do with timers. Or, possibly, something to do with power. You get a lot more power from a USB cable than a 9v battery.

One thing I would test in your place is to plug the USB cable into a USB charger (rather than a PC) and see if the power boost makes a difference.