Lab02 ex1-3

fanwaipio's picture

Description

1. Design a good diffuser for your RGB LEDs (e.g. ping pong ball, Styrofoam, etc)
2. Change the code so that you can control the RGB values with multiple key presses. For example,pressing ‘r’ 5 times will set the brightness to 50% (or brightness = 127) and pressing ‘r’ 10 times will set it to 100% (or brightness = 255)
3. (Optional) Come up with other ways of controlling the colors of the LEDs using the keyboard
Here’s your chance to be creative. You can show off to your friends next week. Example: Read colors and set the appropriate RGB values like “orange” might set r=70%, g=50%, and b=0%.
 
Components Used
Light Emitting Diode (LED) x3
220-ohm Resistor x3
A half A4 of paper
 
Arduino Code for ex 1 & 2
/* 
 * Serial RGB LED
 * ---------------
 * Serial commands control the brightness of R,G,B LEDs
 *
 * Command structure is "<colorCode>[1..10]", where "colorCode" is
 * one of "r","g",or "b"
 * E.g. "rrr"   turns the red LED on with 30%.  
 *
 * Created 13 September 2011
 * copyleft 2011 Fan, Wai Pio
 */
 
// Output
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
char serialInputString[100]; // array that will hold the different bytes of the string.
char colorCode='r'; // store the code of color
int colorCharCount=0; // store how many codes are input
int colorValue=0; // color value for the corresponding color code
 
void setup()
{
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  Serial.begin(9600);
  analogWrite(redPin, 127);
  analogWrite(greenPin, 127);
  analogWrite(bluePin, 127);
  Serial.println("Enter color command (e.g. rrr):");
}
 
void loop()
{
  memset(serialInputString, 0, 100);
  colorCharCount = readSerialString(serialInputString);
  colorCode = serialInputString[0];
  
  if((colorCode=='r'||colorCode=='g'||colorCode=='b')
      &&colorCharCount>0)
  {
    colorValue = colorCharCount/10.0*255;
    Serial.print("setting color ");
    Serial.print(colorCode);
    Serial.print(" to ");
    Serial.print(colorValue);
    Serial.println();
    if(colorCode == 'r') analogWrite(redPin, colorValue);
    else if(colorCode == 'g') analogWrite(greenPin, colorValue);
    else if(colorCode == 'b') analogWrite(bluePin, colorValue);
  }
  delay(100);
}
 
//read a string from the serial and store it in an array
//you must supply the array variable
int readSerialString(char *charArray)
{
  int i = 0;
  int discardInput = 0;
  char dump='r'; // store discarded character
  if(!Serial.available()) return 0;
  while(Serial.available())
  {
    if(discardInput==0) 
    {
       charArray[i] = Serial.read();
       i++;
       if(charArray[i-1]!=charArray[0] || i>=10) 
       {
         // ingore input if it is over or equal to 10 characters
         // or the input character is not the first character
         discardInput = 1;
       }
        
    } 
    else 
    {
      dump = Serial.read();
    }
  }
  //Serial.println(i);
  return i;
}
 
 
Arduino Code for ex 3
/* 
 * Serial RGB LED with color names 
 * ---------------
 * Serial commands control the brightness of R,G,B LEDs
 *
 * Command structure is "<colorName>", where "colorName" is
 * one of "orange", "red", "lime”, etc
 *
 * Created 13 September 2011
 * copyleft 2011 Fan, Wai Pio
 */
#define NCOLORS  22 // the number of colors
// Output
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
char serialInputString[100];
// array that will hold the different names of colors.
char *colorNames[NCOLORS] = {"black","blue","brown","chocolate","crimson","cyan","green","lime","magenta","navy","olive","orange","orchid","pink","purple","red","silver","snow","tomato","violet","white","yellow"};
// array that will hold the different hex values of colors.
char *colorHexValues[NCOLORS] = {"000000","0000FF","A52A2A","D2691E","DC143C","00FFFF","008000","00FF00","FF00FF","000080","808000","FFA500","DA70D6","FFC0CB","800080","FF0000","C0C0C0","FFFAFA","FF6347","EE82EE","FFFFFF","FFFF00"};
char colorCode='r'; // store the code of color
int colorCharCount=0; // store how many codes are input
 
int redValue   = 127; // Variables to store the values to send to the pins
int greenValue = 127;   // Initial values are Red full, Green and Blue off
int blueValue  = 127;
 
void setup()
{
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  Serial.begin(9600);
  analogWrite(redPin, redValue);
  analogWrite(greenPin, greenValue);
  analogWrite(bluePin, blueValue);
  Serial.println("Enter color name (e.g. orange):");
}
 
void loop()
{
  memset(serialInputString, 0, 100);
  readSerialString(serialInputString);
  
  if(findAndSetColorValues()==1)
  {
    Serial.print("setting color (r, g, b) to (");
    Serial.print(redValue);
    Serial.print(", ");
    Serial.print(greenValue);
    Serial.print(", ");
    Serial.print(blueValue);
    Serial.print(") ");
    Serial.println();
    analogWrite(redPin, redValue);
    analogWrite(greenPin, greenValue);
    analogWrite(bluePin, blueValue);
  }
  delay(100);
}
 
// find the name of color and set the values
int findAndSetColorValues()
{
  int i=0;
  for(;i<NCOLORS;i++)
  {
    if(strcmp(serialInputString, colorNames[i])==0)
    {
      redValue = hexCharToDecimal(colorHexValues[i][0])*16 +
                 hexCharToDecimal(colorHexValues[i][1]);
      greenValue = hexCharToDecimal(colorHexValues[i][2])*16 +
                   hexCharToDecimal(colorHexValues[i][3]);
      blueValue = hexCharToDecimal(colorHexValues[i][4])*16 +
                  hexCharToDecimal(colorHexValues[i][5]);
      return 1;
    }
  }
  return 0;
}
 
// convert hex char to decimal value
int hexCharToDecimal(char hexChar)
{
  if(isdigit(hexChar)) 
    return hexChar - '0';
  else
    return hexChar - 'A' + 10;
}
 
//read a string from the serial and store it in an array
//you must supply the array variable
int readSerialString(char *charArray)
{
  int i = 0;
  if(!Serial.available()) return 0;
  while(Serial.available())
  {
     charArray[i] = Serial.read();
     i++;
  }
  return i;
}
 
Serial RGB LED and Rose Origami Diffuser
0
Your rating: None