Red Green Blue LEDs with Color Picker Mouse Interface

Posted by icheung

icheung's picture

Description

- Design a diffuser for LEDs

- Play with keyboard input or other to control 3 LEDs (red, green, blue)

 

Components Used

Diffuser: A muffin cup and a glass bowl.

LEDs: red, green and blue!

220 ohm resistors

 

Extra Fun

Prayag and I worked together to create a Color Picker GUI which allows a user to select a color from a color palette of RGB values which then gets sent over the serial port to the arduino.

 

Arduino Code

***********************************

The multiple key press code:

**********************************

 

/* 
 * Multiple key press code
 * Iris Cheung
 */
 
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 numLetters;
 
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("enter color command (e.g. 'r43') :");  
}
 
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( colorCode == 'r' || colorCode == 'g' || colorCode == 'b' ) {
    //colorVal = atoi(serInString+1);
    colorVal = numLetters*0.1*255;
    Serial.print("setting color ");
    Serial.print(colorCode);
    Serial.print(" to ");
    Serial.print(colorVal);
    Serial.println();
    serInString[0] = 0;                   // indicates we've used this string
    if(colorCode == 'r') {
      analogWrite(redPin, colorVal);}
    else if(colorCode == 'g')
      analogWrite(greenPin, colorVal);
    else if(colorCode == 'b')
      analogWrite(bluePin, colorVal);
  }
  
  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++;
  }
  numLetters = i;
  Serial.print("numLetters:");
  Serial.print(numLetters);
}
 
***************************
The Color Picker Code
****************************
Just uses the serial code provided in assignment 2!
See the attached file for the python code used for creating the Color Picker GUI.
Glass Bowl with Muffin Cup Diffuser
Color Picker GUI
0
Your rating: None