element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Summer of Sensors Design Challenge
  • Challenges & Projects
  • Design Challenges
  • Summer of Sensors Design Challenge
  • More
  • Cancel
Summer of Sensors Design Challenge
Blog Phase 2: Research and design - Pseudo code for SensorXplorer Board and Programming Raspberry Pi
  • Blog
  • Forum
  • Documents
  • Design Challenge
  • Leaderboard
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Summer of Sensors Design Challenge to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: DeltaPi
  • Date Created: 21 Oct 2022 6:29 PM Date Created
  • Views 1180 views
  • Likes 7 likes
  • Comments 3 comments
  • vishay
  • color sensor
  • Golfcart
  • Summer of Sensors Design Challenge
  • proximity sensor
  • SensorXploror
Related
Recommended

Phase 2: Research and design - Pseudo code for SensorXplorer Board and Programming Raspberry Pi

DeltaPi
DeltaPi
21 Oct 2022

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.

  • Sign in to reply
  • DeltaPi
    DeltaPi over 2 years ago in reply to osjef

    Thanks. I hope they come soon, but if not I'll do all I can.in the project. I liked your video on the sensors, it has been helpful in seeing how they work. 

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • osjef
    osjef over 2 years ago

    As much as we love to jump into playing with the software immediately, I agree with you that it is always well worth taking the time to design it first with diagrams and pseudo code.

    Shame you not got the sensors yet Disappointed

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • DeltaPi
    DeltaPi over 2 years ago

    Note: Slight edit to raspberry pi code, instead of 'os.startfile' it now uses omxplayer, and properly joins the path of the folder with the chosen music file.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube