Raspberry Pi Pomodoro timer with lights (utilizing JamHat passion board )


# gpiozero incorporates the JamHat object (the little circuit board with the lights and buzzer on it)

from gpiozero import JamHat

from time import sleep

import math

 

 

######################################################### features #########################################################

def play_end_timer_tune(jamhat):

    jamhat.buzzer.play('C4')

    sleep(0.5)

    jamhat.off()

 

    jamhat.buzzer.play(70)

    sleep(0.5)

    jamhat.off()

 

    jamhat.buzzer.play(220.0)

    sleep(0.5)

 

######################################################### do the calculation to work out what number of lights to activate #########################################################

def calc_number_lights_to_switch_on(jamhat, loopcounter, selectedTimer ):

    # reset all lights

    jamhat.off()        

    totalNumLeds = 6

    gapForEachLight = float(selectedTimer) / float(totalNumLeds)

 

    numberOfLightsToLightNow = float(loopcounter) / float(gapForEachLight)    

    numberOfLightsToLightNow = math.ceil(numberOfLightsToLightNow)

    turnOnThisManyLights(jamhat, numberOfLightsToLightNow)

 

def turnOnThisManyLights(jamhat, numberOfLedsToLight):    

  

    print("numberOfLedsToLight = "+str(numberOfLedsToLight))

    lights = ["jamhat.lights_1.red.on()", "jamhat.lights_1.yellow.on()", "jamhat.lights_1.green.on()",

        "jamhat.lights_2.red.on()", "jamhat.lights_2.yellow.on()", "jamhat.lights_2.green.on()"]

    # loop array based mostly on numberOfLedsToLight

    i = 0

    whereas i < numberOfLedsToLight:

        eval(lights[i])        # name eval on every component within the array , as much as the depend e.g. eval('aVar = aVar + 1')

        i += 1

    sleep(6)        # retains the loop depend timer the identical (so all calculations ought to work on timting )

 

def turn_off_all_lights_and_turn_on_green_lights_to_indicate_ready(jamhat):    

  

    # reset it

    jamhat.off()

    # Flip the inexperienced lights on (indciates booted and program prepared)

    jamhat.lights_1.inexperienced.on()

    jamhat.lights_2.inexperienced.on()    

 

######################################################### finish of features #########################################################

    

######################################################### begin of fundamental program #########################################################

# Initialise the JamHat object.

jamhat = JamHat()

 

turn_off_all_lights_and_turn_on_green_lights_to_indicate_ready(jamhat)

 

# half-hour timer  60 seconds * 30mins = 1800 seconds / 6 (every loop takes 6 seconds ) = 300

timerLength = 300  # 300 = 30 minutes

# e.g. in case you wished a 5minute timer = 60 * 5 = 300 / 6 = 50

# timer = 50   # 5 minute timer

 

print ("funky timer bootingy !")

 

# Setup infinite loop in attempt / catch so person can CTRL+C terminate (if and provided that keyboard connected)

attempt:

    whereas True:

        if(jamhat.button_1.is_pressed): # blue button            

            # every looper must go till 300 ( which is the same as half-hour )                                              

            loopcounter = 0

 

            selectedTimer = timerLength

 

            whereas loopcounter < selectedTimer:

                calc_number_lights_to_switch_on(jamhat, loopcounter, selectedTimer)                

                loopcounter += 1    

 

            # loop ends after half-hour PLAY BUZZER

            play_end_timer_tune(jamhat)

 

        turn_off_all_lights_and_turn_on_green_lights_to_indicate_ready(jamhat)          

          

        sleep(0.1)

 

besides KeyboardInterrupt:

    # If somebody presses CTRL+C, shut the JamHat, liberating of the Pins to be used elsewhere.

    jamhat.shut()



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *