/ Breath_Tester / code.py
code.py
1 # SPDX-FileCopyrightText: 2018 Mikey Sklar for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 import time 6 7 import adafruit_sgp30 8 import board 9 import busio 10 11 i2c = busio.I2C(board.SCL, board.SDA, frequency=100000) 12 13 # Create library object on our I2C port 14 sgp30 = adafruit_sgp30.Adafruit_SGP30(i2c) 15 sgp30.iaq_init() 16 sgp30.set_iaq_baseline(0x8973, 0x8aae) 17 18 # highest tVOC recorded in 30 seconds 19 highest_breath_result = 0 20 21 22 def warmup_message(): 23 warmup_time = 20 24 warmup_counter = 0 25 26 # initial read required to get sensor going 27 sgp30.iaq_measure() 28 29 print() 30 print("Warming Up [%d seconds]..." % warmup_time) 31 32 while warmup_counter <= 20: 33 print('.', end='') 34 time.sleep(1) 35 warmup_counter += 1 36 37 38 def get_breath_reading(): 39 breath_time = 30 # seconds to record breath reading 40 # one second count up to breath_time value 41 breath_counter = 0 42 # initialize list with empty values 43 breath_saves = [0] * (breath_time + 1) 44 45 print() 46 print("We will collect breath samples for 30 seconds.") 47 print("Take a deep breath and exhale into the straw.") 48 input(" *** Press a key when ready. *** ") 49 print() 50 51 while breath_counter <= breath_time: 52 _, tvoc = sgp30.iaq_measure() 53 breath_saves[breath_counter] = tvoc 54 print(tvoc, ', ', end='') 55 time.sleep(1) 56 breath_counter += 1 57 58 breath_saves = sorted(breath_saves) 59 result = breath_saves[breath_counter - 1] 60 61 return result 62 63 64 # show the highest reading recorded 65 66 67 def show_results(breath_result): 68 print() 69 print() 70 print("peak VOC reading:", breath_result) 71 print() 72 input("Press any key to test again") 73 print() 74 75 76 # main 77 while True: 78 warmup_message() 79 highest_breath_result = get_breath_reading() 80 show_results(highest_breath_result)