Description:
In this lab exercise we explored 2 of the digital features of the Arduino Board: Pulse Width Modulation (PWM), and serial communication with the laptop.
We started by uploading the example code from the Arduino Sketchbook (DimmingLEDs.txt) for a fading LED. Then we extended the circuit to include all 3 LEDs having pins red=9, blue=10, green=11. We proceeded to upload another example (serial_led_rgb.txt) which allowed us to use Arduino's Serial Communication feature to control the brightness of each of the 3 LEDs individually.
For the homework assignment, I modified the code such that the user can dim or brighten each LED independently in order to create different color combinations. The user introduces de color of choice ("r", "b", "g") and then uses the "w" key to increase the chosen LED's brightness, and the key "s" to decrease it (these keys mimic the function of "up" and "down" arrows). If the upper brightness limit (255) is reached, further increasing the brightness will restart the selected LED at a brightness of 0. Conversely, decreasing the brightness past the value of 0 will restart the LED at maximum brightness, thus creating a loop. The user also has the choice of typing "f", "m", "o" for setting an LED directly to full, middle, or 0 brightness, respectively.
I created a diffuser by coupling the top of a clear plastic bottle with the cap of an Ikea decoration light (see attached image).
Components:
1- Arduino Uno Microcontroller
1- Breadboard
3- 220 Ω Resistors
3 - Light Emmitting Diodes (LEDs) (Red, Green, Blue)
1- USB Cable
1- MacBook Pro 13" (OSX)
1- Ikea decoration light cap (orange-tinged diffuser)
1- Transparent plastic bottle cap (diffuser support)
Code:
/*
* Serial RGB LED
* ---------------
* Serial commands control the brightness of R,G,B LEDs
*
* User picks which LED they want to control (R,B,G)
* and then uses W to increase intensity and S do decrease
* intensity of the chosen LED. The other 2 LEDs remain stationary.
* User can also type 'f', 'm', or 'o' for full, medium, or zero brightness.
*
* Created 22 September 2013
*
* Sofia Solar Cafaggi
*/
char serInString[100];
char colorCode;
char Control;
int colorVal;
int RIntensity=0;
int BIntensity=0;
int GIntensity=0;
int Red=0;
int Blue=0;
int Green=0;
int redPin = 9; // Red LED, connected to digital pin 9
int greenPin = 10; // Green LED, connected to digital pin 10
int bluePin = 11; // Blue LED, connected to digital pin 11
void setup() {
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
analogWrite(redPin, 127); // set them all to mid brightness
analogWrite(greenPin, 127); // set them all to mid brightness
analogWrite(bluePin, 127); // set them all to mid brightness
Serial.println("Choose a colour (R,B,G). Then use 'w' or 's' to increase/decrease its brightness, respectively. You can also use 'f', 'm', 'o' to turn the selected LED to full, medium, or zero brightness, respectively. :");
}
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 = Serial.read();
//Serial.print(colorCode);
//Red LED Loop
if(colorCode=='r'||colorCode=='R')
{
Serial.println("You've chosen colour Red. Press W to increase intensity, press S to decrease intensity");
Red=1;
Blue=0;
Green=0;
}
if(Red==1)
{
if(colorCode=='w'||colorCode=='W')
{
RIntensity=RIntensity+25;
if(RIntensity > 255)
{RIntensity=0;}
}
if(colorCode=='s'||colorCode=='S')
{
RIntensity=RIntensity-25;
if(RIntensity < 0)
{RIntensity=255;}
}
if(colorCode=='o'||colorCode=='O')
{
RIntensity=0;
}
if(colorCode=='f'||colorCode=='F')
{
RIntensity=255;
}
if(colorCode=='m'||colorCode=='M')
{
RIntensity=127;
}
}
//Blue LED Loop
if(colorCode=='b'||colorCode=='B')
{
Serial.println("You've chosen colour Blue. Press W to increase intensity, press S to decrease intensity");
Red=0;
Blue=1;
Green=0;
}
if(Blue==1)
{
if(colorCode=='w'||colorCode=='W')
{
BIntensity=BIntensity+25;
if(BIntensity > 255)
{BIntensity=0;}
}
if(colorCode=='s'||colorCode=='S')
{
BIntensity=BIntensity-25;
if(BIntensity < 0)
{BIntensity=255;}
}
if(colorCode=='o'||colorCode=='O')
{
BIntensity=0;
}
if(colorCode=='f'||colorCode=='F')
{
BIntensity=255;
}
if(colorCode=='m'||colorCode=='M')
{
BIntensity=127;
}
}
//Green LED Loop
if(colorCode=='g'||colorCode=='G')
{
Serial.println("You've chosen colour Green. Press W to increase intensity, press S to decrease intensity");
Red=0;
Blue=0;
Green=1;
}
if(Green==1)
{
if(colorCode=='w'||colorCode=='W')
{
GIntensity=GIntensity+25;
if(GIntensity > 255)
{GIntensity=0;}
}
if(colorCode=='s'||colorCode=='S' && Green==1)
{
GIntensity=GIntensity-25;
if(GIntensity < 0)
{GIntensity=255;}
}
if(colorCode=='o'||colorCode=='O')
{
GIntensity=0;
}
if(colorCode=='f'||colorCode=='F')
{
GIntensity=255;
}
if(colorCode=='m'||colorCode=='M')
{
GIntensity=127;
}
}
analogWrite(redPin,RIntensity);
analogWrite(bluePin,BIntensity);
analogWrite(greenPin,GIntensity);
delay(100); // 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