5. Heart beat simulation
5.1. Pin touching
- is_touched()
Return True if the pin is being touched with a finger, otherwise return False. It is best to touch the ground pin with the other hand.
from microbit import *
while True:
if pin0.is_touched():
display.show(0)
sleep(500)
else:
display.clear()
sleep(100)
Tasks
Add an elif branch to test for pin1 touching and display a 1 when it is.
Add another elif branch to test for pin2 touching and display a 2 when it is.
Add an elif branch to test for pin1 touching and display a 1 when it is.
from microbit import *
while True:
if pin0.is_touched():
display.show(0)
sleep(500)
elif pin1.is_touched():
display.show(1)
sleep(500)
else:
display.clear()
sleep(100)
Add another elif branch to test for pin2 touching and display a 2 when it is.
from microbit import *
while True:
if pin0.is_touched():
display.show(0)
sleep(500)
elif pin1.is_touched():
display.show(1)
sleep(500)
elif pin2.is_touched():
display.show(2)
sleep(500)
else:
display.clear()
sleep(100)
5.2. Pulse design
Tasks
Plan the design of 7 images to simulate the above heart beat being scrolled from right to left starting with the left edge of the pulse and ending with a flat line. Attempt to complete the code for the 7 images before reading on.
from microbit import *
hb1 = Image("00009:00009:00009:99999:00000")
hb2 = Image("00090:00090:00090:99999:00009")
hb3 = Image("00900:00900:00900:99999:00090")
hb4 = Image("09000:09000:09000:99999:00900")
hb5 = Image("90000:90000:90000:99999:09000")
hb6 = Image("00000:00000:00000:99999:90000")
hb7 = Image("00000:00000:00000:99999:00000")
heart_beat = [hb1, hb2, hb3, hb4, hb5, hb6, hb7]
display.show(hb7)
while True:
if pin0.is_touched():
display.show(heart_beat, delay=100, wait=True)
Tasks
Set the google metronome https://g.co/kgs/Rjdr6e to 65 and see if a delay of 132 keeps in time with the metronome.
Set the google metronome https://g.co/kgs/Rjdr6e to 100. Use the formula to calculate the delay required for a HR of 100 bpm. See if this keeps in time with the metronome.
Set the google metronome https://g.co/kgs/Rjdr6e to 65 and see if a delay of 132 keeps in time with the metronome.
Use:
display.show(heart_beat, delay=132, wait=True)
Set the google metronome https://g.co/kgs/Rjdr6e to 100. Use the formula to calculate the delay required for a HR of 100 bpm. See if this keeps in time with the metronome.
Use:
display.show(heart_beat, delay=85, wait=True)
5.3. Using a function to calculate the delay for a Heart rate
hr
parameter a default value of 60 as in def delay_for_heart_rate(hr=60)
to have a default heart rate of 60 bpm.int
function to convert a decimal to a whole number.def delay_for_heart_rate(hr=60):
return int(60000/(hr * 7))
from microbit import *
hb1 = Image("00009:00009:00009:99999:00000")
hb2 = Image("00090:00090:00090:99999:00009")
hb3 = Image("00900:00900:00900:99999:00090")
hb4 = Image("09000:09000:09000:99999:00900")
hb5 = Image("90000:90000:90000:99999:09000")
hb6 = Image("00000:00000:00000:99999:90000")
hb7 = Image("00000:00000:00000:99999:00000")
heart_beat = [hb1, hb2, hb3, hb4, hb5, hb6, hb7]
def delay_for_heart_rate(hr=60):
return int(60000/(hr * 7))
display.show(hb7)
while True:
if pin0.is_touched():
hr_delay = delay_for_heart_rate(60)
display.show(heart_beat, delay=hr_delay, wait=True)
Tasks
Add an elif branch to test for pin1 touching and use a heart rate for pin1 of 100 bpm.
Add another elif branch to test for pin2 touching and use a heart rate for pin2 of 150 bpm.
Add an elif branch to test for pin1 touching and use a heart rate for pin1 of 100 bpm.
from microbit import *
hb1 = Image("00009:00009:00009:99999:00000")
hb2 = Image("00090:00090:00090:99999:00009")
hb3 = Image("00900:00900:00900:99999:00090")
hb4 = Image("09000:09000:09000:99999:00900")
hb5 = Image("90000:90000:90000:99999:09000")
hb6 = Image("00000:00000:00000:99999:90000")
hb7 = Image("00000:00000:00000:99999:00000")
heart_beat = [hb1, hb2, hb3, hb4, hb5, hb6, hb7]
def delay_for_heart_rate(hr=60):
return int(60000/(hr * 7))
display.show(hb7)
while True:
if pin0.is_touched():
hr_delay = delay_for_heart_rate(60)
display.show(heart_beat, delay=hr_delay, wait=True)
elif pin1.is_touched():
hr_delay = delay_for_heart_rate(100)
display.show(heart_beat, delay=hr_delay, wait=True)
Add another elif branch to test for pin2 touching and use a heart rate for pin2 of 150 bpm.
from microbit import *
hb1 = Image("00009:00009:00009:99999:00000")
hb2 = Image("00090:00090:00090:99999:00009")
hb3 = Image("00900:00900:00900:99999:00090")
hb4 = Image("09000:09000:09000:99999:00900")
hb5 = Image("90000:90000:90000:99999:09000")
hb6 = Image("00000:00000:00000:99999:90000")
hb7 = Image("00000:00000:00000:99999:00000")
heart_beat = [hb1, hb2, hb3, hb4, hb5, hb6, hb7]
def delay_for_heart_rate(hr=60):
return int(60000/(hr * 7))
display.show(hb7)
while True:
if pin0.is_touched():
hr_delay = delay_for_heart_rate(60)
display.show(heart_beat, delay=hr_delay, wait=True)
elif pin1.is_touched():
hr_delay = delay_for_heart_rate(100)
display.show(heart_beat, delay=hr_delay, wait=True)
elif pin2.is_touched():
hr_delay = delay_for_heart_rate(150)
display.show(heart_beat, delay=hr_delay, wait=True)