Object-oriented timer
in
Programming Questions
•
3 years ago
Newbie here. I've been struggling with making timers. This is what I need:
- I have an object with a boolean available.
- If another object gets close to it,
available = false
start a 5 second timer
perform an action while that timer works
- when the 5 second timer is done,
start a 10 second timer
- when the 10 second timer is done,
available = true
My problem is how to make one timer switch on and off the other one, more than one time.
This is my Timer class:
- /* Very basic timer class
- takes one parameter, max. time in sec.
- remember to make it run on main loop!
- */
- class Timer
- {
- int time, startTime, currentTime, maxTime;
- Boolean running;
- Timer(int _maxTime)
- {
- maxTime = _maxTime;
- stop();
- }
- void reset()
- {
- currentTime = startTime = millis()/1000;
- time = currentTime - startTime;
- }
- void start()
- {
- running = true;
- }
- void stop()
- {
- running = false;
- }
- void work()
- {
- if(running)
- {
- currentTime = millis()/1000;
- time = currentTime - startTime;
- }
- }
- Boolean isDone()
- {
- if(time >= maxTime)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
I guess there is a logic flaw somewhere, but I just cannot see it. Any help will be greatly appreciated!
1