After learning Pulse Width Modulation and serial communication with the laptop at the lab, this assignment of building the LED diffuser was to explore these features in more depth. I started with setting three resistors, LED lights, and connecting wires. I carefully built LED circuit by using pin 9, 10, and 11, which can support Pulse Width Modulation. Then, by using the Arduino Sketchbook, I coded a program so that I can control RGB values of LED lights by pressing letters ‘r’, ‘g’, and ‘b’ multiple times. The program is designed to perform as follows:
- Each time a letter (either ‘r’,’g’, or ‘b’) is pressed, the value of that particular color LED light increases 25.5, which is about 10% of maximum value.
- When a LED light reaches maximum value, it goes back to 0 and start to count from the beginning.
- When a user type ‘off’ via serial, all the LED lights will turn off.
Then, I built a diffuser by using a ping pong ball. I used a electric drill to make a hole on one side of the ball. I covered LED lights by the ping pong ball and the diffuser reflected mixed color of three LED lights.
Components used:
1- Arduino Uno
1- Breadboard
1- 220ohms resistors
1- LED lights (red, green, blue)
7- Connecting wires
1- Ping pong ball (diffuser)
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