Welcome Back
Welcome! In this post I will present the base pseudo code for the SensorXplorer board, and the simple python program (running on raspberry pi) that will allow me to play music based on the information the SensorXplorer is reading from the color sensor.
SensorXplorer Pseudo Code
Pseudo code is a fancy way of saying "Code in English" and is great for planning programs before writing them in real code. Since I currently don't have the board or sensors, I am going to use the specs from the datasheets to build the pseudo code programs. Let's focus on the color sensor first, as this is the most complicated part.
Color Sensor:
- Start by getting the values from the color sensor channels R, G, & B
- Check what range each is in by dividing them into two groups, High & Low. The low range is < 2/3rds of the max detection, High range is > 2/3rds max detection. (Note: this may change depending on later adjustments)
- Send this info to the raspberry pi by setting the correct pins to either high (3.3v) or low (0v).
- Repeat every 60 seconds, as most songs are not shorter than this.
Backup Sensor:
- Start by getting the proximity data from the sensor.
- Since the range of the proximity sensor is 500mm - 0mm, I am going to divide it into three ranges: Far = 500mm - 332mm, Medium = 332 - 166, Close = 166 - 0mm. Store this in a variable.
- Depending upon the distance send pulses to the buzzer. Far = 0 pulses/repetition, Medium = 1 pulse/repetition, Close = 2 pulses/repetition.
- Repeat every 2 seconds.
As you can see, planning a program using pseudo code is a good way to break down the program into easily understandable parts. I plan to write out the actual code for the SensorXplorer next post.
Raspberry Pi ColorSound Program
In the last few posts, I decided to use the color sensor from the kit to play songs according to the "mood" or most present color. Music taste is so diverse, so I am setting my program to look for folders labeled: R, G, B, Y, C, and W. (Magenta doesn't really appear in broad scale in nature, so I am not going to have a folder for that.) This allows the user to put whatever songs they believe fits best in the folders.
Here is my first draft of the python program:
import os import random import RPi.GPIO as io import time from mutagen.mp3 import MP3 #paths for files on USB (usb mounts here) pathR="/media/usb/R" pathG="/media/usb/G" pathB="/media/usb/B" pathC="/media/usb/C" pathY="/media/usb/Y" pathW="/media/usb/W" # called when SensorXplorer sets redpin high def redswitch(): print("red") if os.path.exists(pathR): files=os.listdir(pathR) d=os.path.join(pathR,random.choice(files)) duration = MP3(d).info.length os.system("omxplayer "+d) time.sleep(duration) # called when SensorXplorer sets bluepin high def blueswitch(): print("blue") if os.path.exists(pathB): files=os.listdir(pathB) d=os.path.join(pathB,random.choice(files)) duration = MP3(d).info.length os.system("omxplayer "+d) time.sleep(duration) # called when SensorXplorer sets greenpin high def greenswitch(): print("green") if os.path.exists(pathG): files=os.listdir(pathG) d=os.path.join(pathG,random.choice(files)) duration = MP3(d).info.length os.system("omxplayer "+d) time.sleep(duration) #called when SensorXplorer sets greenpin and bluepin high def cyanswitch(): print("cyan") if os.path.exists(pathC): files=os.listdir(pathC) d=os.path.join(pathC,random.choice(files)) duration = MP3(d).info.length os.system("omxplayer "+d) time.sleep(duration) #called when SensorXplorer sets redpin and greenpin high def yellowswitch(): print("yellow") if os.path.exists(pathY): files=os.listdir(pathY) d=os.path.join(pathY,random.choice(files)) duration = MP3(d).info.length os.system("omxplayer "+d) time.sleep(duration) #called when SensorXplorer sets all pins high def whiteswitch(): print("white") if os.path.exists(pathW): files=os.listdir(pathW) d=os.path.join(pathW,random.choice(files)) duration = MP3(d).info.length os.system("omxplayer "+d) time.sleep(duration) #init pins io.setmode(io.BCM) io.setup(9,io.IN) io.setup(10,io.IN) io.setup(11,io.IN) #start looking for states while True: rstate = io.input(9) gstate = io.input(10) bstate = io.input(11) print(str(rstate)+str(gstate)+str(bstate)) #play a song depending on the color if (rstate == 1 and bstate==0 and gstate==0): redswitch() elif (rstate == 0 and bstate==1 and gstate==0): blueswitch() elif (rstate == 0 and bstate==0 and gstate==1): greenswitch() elif (rstate == 0 and bstate==1 and gstate==1): cyanswitch() elif (rstate == 1 and bstate==0 and gstate==1): yellowswitch() elif (rstate == 1 and bstate==1 and gstate==1): whiteswitch() else: print("No input")
The program first imports all the modules required, and then defines the paths to the folders, along with the functions that will be called when the color is updated. Each of these randomly picks a music file from the proper folder, saves its duration time, and then plays it. It then sleeps for the duration of that song, looping after that song has finished. My second draft will include an on/off switch for the program, and I will also have a live demonstration.
Thanks For Reading
Thank you for reading my post. If you liked it and want to know more about the beginning of this project, I suggest reading my previous blogs. Hope you have a great day.