Interactive Assignment: Edelweiss

Summary:

I named this project “Edelweiss,” as I was inspired by the song from The Sound of Music. This assignment is the prototype for my final project. This project is meant to resemble an edelweiss flower and change colors to the song “Edelweiss.” The lights on the circuit playground will shine different colors according to the frequency of the note being played. One note causes the circuit playground to light up red and the next note might turn it yellow. I intend to make my final project a bouquet of these flowers and want their lights to synchronize to the song.

Materials:

  • microphone
  • hot glue gun
  • loofa/mesh
  • tissues
  • circuit playground
  • 3 alligator clips
  • 3 wires

Step 1: Form Flower

  • Cut up loofa/mesh material into 6 large rectangular pieces
  • Cut up loofa/mesh material into 5 small rectangular pieces
  • Stuff each rectangular loofa/mesh piece with tissue
  • Hot glue the 2 ends to give the material a pinched “petal” look
  • Hot glue the ends of the 6 large petals together to create a flower shape
  • Hot glue the ends of the 5 small petals together to create a flower shape
  • Stack the smaller layer on top of the larger layer to form the full flower

Step 2: Connect microphone to circuit playground

  • Connect a wire to VCC, GND, and OUT on the mic
  • Clip an alligator clip to each wire
  • Connect the VCC on the mic to the A4 on the circuit playground using the alligator clip
  • Connect GND on the mic to the GND on the circuit playground using the alligator clip
  • Connect OUT on the mic to VOUT on the circuit playground using the alligator clip

Step 3: Download Code

# SPDX-FileCopyrightText: 2019 Carter Nelson for Adafruit Industries
#
# SPDX-License-Identifier: MIT

import time
import array
import board
import audiobusio
import simpleio
import neopixel

# —| User Configuration |—————————
# SAMPLERATE = 31500
SAMPLERATE = 31500
SAMPLES = 1024
# THRESHOLD = 100
THRESHOLD = 140
MIN_DELTAS = 4
DELAY = 0.15
NUM_PIXELS = 10

FREQ_LOW = 500
FREQ_HIGH = 1000

# octave = 1 2 3 4 5 6 7 8
NOTES = {
“C”: (33, 65, 131, 262, 523, 1047, 2093, 4186),
“D”: (37, 73, 147, 294, 587, 1175, 2349, 4699),
“E”: (41, 82, 165, 330, 659, 1319, 2637, 5274),
“F”: (44, 87, 175, 349, 698, 1397, 2794, 5588),
“G”: (49, 98, 196, 392, 785, 1568, 3136, 6272),
“A”: (55, 110, 220, 440, 880, 1760, 3520, 7040),
“B”: (62, 123, 247, 494, 988, 1976, 3951, 7902),
}
# —————————————————-

COLORS = (
(0xFF, 0x00, 0x00), # pixel 0 red
(0xFF, 0x71, 0x00), # pixel 1
(0xFF, 0xE2, 0x00), # pixel 2
(0xAA, 0xFF, 0x00), # pixel 3
(0x38, 0xFF, 0x00), # pixel 4
(0x00, 0xFF, 0x38), # pixel 5
(0x00, 0xFF, 0xA9), # pixel 6
(0x00, 0xE2, 0xFF), # pixel 7
(0x00, 0x71, 0xFF), # pixel 8
(0x00, 0x00, 0xFF), # pixel 9 blue
)
# —————————————————-

# —————————————————-
RED = (0xFF, 0x44, 0x33)
M_RED = (0xFF, 0x66, 0x33)
L_RED = (0xFF, 0x57, 0x33)
ORANGE = (0xFF, 0x8B, 0x33)
M_ORANGE = (0xFF, 0xAD, 0x33)
L_ORANGE = (0xFF, 0xD6, 0x33)
YELLOW = (0xFF, 0xEE, 0x33)
O_ORANGE = (255, 69, 0)
PINK = (156, 3, 39)
GREY = (220, 154, 154)
OFF = (0, 0, 0)

prevNote = “Z”

# seven notes (E, G, D, C, F, A, B)
# E is red, G, D, F, A, B, C is yellow
LIGHTS = {
“E”: RED,
“G”: M_RED,
“D”: L_RED,
“C”: ORANGE,
“F”: M_ORANGE,
“A”: L_ORANGE,
“B”: YELLOW,
“Z”: OFF,
}

# Iterator to keep track of which iteration it’s on
c = 0

# Create a buffer to record into
samples = array.array(“H”, [0] * SAMPLES)

# Setup the mic input
mic = audiobusio.PDMIn(
board.MICROPHONE_CLOCK, board.MICROPHONE_DATA, sample_rate=SAMPLERATE, bit_depth=16
)

# Setup NeoPixels
pixels = neopixel.NeoPixel(
board.NEOPIXEL, NUM_PIXELS, brightness=0.08, auto_write=False
)

while True:
# Get raw mic data
mic.record(samples, SAMPLES)

# Compute DC offset (mean) and threshold level
mean = int(sum(samples) / len(samples) + 0.5)
threshold = mean + THRESHOLD

# Compute deltas between mean crossing points
# (this bit by Dan Halbert)
deltas = []
last_xing_point = None
crossed_threshold = False
for i in range(SAMPLES – 1):
sample = samples[i]
if sample > threshold:
crossed_threshold = True
if crossed_threshold and sample < mean:
if last_xing_point:
deltas.append(i – last_xing_point)
last_xing_point = i
crossed_threshold = False

# Try again if not enough deltas
if len(deltas) < MIN_DELTAS:
continue

# Average the deltas
mean = sum(deltas) / len(deltas)

# Compute frequency
freq = SAMPLERATE / mean

print(“crossings: {} mean: {} freq: {} “.format(len(deltas), mean, freq))

# Find corresponding note
for note in NOTES:
for octave, note_freq in enumerate(NOTES[note]):
if note_freq * 0.97 <= freq <= note_freq * 1.03:
# input note data
print(“-” * 10)
print(“NOTE={}{}”.format(note, octave + 1))
print(“-” * 10)
# print(“Prev Note = {}”.format(prevNote))

# Show on NeoPixels
# Retrieve associated color with note and set neopixels to that color
pixels.fill(LIGHTS[note])
pixels.show()
time.sleep(0.1)
pixels.show()
# prevNote = note
# time.sleep(DELAY)

video tutorial: https://drive.google.com/file/d/1A6rZD7l7fMBxI3Tq-eqQ1IYjP1K8l77L/view?usp=sharing