Took me awhile to get this project to run the way I wanted it to. This is a project that lets me control the LED strip in my room with either a sound-sensor or using bluetooth at the flip of a switch. For this project I am using a RGB LED Strip not an Addressable LED Strip....( I actually wanted to use an addressable LED Strip for this project but accidentally bought the wrong one.....LOL!!!! ) Without further ado, below is the code I used for this project.
CODE:
char color = 0;
const int Sensor = A0;
const int RED = 5;
const int GREEN = 9;
const int BLUE = 3;
int beat = 0;
int redNow;
int blueNow;
int greenNow;
int redNew;
int greenNew;
int blueNew;
void sequence()
{
analogWrite(RED,255);
analogWrite(GREEN,0);
analogWrite(BLUE,0);
delay(500);
analogWrite(RED,255);
analogWrite(GREEN,87);
analogWrite(BLUE,0);
delay(500);
analogWrite(RED,255);
analogWrite(GREEN,255);
analogWrite(BLUE,0);
delay(500);
analogWrite(RED,0);
analogWrite(GREEN,255);
analogWrite(BLUE,0);
delay(500);
analogWrite(RED,0);
analogWrite(GREEN,0);
analogWrite(BLUE,255);
delay(500);
analogWrite(RED,255);
analogWrite(GREEN,0);
analogWrite(BLUE,255);
delay(500);
analogWrite(RED,255);
analogWrite(GREEN,255);
analogWrite(BLUE,255);
delay(500);
}
void setup()
{
Serial.begin(9600);
pinMode(A0, INPUT);
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
redNow = random(255);
greenNow = random(255);
blueNow = random(255);
redNew = redNow;
greenNew = greenNow;
blueNew = blueNow;
}
#define fade(x,y) if (x>y)x--; else if (x<y) x++;
void loop()
{
if (Serial.available()>0)
{
color = Serial.read();
char Rec = char(color);
if (Rec != '0')
{
Serial.println(Rec);
}
}
if (color == 'n') //off
{
analogWrite(RED,0);
analogWrite(GREEN,0);
analogWrite(BLUE,0);
}
if (color == 'w') //white
{
analogWrite(RED, 255);
analogWrite(GREEN, 255);
analogWrite(BLUE, 255);
}
if (color == 'r') //RED
{
analogWrite(RED,255);
analogWrite(GREEN,0);
analogWrite(BLUE,0);
}
if (color == 'g') //GREEN
{
analogWrite(RED,0);
analogWrite(GREEN,255);
analogWrite(BLUE,0);
}
if (color == 'b') //BLUE
{
analogWrite(RED,0);
analogWrite(GREEN,0);
analogWrite(BLUE,255);
}
/* if (color == 'o') //orange
{
analogWrite(RED,255);
analogWrite(GREEN,87);
analogWrite(BLUE,0);
}*/
if (color == 'c') //cyan
{
analogWrite(RED,0);
analogWrite(GREEN,255);
analogWrite(BLUE,255);
}
if (color == 'y') //yellow
{
analogWrite(RED,255);
analogWrite(GREEN,255);
analogWrite(BLUE,0);
}
if (color == 'v') //violet
{
analogWrite(RED,255);
analogWrite(GREEN,0);
analogWrite(BLUE,255);
}
if (color == 's') //sequence**
{
sequence();
}
if (color == 'q') //stop sequence
{
analogWrite(RED,0);
analogWrite(GREEN,0);
analogWrite(BLUE,0);
}
boolean beat = digitalRead(Sensor);
if (digitalRead(Sensor) == '1')
{
beat == HIGH;
}
else
{
beat == LOW;
if (beat == HIGH)
{
analogWrite(BLUE, blueNow);
analogWrite(RED, redNow);
analogWrite(GREEN, greenNow);
redNew = random(255);
greenNew = random(255);
blueNew = random(255);
//fade into new color
while ((redNow != redNew) || (blueNow != blueNew) || (greenNow != greenNew))
{
fade(redNow, redNew);
fade(blueNow, blueNew);
fade(greenNow, greenNew);
analogWrite(BLUE, blueNow);
analogWrite(RED, redNow);
analogWrite(GREEN, greenNow);
delay(1);
}
}
else
{
analogWrite(RED, 0);
analogWrite(GREEN,0);
analogWrite(BLUE, 0);
}
}
}
This code is a combination of two codes that I was able to find online with a little bit of editing to allow the project to run smoothly. It consists of the code for the bluetooth module as well as the sound sensor module. Now for the setup of the actual project.
( Bear with me here.....I know its a little messy... )
The LED Strip comes with 4 wires, (red, green, blue, black). The black wire is connected to the positive of the power supply. The other 3 wires are each connected to a TIP31C transistor which is then connected to the Arduino. I also used a toggle switch to connect the sound sensor module and the bluetooth module to the 5V pin on the Arduino. The toggle switch will allow me to switch between bluetooth-mode or music-mode. I am also using a 24V 6.25A switching power supply for this particular build as the LED requires an input power of 144W and 150W is the closest that i can find. Final outlook looks something like this...
Next I had to create an app for the bluetooth so that I can control the lights with my phone. Icreated the app using the MIT App Inventor i found online. The App Inventor is very user friendly and I would totally recommend people to use it if they want to create simple applications.
After creating the app, next is to test if everything is working as correctly.
Now that everything is running smoothly, next is to create a casing so that I can mount it on the wall in my room. I decided to use acrylic for the casing. ( The acrylic cracked while I was drilling a hole on it, thus the huge crack....) So here is the final outcome. ENJOY!!!!
I really enjoyed making this project. Really lights up my room ... :D
コメント