0

Color Sensor RGB Values to Sound Output.
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Color Sensor RGB Values to Sound Output.

by cpl340 on Fri Apr 23, 2021 8:36 pm

Code: Select all | TOGGLE FULL SIZE
HI all!

I am new to coding/using the circuit playground express, and I chose a pretty ambitious project for my first attempt on accident. I have been working on converting RGB values from the light sensor into sound output, but I have been having a hard time making it update the sound when new color information comes in. Best case scenario, I would want the tone output to be continuous and change when the color sensor senses a new color. Right now the code I have doesn't change the tone when I change the color in front of it, but it is continuous. For the continuous tone I have been using
Code: Select all | TOGGLE FULL SIZE
cp.start_tone()
, and when I use play
Code: Select all | TOGGLE FULL SIZE
cp.play_tone(frequency, time)
it plays for the time I want, and then updates with the next color sensor information. The only problem with the second version is that it stops playing the tone before starting the new one, so it isn't a continuous tone. Is there a way for me to have the tone be continuous and update with the color information?

Here is the code I have right now (some of it edited from Adafruit basic color sensing tutorial):

Code: Select all | TOGGLE FULL SIZE
import analogio
import board
import neopixel
import time
from adafruit_circuitplayground import cp

while True:
    cp.pixels.fill((0, 0, 0))
    cp.pixels[1] = (255, 0, 0)
    raw_red = cp.light
    red = int(raw_red * (255 / 65535))
   
    cp.pixels[1] = (0, 255, 0)
    raw_green = cp.light
    green = int(raw_green * (255 / 65535))
   
    cp.pixels[1] = (0, 0, 255)
    raw_blue = cp.light
    blue = int(raw_blue * (255 / 65535))
   
    cp.pixels.fill((0, 0, 0))
 
    # Printed to match the color lines on the Mu plotter!
    # The orange line represents red.
    print((green, blue, red))
   

    if green and red and blue == 0:
        cp.start_tone(1000)
    if green and blue > 0:
        cp.start_tone(650)
    if green and red > 0:
        cp.start_tone(600)
    if blue and red > 0:
        cp.start_tone(400)
    if red > 0:
        cp.start_tone(700)
    if green > 0:
        cp.start_tone(550)
    if blue > 0:
        cp.start_tone(600)

cpl340
 
Posts: 1
Joined: Fri Apr 23, 2021 8:26 pm

Please be positive and constructive with your questions and comments.