Tinkering with microcontrollers has been on my “to try” list for quite some time. I recently stumbled upon Creation Crate. Creation Crate is a monthly tech education subscription box. Each month you receive a Arduino UNO R3 and parts to complete a project. The instruction book includes the source code for the project. The source code is licensed under the GNU General Public License. Each month the projects increase in difficulty. I thought Creation Crate would be a wonderful way to get my feet with wet with Arduino and programming electronics.
I was on the fence about signing up for the subscription box because I was not sure what to expect. I intend to write a blog post for each Creation Crate I receive in case there is a reader out there on the fence like I was.
The Project
The project for month one is creating a mood lamp! The mood lamp changes colors and only turns on when it’s dark.
What’s in the Box
Inside of month one’s box we have:
- Arduino UNO R3
- Breadboard
- USB Cable
- Chinese Paper Latern
- U-shaped Jumper wires
- Regular Jumper wires
- 2.2k OHM Resistor
- a blue, red and green LED
- Light Dependent Resistor (LDR)
Building the Hardware
The setting up the hardware was pretty simple. The instruction book includes nice illustrations for connecting the hardware together.
Programming
Source code for the project is included in the instruction book. Typing the sketch file was pretty straight forward. I did make one significant error when I tried to upload the sketch on the Arduino: I selected the wrong port in the Arduino IDE. I was stuck on this error for more time than I care to admit. (Doh! Never again!)
// License: GNU General Public License | |
// Creation Crate: Month 1 - Mood Lamp | |
int pulseSpeed = 1; | |
int ldrPin = 0; // LDR (Light Dependent Resistor) in Analog 0 | |
int redLed = 11; | |
int greenLed = 10; | |
int blueLed = 9; | |
int ambientLight; // light value in the room | |
int power = 150; | |
float RGB[3]; | |
float CommonMathVariable = 180/PI; | |
void setup() { | |
// put your setup code here, to run once: | |
// tells the UNO R3 to send data out to the LEDs | |
pinMode(redLed, OUTPUT); | |
pinMode(greenLed, OUTPUT); | |
pinMode(blueLed, OUTPUT); | |
// sets all the outputs to LOW (off) | |
digitalWrite(redLed, LOW); | |
digitalWrite(greenLed, LOW); | |
digitalWrite(blueLed, LOW); | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
for(float x = 0; x < PI; x = x + -.00001) { | |
// red LED | |
RGB[0] = power * abs(sin(x * (CommonMathVariable))); | |
// green LED | |
RGB[1] = power * abs(sin((x + PI/3) * (CommonMathVariable))); | |
// blue LED | |
RGB[2] = power * abs(sin((x + (2 * PI) / 3) * (CommonMathVariable))); | |
ambientLight = analogRead(ldrPin); | |
if(ambientLight > 600) { | |
analogWrite(redLed, RGB[0]); | |
analogWrite(greenLed, RGB[1]); | |
analogWrite(blueLed, RGB[2]); | |
} | |
else { | |
digitalWrite(redLed, LOW); | |
digitalWrite(greenLed, LOW); | |
digitalWrite(blueLed, LOW); | |
} | |
// Calculate the delay for each color depending on color | |
// brightness; brighter LEDs will change colour slower | |
for(int i = 0; i < 3; i++) { | |
if(RGB[i] < 1) { | |
delay(20 * pulseSpeed); | |
} | |
else if (RGB[i] < 5) { | |
delay(10 * pulseSpeed); | |
} | |
else if (RGB[i] < 10) { | |
delay(2 * pulseSpeed); | |
} | |
else if (RGB[i] < 100) { | |
delay(1 * pulseSpeed); | |
} | |
else {} | |
} | |
delay(1); | |
} | |
} |
Final Thoughts
This was my first experience programming an Arduino board and I loved it! The instruction book leaves out a lot of beginner details like, “what’s a resistor” or explaining the parts that make up an Arduino board, but I think that’s okay. When I finished the project I was finally motivated to sit down and start reading my Getting Started with Arduino book.