Programming the NXT is nice. However, sometimes you run into limits that you think shouldn’t be there. This morning I hit one of those and scratched my head: “How to fix this?”. Normally there are 4 timers at your disposal in ROBOTC (this also goes for NXC, as far as I am aware of). I am not sure why we only get 4 of them and why not, say, 10.
My BobBot Mk2 needs quite a number of timers and reusing the 4 at my disposal isn’t really an option. I had an itch and it needed scratching. This is how most programs get started, it’s the reason the driver suite came to be. So, after a little fiddling and fumbling, I present to you: TMR-driver.h and a little test program. These two will become part of the next release of the driver suite, so they may be subject to change without notice and all that.
The usage should be fairly straightforward, the example program uses two timers to make the NXT beep at different intervals.
Please note that there is no roll-over checking done at all. So if you plan to run your program for more than 596 hours straight, prepare for some weirdness.
You can download the code here: [LINK]. (version 0.2)
Edit: Link has been changed to version 0.2. The separate task is gone now and code has been cleaned up a bit more.
You may want to define the timers as unsigned longs (you may have done so before I posted this, for all I know). You could also use an array of unsigned bytes, in which case the only limit is the available memory on the NXT, but then reading it becomes difficult, unless you are timing how long the program has been running in miliseconds up to centuries, but good luck with hitting year one of continuous operations. (If you succeed, I have been trying to make an alarm clock that also keeps track of school days, allowing me to submit sick days and snow days. It couldn’t maintain a connection to the RTC-Nx, and kept crashing early in the morning, with a soft, continuous click. The nightmare of the old alarm clock’s gohst is real with this one.)
ROBOTC doesn’t have unsigned longs. Using arrays of bytes would be an option, but cumbersome. I think I can live with the limitations 🙂
– Xander
Hi there,
You said it annoyed you thar the NXT has only got four timersN but wouldn’t it be possible to make variables into timers by making them increase by one every second using loops? I’m not really sure if it’d work, and I can’t test it now, but I think it’s possible to use that idea to get a ginormeaus amount of timers. Would it?
Thx,
DiMastero
See comment 1. I thought about using miliseconds, but if you are tracking what time it is so you can get up in the morning, don’t be surprised if you’re late. Of coarse, in NXC, you could get the current tick (timer reading) and wait until it equals or exceeds the original value plus whatever time you’re waiting for. The only problems is when the timer loops around, so you’d need to think of something else to fix that.