RGB Mood Light – Arduino Project

June~September 2012

Introduction

I like shiny things. Shiny things are nice, pretty, and super mesmerizing. Last summer, before playing around with my Carrotlights, I had my first go at playing around with LEDs. I set out to make a RGB Mood Light, a simple project supposedly for beginners: three LEDs would each fade in and out, shifting the color of the moodlight gradually and beautifully. With the help of friends and instructables, my project began!

I. Hello, Arduino!

I don’t have any programming experience whatsoever – I once tried to sign up for an intro programming class at school, but class conflicts forced me to drop it. Any programming language is just as foreign to me as Swahili or German. Nevertheless, Dan convinced me that programming an Arduino wouldn’t be as impossible as I imagined it to be. So, I gave it a shot.

File>examples>basics>blink

These were my first couple of clicks on arduino.exe. Clicking a green arrow to upload the simple program onto my arduino, hooked onto a breadboard and an LED, was absolutely the coolest thing ever. Straightaway the LED was blinking red like a cylon. I messed around for a while, trying out a bunch of different example programs. In the end, ‘blink’ and ‘fade’ became my templates.

I made three programs total for my to-be mood light: “RGB_fade,” “RGB_strobe,” and “heartbeat”

The first two names speak for their functions, I think. The last one with the horribly cheesy name was based off ‘blink’. It used only the red LED which, first blinks slow, then faster and faster and faster like an excited heart :) It was the simplest of the three, but I had fun with it – when recording the video, I cut out a little paper heart and put it on the diffuser, so ‘heartbeat’ actually had a heart outline. Here’s my program:

Heartbeat

#define delayOne 150
volatile int delayTwo = 200;
volatile int delayThree = 1500;

>> Here, I used ‘volatile’ since I was going to change around the tempo of the beats later on. I set the initial delays to emulate what a real heartbeat might look like – the ratios are really inexact, but when tested with the LED, the timing looked convincing enough!

int red = 9;

void setup() {
pinMode(red, OUTPUT);
}

>only using pin 9 which is hooked to the red LED. Nice and simple :)

void loop() {
digitalWrite(red, HIGH);
delay(delayOne);
digitalWrite(red, LOW);
delay(delayTwo);
digitalWrite(red, HIGH);
delay(delayOne);
digitalWrite(red, LOW);
delay(delayThree);

>> Here comes the infinite loop of heartbeats, trying to mimic systoles. Since the rhythm of heartbeats is STRONGweak, I set the first and third blinks to high and the other two to low. The delays were as defined above.

delayTwo -= 10;
if(delayTwo < 100) {delayTwo = 100;}
delayThree -= 45;
if(delayThree < 500) {delayThree = 500;}
}

>>After going round the loop once, the minus equals took care of the artificial heart getting excited. delayTwo and delayThree would be shortened little by little until they each reach their min. delays, 100 and 500, respectively.

RGB_Strobe

Nothin like a makeshift diffuser from paper towels!

Once this cute little job with anxious, nervous, love-struck hearts was over, I got onto strobe-mode. ‘RGB_Strobe’ was also based off ‘blink’, the handy dandy example program. My goal in this one was to get a steady light that changed color at each blink.

int green = 11;
int blue = 10;
int red = 9;

pinMode(green, OUTPUT); 
pinMode(blue, OUTPUT);
pinMode(red, OUTPUT);
}

>>This time I used all three LEDs, RGB!

#define delayTime 240

>>Here, the delay is 240 but I played around with it a lot – too little delay resulted in psychotic, out-of-control strobing though. I found a delay from 150-250 to be appropriate.

void loop() {
digitalWrite(green, HIGH); 
delay(delayTime); 
digitalWrite(red, LOW);
delay(delayTime);
digitalWrite(blue, HIGH);
delay(delayTime); 
digitalWrite(green, LOW);
delay(delayTime);
digitalWrite(red, HIGH);
delay(delayTime);
digitalWrite(blue, LOW);
delay(delayTime);
digitalWrite(blue, HIGH);
digitalWrite(green, HIGH);
delay(delayTime);
digitalWrite(blue, LOW);
}

