Lab2 - Serial input LED color mixer with diffuser

/* 

 * Serial RGB LED
 * ---------------
 * Serial commands control the brightness of R,G,B LEDs
 *
 * Command structure is "<colorCode>[1...10]", where "colorCode"
 * one of "r","g",or "b" and can be repeated 1 or  more times
 * E.g. "rrrrr"   turns the red LED to half brightness.  
 *      "ggggggggg" turns the green LED to 90% brightness
 *      "gg"  again turns the green LED to 10% brightness
 *      "bbbbbbbbbb" turns the blue LED to full brightness
 *
 * Created 18 October 2006
 * copyleft 2006 Tod E. Kurt <tod@todbot.com
 * http://todbot.com/
*
* Modified by ram
 */
 
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 rColorValue;
int gColorValue;
int bColorValue;
 
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. 'rrrrr') :");  
}
 
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];
  int level = 1;
  if( colorCode == 'r' || colorCode == 'g' || colorCode == 'b' ) {
    for(int i = 1; i < sizeof(serInString)-1; i++) {
        if(serInString[i] == colorCode) {
            level += 1;
        }
        else {
            break;
        }
    }
    int inputColorVal = (level*255)/10;
    Serial.print(inputColorVal);
    
    int colorVal;
    if(colorCode == 'r') {
        rColorValue = rColorValue + inputColorVal;
        if(rColorValue > 255) {
          rColorValue = rColorValue - 255;
        }
        colorVal = rColorValue;
        analogWrite(redPin, rColorValue);
    }
    else if(colorCode == 'g') {
        gColorValue = gColorValue + inputColorVal;
        if(gColorValue > 255) {
          gColorValue = gColorValue - 255;
        }
        colorVal = gColorValue;
        analogWrite(greenPin, gColorValue);
    }
    else if(colorCode == 'b') {
        bColorValue = bColorValue + inputColorVal;
        if(bColorValue > 255) {
          bColorValue = bColorValue - 255;
        }
        colorVal = bColorValue;
        analogWrite(bluePin, bColorValue);
    }
 
    Serial.print("set color ");
    Serial.print(colorCode);
    Serial.print(" to ");
    Serial.print(colorVal);
    Serial.println();
    serInString[0] = 0;                   // indicates we've used this string
  }
  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++;
  }
}
Led color mixer with diffuser
LED color mixer circuit
5
Your rating: None Average: 5 (1 vote)