Description:
In this lab, we used pulse length modulation in order to simulate the dimming and brightening of the LED lights. The LEDs were placed close together in order to allow them to blend together better in the diffuser. The code was was modified to brighten the light-- the input letters correspond to the colours, with g bring green, r being red, and b blue. Entering the letters increase the wattage by 25.5 every time. This makes the lights seem brighter until it reaches it's max of 255, after which it resets to the dimmest power.
For my diffuser, I used a folded piece of paper, coffee filter, and created a totoro embroidery to light up.
Components Used:
1-Arduino Uno
1-Breadboard
1-USB cable
3-LEDs; red, blue, green
3-220Ω resistors
7-Jumper Wires
cloth
thread
buttons
Code:
char serInString[100]; // array that will hold the different bytes of the string. 100=100characters;
// -> you must state how long the array will be else it won't work properly
char colorCode;
int colorVal;
int redPin = 10; // Red LED, connected to digital pin 9
int greenPin = 11; // Green LED, connected to digital pin 10
int bluePin = 9; // Blue LED, connected to digital pin 11
int redColorVal= 0; // setting intial color values
int greenColorVal = 0;
int blueColorVal = 0;
void setup() {
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
analogWrite(redPin, 0); // set them all to mid brightness
analogWrite(greenPin, 0); // set them all to mid brightness
analogWrite(bluePin, 0); // set them all to mid brightness
Serial.println("enter 'off' to turn off");
Serial.println("enter color combination in rgb (e.g. 'rrrggggb') ");
}
void loop () {
// clear the string
memset(serInString, 0, 100);
//read the serial port and create a string out of what you read
readSerialString(serInString);
// colorCode = serInString[0];
if(strcmp(serInString, "off") == 0){
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
memset(serInString, 0, 100);
}
for(int i = 0; i <= sizeof(serInString); i++){
colorCode = serInString[i];
if (colorCode == 'r'){
if (redColorVal+25.5 >= 256){
redColorVal = redColorVal+25.5;
redColorVal = redColorVal%256;
Serial.print("Red is");
Serial.println(redColorVal);
}
else{
redColorVal = redColorVal +25.5;
}
analogWrite(redPin, redColorVal);
Serial.print("Setting Red to ");
Serial.print(redColorVal);
Serial.println();
}
if (colorCode == 'g'){
if (greenColorVal+25.5 >= 256){
greenColorVal = greenColorVal+25.5;
greenColorVal = greenColorVal%256;
Serial.print("Green is ");
Serial.println(greenColorVal);
}
else{
greenColorVal = greenColorVal +25.5;
}
analogWrite(greenPin, greenColorVal);
Serial.print("Setting Green to ");
Serial.print(greenColorVal);
Serial.println();
}
if (colorCode == 'b'){
if (blueColorVal+25.5 >= 256){
blueColorVal = blueColorVal+25.5;
blueColorVal = blueColorVal%256;
Serial.print("Blue is ");
Serial.println(blueColorVal);
}
else{
blueColorVal = blueColorVal +25.5;
}
analogWrite(bluePin, blueColorVal);
Serial.print("Setting Blue to ");
Serial.print(blueColorVal);
Serial.println();
}
}
delay(500); // wait a bit, for serial data
}
//read a string from the serial and store it in an array
//you must supply the array variable
void readSerialString (char *strArray) {
int i = 0;
if(!Serial.available()) {
return;
}
while (Serial.available()) {
strArray[i] = Serial.read();
i++;
}
}
- Login to post comments