Hello!
I have had an arduino for quite a while now, but I haven't done much coding lately. A week ago I suddenly wanted to make something useful. I had a DIY led strip lightning in my room couple years ago so I decided to built another, a better one for my brother.
So the project was pretty simple although I had some trouble with the code. (Last project with arduino was over a year ago and I can do only simple codes.) Arduino duemilanove with three TIP122 transistors on breadboard, 12V 5A transformer, IR sensor from old TV, nice little remote from old dvd player and total 6 meters of white led strip were used. Wiring was mainly taken from broken decorative lights. Leds are controlled with remote control. So there are three channels, one for leds under the windows, another for leds on top and third channel for another wall. (See the pics below) The room looks pretty cool!
Here is the code:
#include <IRremote.h> // include IR remote library
int RECV_PIN = 8; // define IR detector Pin
IRrecv irrecv(RECV_PIN);
decode_results results; // IR library stuff
int CH[3] = {9, 10, 6}; // PWM pins for Led strip controlled with transistors
int val[3] = {0, 0, 0}; // led brightness 0-255
int flow[3] = {0, 0, 0}; // direction for single channel operations (0 increasing, 1 decreasing)
long int Result; // used to store the IR remote value
int delayval = 10; // speed value used when turning leds on and off
int SignalPin = 13; // single signal led
void setup(){
Serial.begin(19200); // start serial communication
irrecv.enableIRIn(); // Start the receiver
for(int i=0; i<3; i++){ // defines ledpins as outputs
pinMode(CH[i], OUTPUT);
}
pinMode(SignalPin, OUTPUT);
digitalWrite(SignalPin, HIGH); // Flash the signal led once at the start
delay(500);
digitalWrite(SignalPin, LOW);
Serial.println("READY"); // print ready to the serial monitor
}
void loop() {
if (irrecv.decode(&results)) { // search for transmitted signal (?)
if(results.value == 4294967295) {} //When button is pressed for long, first result is the important one and the rest are all the same
else{ //this piece of code discards value 4294967295
Result = results.value;
}
switch (Result) { //switch operation allows check which button is pressed
case 21538:
if(val[0]+val[1]+val[2] ==0){ // If all three channels are off, button turns them on
for(int j=0; j<255; j=j+5){
for(int i=0; i<3; i++){ // controls all three channels
if(val[i] <= 255){ // doesn't affect if channel is already on full brightness
val[i]=val[i]+5;
}
else{}
val[i]=constrain(val[i],0,255); // constrain just in case if something has gone wrong
analogWrite(CH[i],val[i]); // change the actual brightness
}
delay(delayval); // lights turn on and off smoothly
}
}
else{ // if even one channel is on, button turns it off
for(int j=0; j<255; j=j+5){
for(int i=0; i<3; i++){ // same principle as above
if(val[i] >0){
val[i]=val[i]-5;
}
else{}
val[i]=constrain(val[i],0,255);
analogWrite(CH[i],val[i]);
}
delay(10);
}
for(int i=0; i<3; i++){
flow[i]=0; // sets flow variables to increasing, without this single channel buttons could jam the code when used
}
}
Result=0; // clears the message from the ir remote, fixes bugs when different buttons are pushed quickly
break;
case 28962: //these buttons control each channel individually
singleChannel(0); // singleChannel() function at the end of the code
break;
case 31526:
singleChannel(1);
break;
case 26150:
singleChannel(2);
break;
case 9250:
for(int i=0; i<3; i++){ // increases brightness in every channel...
val[i]=val[i]+5;
}
break;
case 25634:
for(int i=0; i<3; i++){ // ...and the same for decreasing
val[i]=val[i]-5;
}
break;
}
Serial.print("channel 1,2,3 values:"); // sends brightness values to the serial monitor
for(int i=0; i<3; i++){ // every time the remote is pressed
Serial.print(val[i]);
Serial.print(" ");
}
Serial.println(" ");
digitalWrite(SignalPin, HIGH); // Flash the signal pin once
delay(1); // to signal that data has been received
digitalWrite(SignalPin, LOW); // from the IR remote
irrecv.resume(); // Receive the next value
}
for(int i=0; i<3; i++){
val[i] = constrain(val[i], 0, 255); // not sure if this is anymore needed
analogWrite(CH[i], val[i]); // updates leds
}
} // loop ends
int singleChannel(int x){ //function for controlling single channel
if(flow[x] == 0 && val[x]<255){ // x defines which channel is controlled
val[x]=val[x]+5; // When single channel button is pressed, brigtness starts
} // slowly increasing. When brightness is at max, it starts
else{ // decreasing. Function stops when led is completely off
val[x]=val[x]-5; // if you stop pushing the button when brightness is decreasing
} // and you press the same button again, effect continues to the same direction
if(val[x] == 255){ // easy to use, hard to explain
flow[x] = 1;
}
if(val[x] == 0){
flow[x] = 0;
analogWrite(CH[x], 0); // set the channel off
Result=0; // again, fixes bugs when remote is used quickly
}
}
I have ordered components for another arduino, because now the casing is not very good. So I'm planning to to solder all components permanently on a single board instead of that mess under the desk. The place and casing for the IR sensor is also a question but I need a little time to think about it.
So what you guys think? It is very simple project, but results are quite nice. Planning to make similar lightnings in other rooms as well. And another thing, since I don't know a lot about coding and I tried to make the code as simple as possible, I would appreciate if you could help me improve the code, how could I make it more simple?
And don't hate if you find some grammar mistakes or something like that, I'm not native english speaker. And sorry about pictures, they are very bad quality.