Description:
A lamp constructed from components from a plastic molecule building kit and crumpled cellophane. Light is directed through the cellophane, which contains semi opaque white and pink patterns. The intention was to create a When running the code below, the lamp may be set to one of five colors by keyboard input.
Components
1 Arduino Uno
1 Breadboard
1 Red LED
1 Blue LED
1 Green LED
1 USB Cable
7 wires
3 220 Ohm Resistors
Code:
/* Code enables user to control the intensity of 3 LEDs, red, green, and blue
using Pulse Width Modulation and keyboard input*/
//Adapted from sample code - 2013, Kristine Yoshihara, infoc262
// output
int redPin = 9;
int greenPin = 10;
int bluePin = 11;
//variables
int redLevel = 127;
int greenLevel = 127;
int blueLevel = 127;
int i = 0;
int wait = 50;
int DEBUG = 0;
char serInString[100];
char color;
void setup(){
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
analogWrite(redPin, redLevel);
analogWrite(greenPin, greenLevel);
analogWrite(bluePin, blueLevel);
Serial.println("Colors may be controlled with your keyboard. Press R for red, O for orange, G for green, B for blue, or P for purple");
}
// Main program
void loop()
{
readSerialString(serInString, 100);
color = serInString[0];
if (color =='r' || color =='R') {
redLevel = 255;
greenLevel = 0;
blueLevel = 0;
}
else if (color =='o' || color =='O'){
redLevel = 200;
greenLevel = 127;
blueLevel = 0;
}
else if (color =='g' || color =='G'){
redLevel = 0;
greenLevel = 255;
blueLevel = 0;
}
else if (color =='b' || color =='B'){
redLevel = 0;
greenLevel = 0;
blueLevel = 255;
}
else if (color =='p' || color =='P'){
redLevel = 127;
greenLevel = 0;
blueLevel = 127;
}
else {
redLevel = 127;
greenLevel = 127;
blueLevel = 127;
}
delay(100); //wait for serial data
analogWrite(redPin, redLevel); // Write current values to LED pins
analogWrite(greenPin, greenLevel);
analogWrite(bluePin, blueLevel);
if (DEBUG) { // If we want to read the output
DEBUG += 1; // Increment the DEBUG counter
if (DEBUG > 10) // Print every 10 loops
{
DEBUG = 1; // Reset the counter
Serial.print(i); // Serial commands in 0004 style
Serial.print("\t"); // Print a tab
Serial.print("R:"); // Indicate that output is red value
Serial.print(redLevel); // Print red value
Serial.print("\t"); // Print a tab
Serial.print("G:"); // Repeat for green and blue...
Serial.print(greenLevel);
Serial.print("\t");
Serial.print("B:");
Serial.println(blueLevel); // println, to end with a carriage return
}
}
delay(wait); // Pause for 'wait' milliseconds before resuming the loop
}
//reads and stores string from serial port
void readSerialString(char *strArray, int maxLen){
int i = 0;
if (!Serial.available()){
return;
}
while (Serial.available() && i < maxLen){
strArray[i] = Serial.read();
i++;
}
}
//resets
void resetSerialString (char *strArray, int length) {
for (int i = 0; i < length; i++) {
strArray[i] = '\0';
}
}
- Login to post comments