Back to: Multiplier Event Luxembourg
import board
import time
from adafruit_circuitplayground import cp
# Turn only NeoPixel #1 green.
cp.pixels[1] = (0, 255, 0)
# Define the number of light readings taken per sample.
NUM_OVERSAMPLE = 10
# Define the number of samples taken to calculate the average.
NUM_SAMPLES = 20
# Initialise samples.
samples = [0] * NUM_SAMPLES
while True:
# Cycle through a range of NUM_SAMPLES values.
for s in range(NUM_SAMPLES):
# The NUM_OVERSAMPLE number of light readings is taken really fast.
oversample = 0
# Sum up all the oversamples (light readings taken per sample).
for o in range(NUM_OVERSAMPLE):
oversample += cp.light
# Calculate the average of the oversamples and save the value.
samples[s] = oversample / NUM_OVERSAMPLE
# Calculate the average of the samples.
# When the next sample comes along, we drop the oldest one and
# recompute the average of the NUM_SAMPLES most recent values.
mean = sum(samples) / len(samples)
# Subtract the average of the samples from the sampled value
# * to center the light readings around zero, or
# * to remove the DC bias, i.e., the mean value of the waveform.
pulse = samples[s] - mean
# Depending on the switch, write to
# * the serial console (if True, i.e. left), or to
# * the CircuitPython storage (if False, i.e. right).
if cp.switch:
print( (pulse, ) )
else:
f = open("pulse.csv", "a")
f.write(repr(pulse) + "\n")
f.close()
# Allow a slight delay between the readings.
time.sleep(0.025)
import storage
from adafruit_circuitplayground import cp
storage.remount("/", readonly=cp.switch)
