Timer call & reset
in
Programming Questions
•
1 year ago
I'm struggling with setting up a timer to count down, but to be reset if there is input.
/*
Please forgive my ignorance. I only know what I've already learned, and this world is a lot bigger than that.
*/
I have searched on the forums, and I have seen a few different timer sketches, but they all seem to be triggered once and then set to run down without further input.
I need a timer that begins counting when there is no input. When input is seen, the timer resets itself.
I tried modifying code that I found, but the sketch, once it starts the timer, will never reset it. The timer will only run once over the life of the sketch.
What am I missing here?
- boolean movement = false;
- float mvmt;
- Timer timer;
- void setup() {
- size(200,200);
- background(0);
- timer = new Timer(5000);
- timer.start();
- }
- void draw() {
- mouseDetect();
- if(movement == false){
- timer.start();
- }
- if (timer.isFinished()) {
- fill(random(0, 255));
- ellipse(random(10, width), random(10, height), 20, 20 );
- }
- }
- void mouseDetect(){
- float mvmt = abs(pmouseX - mouseX);
- if (mvmt > 0){
- println(mvmt);
- movement = true;
- }else{
- movement = movement || false;
- }
- }
- class Timer {
- int savedTime; // When Timer started
- int totalTime; // How long Timer should last
- Timer(int _TotalTime) {
- totalTime = _TotalTime;
- }
- // Starting the timer
- void start() {
- // When the timer starts it stores the current time in milliseconds.
- savedTime = millis();
- println("timer start at" + savedTime);
- }
- // The function isFinished() returns true if 5,000 ms have passed.
- // The work of the timer is farmed out to this method.
- boolean isFinished() {
- // Check how much time has passed
- int passedTime = millis()- savedTime;
- if (passedTime > totalTime) {
- return true;
- } else {
- return false;
- }
- }
- }
/*
Please forgive my ignorance. I only know what I've already learned, and this world is a lot bigger than that.
*/
1