Page 1 of 1

Countdown timer

Posted: Tue Nov 27, 2018 10:56 am
by Tillmario
Hello,

I'm trying to write my first real MicroPython script. It's supposed to be a timer that starts to count down from 60, when a button is pushed and visualise it on a display. This works fine so far:

Code: Select all

import machine
from machine import Pin, I2C
import ssd1306
from time import sleep

i2c = I2C(-1, Pin(5), Pin(4))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
led = Pin(15, Pin.OUT)
button = Pin(12, Pin.IN, Pin.PULL_UP)

oled.fill(1)
oled.show()

def frame():
    oled.line(1,1,126,1,0)
    oled.line(1,2,126,2,0)
    oled.line(1,3,126,3,0)
    
    oled.line(1,60,126,60,0)
    oled.line(1,61,126,61,0)
    oled.line(1,62,126,62,0)
    
    oled.line(1,4,1,59,0)
    oled.line(2,4,2,59,0)
    oled.line(3,4,3,59,0)
    
    oled.line(124,4,124,59,0)
    oled.line(125,4,125,59,0)
    oled.line(126,4,126,59,0)

    oled.line(5,5,5,58,0)
    oled.line(5,5,122,5,0)
    oled.line(122,5,122,58,0)
    oled.line(5,58,122,58,0)
    oled.show()
    
def clear():
    for j in range(6,57):
        oled.line(6,j,121,j,1)
    oled.show()

def countdown(): 
        for i in range(60,-1,-1):
            oled.text(str(i),55,32,0)
            oled.show()
            sleep(0.9)
            clear()
            oled.show()
        
frame()
while True:
    if not button.value():
        countdown()
But that's not all I want. When the countdown is running and the button is pushed again the countdown is supposed to immediately start from 60 again.

I was thinking:

Code: Select all

def countdown():    
    try:    
        for i in range(60,-1,-1):
            oled.text(str(i),55,32,0)
            oled.show()
            sleep(0.9)
            clear()
            oled.show()
    except:
        if not button.value():
            countdown()
But that doesn't seem to work. Can someone explain to me why not and how I could do it?

Greetings, Tom

Re: Countdown timer

Posted: Tue Nov 27, 2018 11:06 am
by OutoftheBOTS_
A simplex approach could be this

Code: Select all

count = 60		
while True:
  clear()
  oled.text(str(count),55,32,0)
  oled.show()
  count -=1
  if count < 0: count = 60
  if button.value(): count = 60
  time.sleep(0.9)
The downside to that approach is that the button press can't be detected during the time.sleep(0.9). If you want the bvutton to be tected anytime it is pressed then there will need to be a tiny bit more code to loop testing while waiting

Re: Countdown timer

Posted: Tue Nov 27, 2018 11:32 am
by Tillmario
Thanks for your help. :)
OutoftheBOTS_ wrote:
Tue Nov 27, 2018 11:06 am
If you want the bvutton to be tected anytime it is pressed then there will need to be a tiny bit more code to loop testing while waiting
That's seems to be what I need and I'm willing to learn how it works. Can you lead me in the right direction how to learn it? link or topic what to google for?

Re: Countdown timer

Posted: Tue Nov 27, 2018 12:54 pm
by OutoftheBOTS_
for a really simple solution you can do this

Code: Select all

count = 60		
while True:
  clear()
  oled.text(str(count),55,32,0)
  oled.show()
  count -=1
  if count < 0: count = 60  
  for i in range(9):    
	if button.value(): count = 60
	time.sleep(0.1) 
A little more complex but better for if you want to grow you code the following will only run the inner loop every 0.9 secs but the outter loop will run flat out.

Code: Select all

count = 60
start_time = time.time()		
while True:
  if button.value(): count = 60
  if time.time() - start_time > 0.9:    
	clear()    
	oled.text(str(count),55,32,0)    
	oled.show()    
	count -=1    
	if count < 0: count = 60
	start_time = time.time()