babyO timer?

I am trying to create a 1-2 month timer on my babyO. I created a one second pulse that I could use to keep track except I ran into probs. I started with an interrupt but found it interferred with the other libraries. I then tried using millis() and it is very clunky. I am using the arduino environment with the pololu libs.
Can anyone suggest a better way of keeping track of time for a one to two month period using a baby orangutan???

Here is my code to pulse an led as a test. I don’t like it but can’t think of a good way to keep track of time. The libraries don’t seem to have any function for this unless I missed it. Suggestions anyone?

Thanks!

#include <OrangutanLEDs.h>

OrangutanLEDs leds;
boolean Working = false;
unsigned long StartTime = 0;

void setup() {
StartTime = millis();
}

void loop() {
// if we are in the first few milliseconds of the second …
if(((millis() - StartTime)%1000 < 20) & !Working){
Working = true;
leds.red(true); // turn red LED on
delay(20); // wait to ensure we don’t re-enter this second
Working = false;
}
// if we are in the first few milliseconds of the second half of the second …
if(((millis() - StartTime)%1000 >= 500) & ((millis() - StartTime) >= 520) & Working){
Working = true;
leds.red(false); // turn red LED off
delay(20); // wait to ensure we don’t re-enter this second
Working = false;
}
}

Thanks

Hello.

Is there any particular reason you’re working in the Arduino environment? I think AVR Studio is a far superior IDE, and you can use our more capable Pololu AVR Libraries.

To help you, I need to know a bit more about what you’re trying to do. For example, what resolution do you need in your timer? What else does your main loop need to do besides timing? The first solution that comes to mind is to update a 32-bit second counter variable every 1000 millisecond, which would let you time for 2^32 seconds, or 136 years, before your counter overflows:

unsigned long seconds = 0;

int main()
{
  unsigned long prevMs = millis();
  while (1)
  {
    if (millis() - prevMs >= 1000)
    {
      seconds++;
      prevMs += 1000;
    }

    if (seconds >= 60UL*60*24*31)
      // 31 days have elapsed; do something
  }
}

Note that the Baby Orangutans use ceramic resonators with an initial frequency tolerance of ±0.5%, which means that you could be gaining or losing as much as 7.2 minutes per day, or 3.7 hours per month. If you want better accuracy, you can calibrate your particular Baby Orangutan to compensate. The resonator frequency can also vary with temperature.

- Ben

Thanks Ben
That info is exactly what I need. The one second resolution is fine for my application (an egg incubator).
I have been trying to get into the AVR Studio but having trouble figuring things out. I come from the arduino side but liked the integrated motor drivers on the babyO but have not been able to find a good reference page for the general questions or for the library definitions and function descriptions. I find the arduino reference page easier to use and understand (arduino.cc/en/Reference/Libraries).
I would like to switch to the AVR Studio but may need a little help. Can you point me to some good reference pages for the pololu environment?

another question …
in this code, if (seconds >= 60UL*60*24*31) what does the 60UL mean/do?

Thanks

The code I gave you above will work in the Arduino environment if you change it around to use setup() and loop() instead of main(), so if you’re more comfortable using that, don’t feel like you have to change. Here are some things you might find helpful if you do want to change:

Pololu Library user’s guide
Pololu library command reference
Baby Orangutan user’s guide
USB AVR Programmer user’s guide

The programmer user’s guide has a section about using AVR Studio that you might find helpful, as do some of the guides for our other AVR based products.

The “UL” in 60UL tells the compiler to treat the value as an unsigned long (a 32-bit, or 4-byte, value), which means you can be sure the result of the multiplication is an unsigned long. It is probably smart enough to do this right on its own without the “UL”, but it doesn’t hurt to give it that extra information so you can be sure. If for some reason it decided the value was an integer (16-bit, or 2-byte), the multiplication will overflow as it’s too big to fit in a 16-bit variable.

- Ben