Bird – RGB lights mapped to Sonar

I put the RGB LED into the bird and controlled its colour output using the incoming Sonar values. The Sonar values were also translated mapped onto tone values played through a speaker.

Each of three RGB pins for the common anode LED were given mapped values between 0-255. The ratio of RGB values was correlated with distance values (0-20 cm) obtained by the Sonar distance sensor. (See code below for how the ratio of values were mapped and calculated).

 

 

Here’s a couple images of my wiring

IMG_0146

 

IMG_0141

Below is the CODE I used if you wanna see how I controlled the RGB LED and mapped the sounds, distance, colours onto each other.
// For RGB LED
int redPin = 11;
int greenPin = 10;
int bluePin = 9;
int micPin = 0;
int micVal = 0;
int sampleRate = 500;
long lastChange = 0;

// For Ping Sensor
#include <NewPing.h>
#define TRIGGER_PIN 6 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 5 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 40 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

// For Speaker Output
#include <NewTone.h>
#define TONE_PIN 8
int speakerRate = 1000;
int minFreq = 250; // minimum and maximum frequencies
int maxFreq = 2000;

/*uncomment this line if using a Common Anode LED */
#define COMMON_ANODE

void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);

Serial.begin(115200);
}

void loop() {
// PING
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
Serial.print(“Ping: “);
int distance = uS / US_ROUNDTRIP_CM;
Serial.print(distance); // Convert ping time to distance in cm and print result (0 = outside set distance range)
Serial.println(“cm”);

int distanceScaled = map(distance, 0, MAX_DISTANCE, 0, 100); // Normalize ping distance values

// RGB Values calculated from distanceScaled
int red = getRed(distanceScaled);
int green = getGreen(distanceScaled);
int blue = getBlue(distanceScaled);

// Output Tone
if(distanceScaled > 0) {
int freq = map(distanceScaled, 0, 100, 1500, 250);
NewTone(TONE_PIN, freq);
delay(1);
noNewTone(TONE_PIN);
}

if((millis()-lastChange>=speakerRate) && (distanceScaled > 0)) {
int freq = map(distanceScaled, 0, 100, maxFreq, minFreq);
NewTone(TONE_PIN, freq);
delay(5);
noNewTone(TONE_PIN);
lastChange=millis();
}

setColor(blue,red,green); // red delay(1000);
}

void setColor(int red, int green, int blue) {
#ifdef COMMON_ANODE
red = 255 – red;
green = 255 – green;
blue = 255 – blue;
#endif
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}

int getRed(int distance) {
if((distance > 0) && (distance < 26)) {
int red = map(distance, 1,25, 0,255);
return red;
}
else if((distance > 25) && (distance < 51)) {
int red = map(distance, 26, 50, 255, 0); return red;
}
else{int red = 0; return red;}
}

int getGreen(int distance) {
if((distance > 25) && (distance < 51)) {
int green = map(distance, 25,50, 0,255);
return green;
}
else if((distance > 50) && (distance < 76)) {
int green = map(distance, 26, 50, 255, 0); return green;
}
else{int green = 0; return green;}
}

int getBlue(int distance) {
if((distance > 49) && (distance < 76)) {
int blue = map(distance, 50,75, 0,255);
return blue;
}
else if((distance > 75) && (distance < 101)) {
int blue = map(distance, 76, 100, 255, 0); return blue;
}
else{int blue = 0; return blue;}
}