Welcome
Hello everyone and welcome back! This week I will explain my code for the two sensors, along with a video demonstration of the color sound program in action.
Sensor Prototype Code with Arduino
For this project I will be using an Arduino Uno that I happen to have lying around. Here is the code I used in my prototype:
//Color sensor: //1. Start by getting the values from the color sensor channels R, G, & B //2. 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) //3. Send this info to the raspberry pi by setting the correct pins to either high (3.3v) or low (0v). //4. Repeat every 60 seconds, as most songs are not shorter than this. //Backup sensor //1.Start by getting the proximity data from the sensor. //2.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. //3.Depending upon the distance send pulses to the buzzer. Far = 0 pulses/repetition, Medium = 1 pulse/repetition, Close = 2 pulses/repetition. //4. Repeat every 2 seconds. //Pins to output color logic const int redpin = 5; const int greenpin = 6; const int bluepin = 7; //Pin for buzzer const int buzzpin = 9; //Uses this as an example of distance. I will have it loop through distances as if an object is back and forth in front of it. It starts out far away, at 500 mm. int distance = 500; bool incrementing = false; void setup() { //for debug Serial.begin(115200); //acts as color sensor, uses analog pin 0's floating values to seed randomSeed(analogRead(0)); //set up pins to control music playing pinMode(redpin,OUTPUT); pinMode(greenpin,OUTPUT); pinMode(bluepin,OUTPUT); //set up pin for buzzer pinMode(buzzpin,OUTPUT); } void loop() { //Color Sensor Logic //--I will change this to read info from the color sensor once I get them, currently I will set each color value to a random number within 0 to 255 to act as a color sensor-- int Rval = random(255); int Gval = random(255); int Bval = random(255); //debug 4 colors Serial.println("R: "+String(Rval)); Serial.println("G: "+String(Gval)); Serial.println("B: "+String(Bval)); //define variables bool Rhigh = false; bool Ghigh = false; bool Bhigh = false; //find what range each color is in if (Rval >= 127) {Rhigh = true;} //100 is a little under 1/2 of 255, so anything higher then it is set high. This number may change once I test the color sensor if (Gval >= 127) {Ghigh = true;} if (Bval >= 127) {Bhigh = true;} //debug for asessment Serial.println("Rpin: "+String(Rhigh)); Serial.println("Gpin: "+String(Ghigh)); Serial.println("Bpin: "+String(Bhigh)); //relate it to pins (the raspberry pi reads these and relates them to the color, so we dont need to do that) This does set the pins to 5v on high, so we will have to use a digitalWrite(redpin,Rhigh); digitalWrite(greenpin,Ghigh); digitalWrite(bluepin,Bhigh); //Backup Sensor Logic //update distance, this will change to a reading from the proximkty sensor when I get that. UpdateDistance(20); //debug distance Serial.println(distance); if(distance >= 332) { //Far: Buzz 0 times/2 seconds noTone(buzzpin); delay(1000); } else if(distance >= 166) { //Medium: Buzz 1 time/2 seconds tone(buzzpin, 256); //on 1 sec delay(1000); noTone(buzzpin);//off 1 second delay(1000); } else { //Close: 2 times/2 seconds ( tone(buzzpin, 256); //on 1/2 second delay(500); noTone(buzzpin);//off 1/2 second delay(500); tone(buzzpin,256);//on 1/2 second delay(500); noTone(buzzpin);//off 1/2 second delay(500); } Serial.println(); } //This just decreases 'distance' from 500 to zero and back. Increases/Decreases by countincrease each time called. void UpdateDistance(int countincrease) { //when we are counting down and above zero, count down if(distance > 0 && !incrementing){ distance -= countincrease; } else if(distance < 500 && incrementing){ distance += countincrease; } //when we reach 500, start counting down else if(distance >= 500 && incrementing){ distance -= countincrease; incrementing = false; } //when we reach zero, start counting up else if(distance <= 0){ distance += countincrease; incrementing = true; } }
As you can see in the code, the program is currently generating three random values for R, G, and B in the range of 0-255 on line 44-46, and for the backup sensor I am just using the variable distance and having it bounce from 0 to 500 and back. This will be changed when I receive the sensor color sensor and proximity sensor to where it reads data from them and updates these values. For now, this works as an example, as most of this code processing that data will remain the same. As promised here is the final draft of ColorSound python code, and a video of the Arduino and Raspberry pi working together to play a music file:
import os import random import RPi.GPIO as io import time #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)) os.system("omxplayer "+d) # 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)) os.system("omxplayer "+d) # 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)) os.system("omxplayer "+d) #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)) os.system("omxplayer "+d) #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)) os.system("omxplayer "+d) #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)) os.system("omxplayer "+d) #init pins io.setmode(io.BCM) io.setup(9,io.IN) io.setup(10,io.IN) io.setup(11,io.IN) #To show raspberry pi is ready to start playing music, turn LED on io.setup(24,io.OUT) io.output(24,1) #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") time.sleep(0.5)
The music player I am using is OMXPlayer and had to be installed separately. When called to play a song it automatically pauses the python program, therefore I have no need to do so in the python program itself.
I apologize for the sound quality. As you saw, the raspberry pi I am using is a rather old one, so it takes a bit to start up. I decided to combat this by using a red LED to show when the python program has started and is listening for information from the Arduino Uno. It works! Note: the Arduino's logic level for its digital pins is 5v, whereas anything over 3.3v on the raspberry pi's gpio pins will cause damage, so I am using a cheap Logic Level Converter. A voltage divider with the appropriate resistor values would also work well, I just happened to have a few converters lying around.
Shipping Update
Recently I have been contacted and told that Newark is going to send me a kit to my correct address. I also have gotten an email from UPS saying the kit is arriving today, Nov 3rd, so I decided to post this today and post an unboxing tomorrow, if I receive the kit.
Thanks For Reading!
Next post (After the unboxing post) Will be about the Arduino, Sensors, and outputs (Buzzer and Music) all being wired together. Final Arduino code will be posted along with a wiring diagram. LVD circuit updates have been disregarded for the time being as I would really like to spotlight the sensors from Vishay. Thank you for reading and stay creative!