I am using a Raspberry Pi Pico, Micro Python, I have created code to work as a stopwatch the code is below:
The program runs smoothly and can count up to 9 minutes when plugged into my computer via USB. However, if I use a battery pack instead, the timer stops at a random point, always less than 5 minutes. When I reconnect the device to the computer via USB, the timer resumes correctly, adjusting for the time it was paused. Keeping it hooked up to the PC is not an option, it needs to be able to run on a battery pack.
The battery pack is 3AA batteries, and can power everything without flickering at all, so I don't think its a power issue.
Additionally, I need this stopwatch so if there is an easier way that won't get stuck please point me in that direction.
The program runs smoothly and can count up to 9 minutes when plugged into my computer via USB. However, if I use a battery pack instead, the timer stops at a random point, always less than 5 minutes. When I reconnect the device to the computer via USB, the timer resumes correctly, adjusting for the time it was paused. Keeping it hooked up to the PC is not an option, it needs to be able to run on a battery pack.
The battery pack is 3AA batteries, and can power everything without flickering at all, so I don't think its a power issue.
Additionally, I need this stopwatch so if there is an easier way that won't get stuck please point me in that direction.
Code:
import tm1637import timeimport sysimport selectimport gc; gc.collect();import micropythonfrom machine import Pinfrom utime import sleepmydisplay = tm1637.TM1637(clk=Pin(0), dio=Pin(1))mydisplay.brightness(4)button = Pin(6,Pin.IN,Pin.PULL_UP)bigREDButton = Pin(3,Pin.IN,Pin.PULL_UP)greenLED = Pin(4,Pin.OUT)#redLED = Pin(5,Pin.OUT)#redLED.off()greenLED.off()debounce_time=0gc.enable()class StopWatch: def __init__(self): self.startTime = None self.elaspedTime = 0 self.isRunning = False def startWatch(self): if not self.isRunning: self.startTime = ((time.time_ns() + 500000) // 1000000) self.isRunning = True print("Stop Watch has started") def stopWatch(self): if self.isRunning: self.elaspedTime = ((time.time_ns() + 500000) // 1000000) - self.startTime self.isRunning = False print("Stop Watch has stopped") def restartWatch(self): self.startTime = None self.elaspedTime = 0 self.isRunning = False print("Stop watch has been reset") def logWatchTime(self): totalTime = self.elaspedTime if self.isRunning: totalTime += ((time.time_ns() + 500000) // 1000000) - self.startTime milisec = int(((totalTime)%1000)/100) sec = int(totalTime/1000)%60 minute = int(totalTime/(1000*60))%60 print(milisec) print(totalTime) x= f"{minute:01}{sec:02}{milisec:01}" print(f"{minute:01}:{sec:02}:{milisec:01}") mydisplay.show(x) Mystopwatch = StopWatch()lastbuttonstate = 0totalTime = 0 while True: lastbuttonstate = button.value() if bigREDButton.value() == 0 and Mystopwatch.isRunning and time.ticks_ms()-debounce_time > 500: Mystopwatch.stopWatch() debounce_time=time.ticks_ms() #set the light to off greenLED.off() #redLED.off() elif button.value() == 0 and time.ticks_ms()-debounce_time > 500: Mystopwatch.restartWatch() for x in range(10,0,-1): mydisplay.number(x) time.sleep(1) #set the light to green greenLED.on() #redLED.on() Mystopwatch.startWatch() debounce_time=time.ticks_ms() elif(Mystopwatch.isRunning): Mystopwatch.logWatchTime() print(micropython.mem_info()) gc.collect()
Statistics: Posted by ericBrock — Thu Feb 08, 2024 6:06 pm