Interesting question:
DELAY method - we can do a delay, which I think pauses everything in the code and then go increment counters to keep time without calling for longs or millis which uses more RAM. By not comparing long variables, I thought this would lower power consumption and throttle back the CPU by not doing non stop long math...
main loop()
{
delay(20); //wait 20 milliseconds
counters++; //increment timers used to keep longer times like 3 seconds
buttons(); // read buttons on weapons
watch(); // interpret data received on infared sensors that collect data on interupt port 1
}
millis() METHOD - same thing as above, but this causes a NON stop subtraction and comparsion of longs. It uses more RAM, and in my mind, it seems that the CPU would run full throttle as it would do this 1000's of times per second instead of 50 times per second as above... I could be wrong though. Please tell me if you know.
main loop()
{
if(millis() - timestamp > 20) //do this 50 times per second
{
timestamp = millis(); //reset timer
counters++; //increment timers used to keep longer times like 3 seconds
buttons(); // read buttons on weapons
}
watch(); // interpret data received on infared sensors that collect data on interupt port 1
}
See, the delay function does not keep great time because if you run other code afterwards, it may take 100ms or so, so when you come back again, it may have been 120ms instead of 20ms... but it saves a lot of RAM (which I'm near capacity even w/ common issues as storing strings in flash memory and using minimal bytes of variables). If I go to the milli() sytle, I keep better time but it would seem to use more power/batteries = more heat. I can watch interupts while in delay, so I can interpret the data immediately thereafter w/o effect. I've played two seasons of laser tag using both methods and I'm trying to figure out which is better?
keep better time() //another idea to keep time more accurately
}
if(millis() - timestamp > 20) //do this 50 times per second
{
long temp = millis() - timestamp;
temp /= 20; //in case we were out of the main loop for much over 20ms, say 100ms
timestamp = millis(); //reset timer
while(temp > 0) //not sure if I can use a for loop w/ a long
{
counters++; //increment timers used to keep longer times like 3 seconds
temp --; //
}
buttons(); // read buttons on weapons
}
}
- I am no newbie. I designed a laser tag system using the arduino programming language and have fabricated my own boards w/ audio and the ATmega328P chip.