Timing problems
in
Programming Questions
•
3 years ago
Hi there, newbie speaking.
I'm writing a program with two timers that should work together: when one ends, the other one starts, and some booleans are changed. Now, all works well with timer1, but timer2 fails miserably. When it's done, it's supposed to change a bool from false to true, but it just doesn't. And I am not sure what I am doing wrong!
Here's the code, any help will be much appreciated!
- //testing Timer class
- Timer scoreTimer;
- Timer cooldownTimer;
- PFont font;
- Boolean available;
- int score = 0;
- void setup()
- {
- size (400, 400);
- scoreTimer = new Timer(5);
- cooldownTimer = new Timer(3);
- font = createFont("Helvetica", 22);
- textFont(font);
- available = true;
- }
- void draw()
- {
- background(0);
- stroke(255);
- line(200, 0, 200, 400);
- fill(255, 0, 0);
- text((scoreTimer.time), 100, 100);
- fill(0, 255, 0);
- text(cooldownTimer.time, 300, 100);
- checkTimers();
- if(mouseX > 200 && available == false)
- {
- score();
- }
- if(scoreTimer.isDone)
- {
- cooldown();
- }
- if(cooldownTimer.isDone)
- {
- available = true;
- }
- println(available);
- }
- void checkTimers()
- {
- if(scoreTimer.running)
- {
- scoreTimer.work();
- }
- if(cooldownTimer.running)
- {
- cooldownTimer.work();
- }
- }
- void score()
- {
- scoreTimer.start();
- available = false;
- }
- void cooldown()
- {
- cooldownTimer.start();
- }
- /* Very basic timer class
- takes one parameter, max. time in sec.
- */
- class Timer
- {
- int time, startTime, currentTime, maxTime;
- Boolean running, isDone;
- Timer(int _maxTime)
- {
- maxTime = _maxTime;
- isDone = false;
- stop();
- }
- void reset()
- {
- currentTime = startTime = millis()/1000;
- time = currentTime - startTime;
- }
- void start()
- {
- isDone = false;
- running = true;
- }
- void stop()
- {
- running = false;
- }
- void work()
- {
- if(running)
- {
- currentTime = millis()/1000;
- time = currentTime - startTime;
- if(time >= maxTime)
- {
- isDone = true;
- reset();
- stop();
- }
- }
- }
- }
1