Timer resets when resumed

Hi everyone!

I have recently run into an interesting issue, it appears that the timer (edu.wpi.first.wpilibj.Timer) resets, when you .stop() and then .start(). Is there a different timer I should be using or a better way to implement the one I have? I am looking for it to resume exactly where it left off, closer to a pause/resume than a start/stop.

Thanks for the help!

I don’t know if there is a pause sort of thing. You could try setting an integer equal to the value of the timer before you stop it, and setting the value of the timer equal to the variable after you restart it. Not sure if that would work, but it’s a thought!

Thanks for the reply! I had previously considered this, however unless I’m mistaken, there also is no Set() method.

Looking at the source for the Timer class, it definitely should continue increasing if you stop() then start() again.

 
85             public synchronized double get() {
86                 if (m_running) {
87                     return ((double) ((getMsClock() - m_startTime) + m_accumulatedTime)) / 1000.0;
88                 } else {
89                     return m_accumulatedTime;
90                 }
91             }

107             public synchronized void start() {
108                 m_startTime = getMsClock();
109                 m_running = true;
110             }

118             public synchronized void stop() {
119                 final double temp = get();
120                 m_accumulatedTime = temp;
121                 m_running = false;
122             }

Can you post your code showing your usage so we can see if anything jumps out as to why you’re seeing it reset?

Interesting! Must be an issue with how I use it…unfortunately I don’t have the code in front of me right now, I’ll post it later today. Thanks for the help!

Ok, after doing testing I have narrowed down the timer reset to the line

timer.start();

here is some test code that is resetting when you start and then stop.

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Custom;
import edu.wpi.first.wpilibj.Timer;

/**
 *
 * @author Robotics
 */
public class TimerTest {
	Timer autoTimer;
    public void robotInit(){
        
    }
       
    public void autonomousInit(){
    	autoTimer = new Timer();
        autoTimer.start();
    }
    public void autonomousPeriodic() {
    	System.out.println("AutoTimer First: " + autoTimer.get());
    	Timer.delay(2);
    	System.out.println("AutoTimer Second: " + autoTimer.get()+"

");
    	autoTimer.stop();
    	Timer.delay(2);
    	System.out.println("AutoTimer Third: " + autoTimer.get()+"

");
    	Timer.delay(2);
    	autoTimer.start();
    	System.out.println("AutoTimer Fourth: " + autoTimer.get()+"

");
    }
    
    public void teleopInit(){
    }
    
    public void teleopPeriodic() {
    }
    
    public void testInit(){
    }
    
    public void testPeriodic() {
    }
    
    
    public void disabledInit(){
    }
    
}

you should notice that after the timer is re-started the autoTimer.get(); printout becomes 0.

I have actually solved my problem using an offshoot of what @legts suggested.

Thanks for the help!