/ Breath_Mask / 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_CCS811
  8  import board
  9  import busio
 10  import neopixel
 11  
 12  # i2c interface for the gas sensor
 13  i2c_bus = busio.I2C(board.SCL, board.SDA)
 14  ccs = adafruit_CCS811.CCS811(i2c_bus)
 15  
 16  # Three Different NeoPixel 8 LED Lengths for Output:
 17  # 1 - Temperature - Circuit Playground Built-In LEDs
 18  # 2 - Total Volatile Organic Compounds [strip]
 19  # 3 - Co2 Output - NeoPixel [strip]
 20  num_leds = 8
 21  temperature_pix = neopixel.NeoPixel(board.NEOPIXEL, num_leds, brightness=.1)
 22  tvoc_pix = neopixel.NeoPixel(board.A1, num_leds, bpp=4, brightness=.1)
 23  co2_pix = neopixel.NeoPixel(board.A2, num_leds, bpp=4, brightness=.1)
 24  led_draw = .05  # delay for LED pixel turn on/off
 25  
 26  # wait for the sensor to be ready and calibrate the thermistor
 27  while not ccs.data_ready:
 28      pass
 29  temp = ccs.temperature
 30  ccs.temp_offset = temp - 25.0
 31  
 32  
 33  def clear_pix(delay):
 34      # clear all LEDs for breathing effect
 35      for i in range(0, num_leds):
 36          temperature_pix[i] = (0, 0, 0)
 37          co2_pix[i] = (0, 0, 0, 0)
 38          tvoc_pix[i] = (0, 0, 0, 0)
 39          time.sleep(delay)
 40  
 41  
 42  def co2_led_meter():
 43      # Show Carbon Dioxide on a NeoPixel Strip
 44      co2_floor = 400
 45      co2_ceiling = 8192
 46  
 47      # Map CO2 range to 8 LED NeoPixel Stick
 48      co2_range = co2_ceiling - co2_floor
 49      co2_led_steps = co2_range / num_leds
 50      co2_leds = int((ccs.eCO2 - co2_floor) / co2_led_steps)
 51  
 52      # Insert Colors
 53      for i in range(0, (co2_leds - 1)):
 54          co2_pix[i] = (255, 0, 255, 0)
 55          time.sleep(led_draw)
 56  
 57  
 58  def tvoc_led_meter():
 59      # Show Total Volatile Organic Compounds on a NeoPixel Strip
 60      tvoc_floor = 0
 61      tvoc_ceiling = 1187
 62  
 63      # Map CO2 range to 8 LED NeoPixel Stick
 64      tvoc_range = tvoc_ceiling - tvoc_floor
 65      tvoc_led_steps = tvoc_range / num_leds
 66      tvoc_leds = int(ccs.TVOC / tvoc_led_steps)
 67  
 68      # Insert Colors
 69      for i in range(0, (tvoc_leds - 1)):
 70          tvoc_pix[i] = (0, 0, 255, 0)
 71          time.sleep(led_draw)
 72  
 73  
 74  def temp_led_meter():
 75      # Show Temperature on Circuit Playground built-in NeoPixels
 76      temp_floor = 23
 77      temp_ceiling = 36
 78  
 79      # Map temperature range to 8 LEDs on Circuit Playground
 80      temp_range = temp_ceiling - temp_floor
 81      temp_led_steps = temp_range / num_leds
 82      temp_leds = int((ccs.temperature - temp_floor) / temp_led_steps)
 83  
 84      # Insert Colors
 85      for i in range(0, (temp_leds - 1)):
 86          temperature_pix[i] = (255, 255, 0)
 87          time.sleep(led_draw)
 88  
 89  
 90  while True:
 91      # print to console
 92      # - co2
 93      # - total voltatile organic compounds
 94      # - temperature in celsius
 95      print("CO2: ", ccs.eCO2, " TVOC:", ccs.TVOC, " temp:", ccs.temperature)
 96  
 97      co2_led_meter()
 98      tvoc_led_meter()
 99      temp_led_meter()
100  
101      time.sleep(.5)
102      clear_pix(led_draw)