Description:
In this lab, I set up the Arduino Processor to accept individual keyboard commands to control the brightness of a red, blue, and green LED. I connected the circuits to the PWM input through 220 ohm resistors, used a "hairdo" cap and plastic cups as a diffuser, and provided code to change code in the following ways (taken from the Help section inside the code, which can be accessed from the Serial Monitor by pressing “H”):
HELP MENU
R, G, or B stand for the Red, Blue, and Green LEDs.
If the single letter is pressed, the LED turns OFF.
R => Red off
Pressing a color consecutive times increases the brightness.
Press the color (R) to begin that color, then press more for each increase:
R =>Red color resets to 0
RRR =>Red selected, then pressing 2x changes brightness from 0 to 25, then 25 to 50
RRRR =>Red selected, then 3x changes from 50 -> 75 -> 100 -> 125
There are 10 scales, labeled 0 (LOW) to 9 (MAX)
Change brightness by pressing a key followed by a number (i.e., R6)
R6 =>Red Brightness to 179...
COMMANDS:
H: Help menu
C: Clear
O: Orange
P: Purple
Y: Yellow
W: White
V: Violet
A: Aqua
Components:
1 Breadboard
1 Arduino Uno
3 LEDs (Red, Blue, Green)
1 USB Cable
3 220 Ohm Resistors
Connecting wires
Code:
/*
* Code for cross-fading 3 LEDs, red, green and blue, or one tri-color LED, using PWM
* The program allows pressing the R, G, or B keys multiple times, or the Key and number (R7)
* The debugging code assumes Arduino 0004, as it uses the new Serial.begin()-style functions
* Clint Anderson <clintanderson@berkeley.edu>
*/
//Used Fellow Classmate "Peter Nguyen's" code for "ReadSerialString"
//Used his idea to get First Character from string
//Code differs in that I increase brightness by "Letter-SingleNumber"
// Output
char charactersTyped[50];
char keyPressed;
char numPressed;
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
// Program variables
int redVal = 1; // Variables to store the values to send to the pins
int greenVal = 1;
int blueVal = 1;
int i = 0; // Loop counter
int wait = 50; // 50ms (.05 second) delay; shorten for faster fades
int DEBUGHW = 1;
bool notInitialized = true; //Shows the "Welcome" info once, then set to false
void setup()
{
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
if ( DEBUGHW) { // If we want to see the pin values for debugging...
Serial.begin(9600); // ...set up the serial ouput on 0004 style
}
}
// Main program
void loop()
{
if (notInitialized){
Serial.print("Welcome to Clint's Lab Assignment 2! LED COLORS.\n");
Serial.print("Please press H for list of Help items.\n");
Serial.print("Press R, G, or B followed by (1-9) to begin.\n\n\n");
notInitialized = false;
}
int key = 0; //The pin of concern. Set initially to non-used pin 0.
int brightness = 0;
int incrementor = 0; //Used when multiple colorKeys are pressed (=> r3 = Red, 3rd scale vs. rrr = increment Red 3 times).
memset(charactersTyped, 0, 50);
readSerialString(charactersTyped);
keyPressed = charactersTyped[0];
numPressed = charactersTyped[1];
brightness = getBrightness(numPressed, charactersTyped, 1); //Based on a pressed key (R) and uses string to view how many more times (starting at "1")
//Sets the key in final analogWrite(KEY, brightness) to the correct value--red, green, or blue.
//With each added color (orange, pink, etc.):
// 1. Add the case in the "keyPressed" switch,
// 2. Add the color to the "numPressed" default
// 3. Add the analongWrite function at bottom: showNEW_COLOR
switch(keyPressed){
case 'r': key = redPin;
Serial.print("\nRed Pressed\t");
break;
case 'g': key = greenPin;
Serial.print("\nGreen Pressed\t");
break;
case 'b': key = bluePin;
Serial.print("\nBlue Pressed\t");
break;
case 'o': Serial.print("\nOrange Pressed\t");
key = showOrange(); //changes all colors, switches pin to non-used
break;
case 'p': Serial.print("\nPurple Pressed\t");
key = showPurple(); //changes all colors, switches pin to non-used
break;
case 'y': Serial.print("\nYellow Pressed\t");
key = showYellow(); //changes all colors, switches pin to non-used
break;
case 'w': Serial.print("\nWhite Pressed\t");
key = showWhite(); //changes all colors, switches pin to non-used
break;
case 'v': Serial.print("\nViolet Pressed\t");
key = showViolet(); //changes all colors, switches pin to non-used
break;
case 'a': Serial.print("\nAqua Pressed\t");
key = showAqua(); //changes all colors, switches pin to non-used
break;
case 'h': showInstructions();
break;
case 'c': clear();
break;
default: ; //Since this loops, ensure NOTHING is here
}
//Writes the printing statements and resets ColorValues if only 1 key pressed.
//The "getBrightness" function above is the one that changes the brightness.
switch(numPressed){
case '0': Serial.print("Brightness: 1 (Off)\n" );
break;
case '1': Serial.print("Brightness: 51\n" );
break;
case '2': Serial.print("Brightness: 77\n" );
break;
case '3': Serial.print("Brightness: 102\n" );
break;
case '4': Serial.print("Brightness: 128 (Half)\n" );
break;
case '5': Serial.print("Brightness: 153\n" );
break;
case '6': Serial.print("Brightness: 179\n" );
break;
case '7': Serial.print("Brightness: 204\n" );
break;
case '8': Serial.print("Brightness: 230\n" );
break;
case '9': Serial.print("Brightness: 255 (Max)\n" );
break;
case 'r':
case 'b':
case 'g': Serial.print("\n"); //means that several presses of R/G/B was pressed => do NOT reset values
break;
//Else, single value was pressed --
default: if (keyPressed == 'r' || keyPressed == 'g' || keyPressed == 'b' ||
keyPressed == 'h' || keyPressed == 'c' || keyPressed == 'o' ||
keyPressed == 'p' || keyPressed == 'y' || keyPressed == 'w' ||
keyPressed == 'v' || keyPressed == 'a'){
Serial.print("\n"); //Need return in case letter only pressed
switch(keyPressed){
case 'r': redVal = 0;
Serial.print("Red color reset\n");
break;
case 'b': blueVal = 0;
Serial.print("Blue color reset\n");
break;
case 'g': greenVal = 0;
Serial.print("Green color reset\n");
break;
default: ;
}//end inner default
}//end outside default
}
analogWrite(key, brightness);
delay(wait); // Pause for 'wait' milliseconds before resuming the loop
}
//Given the second number (numChar) in an input string (charactersTyped), returns an int brightness.
//If numChar is a number, returns that brightness. If it is a repetition of the first string (i.e., "rr"),
//recursively cycles through remaining characters to return brightness based on # of entries.
int getBrightness(char numberChar, char* charactersTyped, int iteration){
int brightness = 0;
switch(numberChar){
case '0': brightness = 1;
break;
case '1': brightness = 51;
break;
case '2': brightness = 77;
break;
case '3': brightness = 102;
break;
case '4': brightness = 128;
break;
case '5': brightness = 153;
break;
case '6': brightness = 179;
break;
case '7': brightness = 204;
break;
case '8': brightness = 230;
break;
case '9': brightness = 255;
break;
// Next 3 cases connect the current Color Value to the brightness selected. Used when multiple keys pressed next cycle
case 'r': Serial.print("\nIteration: ");
Serial.print(iteration);
brightness = redVal + 25;
if(brightness > 255)
brightness = 255;
redVal = brightness;
Serial.print("\tBrightness: ");
Serial.print(brightness);
if(charactersTyped[iteration + 1]){
brightness = getBrightness('r', charactersTyped, iteration + 1);
redVal = brightness;
}
break;
case 'g': Serial.print("\nIteration: ");
Serial.print(iteration);
brightness = greenVal + 25;
if(brightness > 255)
brightness = 255;
greenVal = brightness;
Serial.print("\tBrightness: ");
Serial.print(brightness);
if(charactersTyped[iteration + 1]){
brightness = getBrightness('g', charactersTyped, iteration + 1);
greenVal = brightness;
}
break;
case 'b': Serial.print("\nIteration: ");
Serial.print(iteration);
brightness = blueVal + 25;
if(brightness > 255)
brightness = 255;
blueVal = brightness;
Serial.print("\tBrightness: ");
Serial.print(brightness);
if(charactersTyped[iteration + 1]){
brightness = getBrightness('b', charactersTyped, iteration + 1);
blueVal = brightness;
}
break;
default: ;
}
return brightness;
}
void showInstructions(){
clear();
Serial.print(" HELP MENU\n");
Serial.print("R, G, or B stand for the Red, Blue, and Green LEDs.\n");
Serial.print("If the single letter is pressed, the LED turns OFF.\n");
Serial.print(" R\t=> Red off \n");
Serial.print("Pressing a color consecutive times increases the brightness.\n");
Serial.print("Press the color (R) to begin that color, then press more for each increase:\n");
Serial.print(" R\t=>Red color resets to 0\n");
Serial.print(" RRR\t=>Red selected, then pressing 2x changes brightness from 0 to 25, then 25 to 50 \n");
Serial.print(" RRRR\t=>Red selected, then 3x changes from 50 -> 75 -> 100 -> 125\n");
Serial.print("There are 10 scales, labeled 0 (LOW) to 9 (MAX).\n");
Serial.print("Change brightness by pressing a key followed by a number (i.e., R6)\n");
Serial.print(" R6\t=>Red Brightness to 179...\n");
Serial.print("\t\tCOMMANDS:\n");
Serial.print("H: Help menu\n");
Serial.print("C: Clear\n");
Serial.print("O: Orange\n");
Serial.print("P: Purple\n");
Serial.print("Y: Yellow\n");
Serial.print("W: White\n");
Serial.print("V: Violet\n");
Serial.print("A: Aqua\n");
}
int showWhite(){
analogWrite(redPin, 255);
analogWrite(bluePin, 255);
analogWrite(greenPin, 255);
return 0;
}
int showOrange(){
analogWrite(redPin, 255);
analogWrite(bluePin, 0);
analogWrite(greenPin, 128);
return 0;
}
int showPurple(){
analogWrite(redPin, 128);
analogWrite(bluePin, 255);
analogWrite(greenPin, 0);
return 0;
}
int showYellow(){
analogWrite(redPin, 255);
analogWrite(bluePin, 0);
analogWrite(greenPin, 255);
return 0;
}
int showViolet(){
analogWrite(redPin, 255);
analogWrite(bluePin, 128);
analogWrite(greenPin, 0);
return 0;
}
int showAqua(){
analogWrite(redPin, 0);
analogWrite(bluePin, 255);
analogWrite(greenPin, 255);
return 0;
}
void clear(){
for(int i = 0; i < 30; i++)
Serial.print("\n");
}
//Peter Nguyen's readSerialString
void readSerialString (char *strArray) {
int i = 0;
if(!Serial.available()) {
return;
}
while (Serial.available()) {
strArray[i] = Serial.read();
i++;
}
}
- Login to post comments