Combining 2 different codes

Greetings all,

I've been working with the CircuitPlayground Express trying to make an interactive environmental sensors. Im using the sensors on the board as well as using a BME680 for extra readings. I'm outputting the levels to Neopixel Rings and i've managed to get the codes working individually but I cant seem to combine the codes to do it simultaneously. The codes are the following:

import array
import math
import audiobusio
import board
import neopixel
from busio import I2C

i2c = I2C(board.SCL, board.SDA)

thermistor = bme680.temperature
pixels = neopixel.NeoPixel(board.A1, 16, brightness=.01)
pixels.fill((0, 0, 0))
pixels.show()

n_pixels = 16 # Number of pixels you are using
mintemp = 0 # For adjustment of graph low
maxtemp = 30 # For adjustment of graph high

CURVE = 2
SCALE_EXPONENT = math.pow(10, CURVE * -0.1)

PEAK_COLOR = (100, 0, 255)
NUM_PIXELS = 16

NUM_SAMPLES = 120

def constrain(value, floor, ceiling):
return max(floor, min(value, ceiling))

def log_scale(input_value, input_min, input_max, output_min, output_max):
normalized_input_value = (input_value - input_min) / \
(input_max - input_min)
return output_min + \
math.pow(normalized_input_value, SCALE_EXPONENT) \
* (output_max - output_min)

def normalized_rms(values):
minbuf = int(mean(values))
samples_sum = sum(
float(sample - minbuf) * (sample - minbuf)
for sample in values
)

return math.sqrt(samples_sum / len(values))

def mean(values):
return sum(values) / len(values)

def volume_color(volume):
return 200, volume * (255 // NUM_PIXELS), 0

pixels = neopixel.NeoPixel(board.A2, NUM_PIXELS,
brightness=0.1, auto_write=False)
pixels.fill(0)
pixels.show()

mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA,
sample_rate=16000, bit_depth=16)

samples = array.array('H', [0] * NUM_SAMPLES)
mic.record(samples, len(samples))
# Set lowest level to expect, plus a little.
input_floor = normalized_rms(samples) + 10
# OR: used a fixed floor
# input_floor = 50

# You might want to print the input_floor to help adjust other values.
# print(input_floor)

# Corresponds to sensitivity: lower means more pixels light up with lower sound
# Adjust this as you see fit.
input_ceiling = input_floor + 500

peak = 0
while True:
mic.record(samples, len(samples))
magnitude = normalized_rms(samples)
# You might want to print this to see the values.
# print(magnitude)

# Compute scaled logarithmic reading in the range 0 to NUM_PIXELS
c = log_scale(constrain(magnitude, input_floor, input_ceiling),
input_floor, input_ceiling, 0, NUM_PIXELS)

# Light up pixels that are below the scaled and interpolated magnitude.
pixels.fill(0)
for i in range(NUM_PIXELS):
if i < c:
pixels[i] = volume_color(i)
# Light up the peak pixel and animate it slowly dropping.
if c >= peak:
peak = min(c, NUM_PIXELS - 1)
elif peak > 0:
peak = peak - 1
if peak > 0:
pixels[int(peak)] = PEAK_COLOR
pixels.show()

The 2nd code is:
import array
import math
import math
import audiobusio
import board
import neopixel
from busio import I2C

CURVE = 2
SCALE_EXPONENT = math.pow(10, CURVE * -0.1)

PEAK_COLOR = (100, 0, 255)
NUM_PIXELS = 16

NUM_SAMPLES = 160

def constrain(value, floor, ceiling):
return max(floor, min(value, ceiling))

def log_scale(input_value, input_min, input_max, output_min, output_max):
normalized_input_value = (input_value - input_min) / \
(input_max - input_min)
return output_min + \
math.pow(normalized_input_value, SCALE_EXPONENT) \
* (output_max - output_min)

def normalized_rms(values):
minbuf = int(mean(values))
samples_sum = sum(
float(sample - minbuf) * (sample - minbuf)
for sample in values
)

return math.sqrt(samples_sum / len(values))

def mean(values):
return sum(values) / len(values)

def volume_color(volume):
return 200, volume * (255 // NUM_PIXELS), 0

pixels = neopixel.NeoPixel(board.A2, NUM_PIXELS,
brightness=0.1, auto_write=False)
pixels.fill(0)
pixels.show()

mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA,
sample_rate=16000, bit_depth=16)

samples = array.array('H', [0] * NUM_SAMPLES)
mic.record(samples, len(samples))
# Set lowest level to expect, plus a little.
input_floor = normalized_rms(samples) + 10
# OR: used a fixed floor
# input_floor = 50

# You might want to print the input_floor to help adjust other values.
# print(input_floor)

# Corresponds to sensitivity: lower means more pixels light up with lower sound
# Adjust this as you see fit.
input_ceiling = input_floor + 500

peak = 0
while True:
mic.record(samples, len(samples))
magnitude = normalized_rms(samples)
# You might want to print this to see the values.
# print(magnitude)

# Compute scaled logarithmic reading in the range 0 to NUM_PIXELS
c = log_scale(constrain(magnitude, input_floor, input_ceiling),
input_floor, input_ceiling, 0, NUM_PIXELS)

# Light up pixels that are below the scaled and interpolated magnitude.
pixels.fill(0)
for i in range(NUM_PIXELS):
if i < c:
pixels[i] = volume_color(i)
# Light up the peak pixel and animate it slowly dropping.
if c >= peak:
peak = min(c, NUM_PIXELS - 1)
elif peak > 0:
peak = peak - 1
if peak > 0:
pixels[int(peak)] = PEAK_COLOR
pixels.show()

How would i go about combining the codes? Any advice would be greatly appreciated. T

Thanks!

Teedee1

Posts: 8
Joined: Thu Jan 30, 2020 5:45 am

Re: Combining 2 different codes

Copy the code you are getting this error with:
I've been trying to define my sound but cant seem to get it sorted.

I've been using: mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA,
sample_rate=16000, bit_depth=16)

as my mic variable but the serial console returns AttributeError: 'PDMIn' object has no attribute 'value'

When submitting your code you will see a [code] box above the window your are working in.
Press the box and paste your code between the pair of bracketed code words that now appear in the window you are typing in. Then press preview to see what it will look like. When you are satisfied press submit.

FormatCode.PNG (17.31 KiB) Viewed 35 times

V2man

Posts: 703
Joined: Mon Dec 03, 2018 12:38 am