>>I experimented a bunch here, trying different combinations of the three LEDs in HIGH or LOW brightness, each followed by the same delay time. Turning on a green, then a blue made aquamarine, and then turning off the green gave me blue, adding red gave me purple, so on and so forth.

RGB_Fade

This was my actual mission from the beginning – beautiful everchanging shiny lights that draw you in for hours and hours and hours… Looking at it now, the program is easier to understand, but trying to digest and define so many new functions in my head at once was the most baffling thing. But the result was so satisfying :)

#define green 11
#define blue 10
#define red 9
#define delayTime 25

void setRGB(int, int, int);

int greenState = 127;
int blueState = 255;
int redState = 0;

>> Three LEDs again. Initially, Blue is at max brightness, red is at min. Green is intermediate.

boolean greenUp = true;
boolean blueUp = false;
boolean redUp = true;

>> So naturally, blue’s brightness will have to go down while the other two go up. Thus, true, false, true for green, blue, and red, respectively.

void setup () {
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
pinMode(red, OUTPUT);
setRGB(redState, greenState, blueState);
}

void loop () {
if(greenUp) {
if(greenState >= 255) {greenUp = false;}
else {greenState++;}
}
else {
if(greenState <= 0) {greenUp = true;}
else {greenState–;}
}
>>Loop again! I used if and else directions within if and else directions to guide the lights. The one right above says that if green is currently getting brighter, and its brightness hasn’t reached max(255) yet, then keep increasing(++). And if green is at min(0), then make the brightness increase. If it isn’t at 0, then keep decreasing brightness.

if(blueUp) {
if(blueState >= 255) {blueUp = false;}
else {blueState++;}
}
else {
if(blueState <= 0) {blueUp = true;}
else {blueState–;}
}

if(redUp) {
if(redState >= 255) {redUp = false;}
else {redState++;}
}
else {
if(redState <= 0) {redUp = true;}
else {redState–;}
}

>>This is repeated for blue and red, but the three LEDs are all out of sync since their starting points were different. The result? Pretty lights!

II. Arduino + Breadboard = Magic!

III. Making the Circuit Board

Now that I had all of the programming I needed, I could get onto making an actual mood light that is not on a bread board. I used EAGLE to design the board.

Schematic

The hardest part was probably making the schematic – unfamiliar abbreviations, so many pins on the atmega, wires criss-crossing everywhere.. Once the schematic was done though, I had fun routing paths on the board since it was like puzzles. Connecting here and there making sure there aren’t overlaps in the paths, rat’s nest. More connections, rat’s nest. Rat’s nest, rat’s nest, rat’s nest. It was so exciting to see that my board was starting to look neat like an actual circuit board!

Tadaaaa. There it was: a beautiful, geometric circuit board of green, blue, and black. I printed it at MITers, stuck it in some ferric chloride, then began soldering. I’m still very new to soldering, and I’m awfully slow at it, but I like shiny things and solder is shiny, so naturally we got along. (I ended up soldering almost the entire board to make it look nice >_> )

IV. Fixing 

When I was done soldering everything, my friend and I wanted to get the program in it right away, but for some reason, the board wasn’t letting us. It turns out, my first go at eagling wasn’t so perfect, after all. Two traces were overlapping on my shiny board :(

 

I hope it’s not too hard to tell, but the thin blue trace overlaps with the second pin from the right. Should’ve DRC’d! The design rule check gave me a bunch of clearance errors(which I don’t care too much for) and one overlap right there. Here’s the fixed one.

You can see that now, the trace goes smack in between the ATmega8 pins instead of on them. This would’ve fixed the board.

But since I didn’t want to make a new board after all the soldering and the drill pressing, we added just a jumper to replace one of the bad traces.

Less shiny, more functiony!

Voila! Problem solved.

V. AVR Studio

Only one thing remained to be done – I was done with all the hard stuff.

Using Atmel Studio, I programmed the ATmega8 with RGB_Fade. Couple of clicks later, the circuit board was behaving just like the breadboard :)

I found a switch, a battery, and a battery holder, and added them on – it doesn’t look elegant, but it works. Once I have time, I’m definitely going to try for a mood light that can plug into AC power plugs instead of using batteries.

Off

And on!

VI. Final Product – First Arduino Project, RGB Mood Light!

Tada!

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s