code.py
 1  # SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  import time
 6  
 7  import analogio
 8  import board
 9  import neopixel
10  
11  pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=1.0)
12  light = analogio.AnalogIn(board.LIGHT)
13  
14  # Turn only pixel #1 green
15  pixels[1] = (0, 255, 0)
16  
17  # How many light readings per sample
18  NUM_OVERSAMPLE = 10
19  # How many samples we take to calculate 'average'
20  NUM_SAMPLES = 20
21  samples = [0] * NUM_SAMPLES
22  
23  while True:
24      for i in range(NUM_SAMPLES):
25          # Take NUM_OVERSAMPLE number of readings really fast
26          oversample = 0
27          for s in range(NUM_OVERSAMPLE):
28              oversample += float(light.value)
29          # and save the average from the oversamples
30          samples[i] = oversample / NUM_OVERSAMPLE  # Find the average
31  
32          mean = sum(samples) / float(len(samples))  # take the average
33          print((samples[i] - mean,))  # 'center' the reading
34          time.sleep(0.025)  # change to go faster/slower