Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Please Help: reseting the count of variable in a while loop

from microbit import *
from ssd1306 import initialize, clear_oled
from ssd1306_stamp import draw_stamp
from ssd1306_img import create_stamp
from ssd1306_text import add_text
import utime as time
window = []  
threshold = 550  # limit
count = 0 
sample = 0 
beat = False  
start = False
good = create_stamp(Image.HAPPY)
bad = create_stamp(Image.SAD)
heart = create_stamp(Image.HEART)
values = []  
def mean(datalist):
    sum = 0 
    for i in datalist:  
        sum += i 
    if len(datalist) > 0:  
        return sum/len(datalist)  
    else:
        return None 
while True:
    if button_a.was_pressed():
        while True:
            signal = pin1.read_analog()   
            window.append(signal)  
            avg = round(mean(window)) 
            values.append(avg) 
            if len(window) == 11:    
                window.pop(0)  
            if beat is False and avg >= threshold+10:
                beat = True 
                count += 1  
                display.show(Image.HEART, wait=False) 
                if count == 1:  
                    t1 = time.ticks_ms() 
                if count == 11:  
                    t2 = time.ticks_ms()  
                    T = t2 - t1     
                    bpm = round(600*1000/(T)) 
                    display.scroll(str(bpm)) 
                    initialize()
                    clear_oled()
                    add_text(0, 0, "Heart rate :")
                    add_text(0, 3, str(bpm) + " bpm")
                    if 60 <= bpm <= 100:
                        draw_stamp(38, 24, good, 1)
                    else :
                        draw_stamp(38, 24, bad, 1)
                    count = 0  
                    sleep(2000)
                if button_b.was_pressed():
                    break
                    count = 0
            elif beat is True and avg <= threshold-10:
                beat = False 
                display.clear()  
                sample += 1  
            if sample == 250:  
                threshold = mean(values)
                values = []  
                sample = 0  
            sleep(20)  
        sleep(20)  

This is the code connect to the microbit, and the function is about the heart rate sensor, which it will appear the average beat per minute when sensing 10 beats of the heart. I am thinking of adding a function that if button b is pressed, the loop will pause, and press the button a to start again. I tried to add a break function in the loop, it worked when I click the button b, it pause the loop, however, the count of beat won’t reset, and the time between the beat is also recorded although I break the loop, is there any way that I can break the loop and reset the count?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You should reset the counter before breaking. In your current code the line for the count is not executing since it breaks out before it reaches it.

if button_b.was_pressed():
    count = 0
    break
    
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading