Serial LED -lab2

Submitted by sebarness on Mon, 02/11/2013 - 19:35

 

Description: serial input 'r', 'g', 'b' will control the brightness of the red, green and blue LED.
 
Components: 
-3 LED's
-Breadboard
-Arduino
-Cotton
-Candle Holder
-Lighter
-220 resistors
 
 
/* 
 * Serial RGB LED
*/
 
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 redValue = 255;
int greenValue = 255;
int blueValue = 255;
 
 
 
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,  redValue);   // set them all to 255
  analogWrite(greenPin, greenValue);   // set them all to 255
  analogWrite(bluePin,  blueValue);   // set them all to 255
  Serial.println("enter color command (e.g. 'r43' or 'rrrrgb') :");  
}
 
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];  //keep track of colorCode in array
  redValue = 0;                //start color values at 0
  greenValue = 0;
  blueValue = 0;
 
//if enter 'r', 'g', 'b' perform for loop
 if (colorCode == 'r' || colorCode == 'g' || colorCode == 'b'){
  
  for(int i=0; i<100; i++){  //this is a for loop to go through the array
   if(serInString[i]== 'r') //increase brightness by 10
    redValue = redValue + 10;
   else if(serInString[i]== 'g') //g increases greenpin by 10's
     greenValue = greenValue + 10;
   else if(serInString[i]== 'b')
     blueValue = blueValue + 10;
  }
  Serial.println();           //tell the user what their input was
    Serial.print("Red's: ");
    Serial.print(redValue / 10);
    Serial.println();
    Serial.print("setting color ");
    Serial.print("red");
    Serial.print(" to ");
    Serial.print(redValue);
    Serial.println();
    Serial.println();
    
    Serial.print("Greens: ");
    Serial.print(greenValue / 10);
    Serial.println();
    Serial.print("setting color ");
    Serial.print("green");
    Serial.print(" to ");
    Serial.print(greenValue);
    Serial.println();
    Serial.println();
    
    Serial.print("Blues: ");
    Serial.print(blueValue / 10);
    Serial.println();
    Serial.print("setting color ");
    Serial.print("blue");
    Serial.print(" to ");
    Serial.print(blueValue);
    Serial.println();
    
    
    analogWrite(redPin, redValue);    //set the values of the leds to the levels that have been indicated
    analogWrite(greenPin, greenValue);
    analogWrite(bluePin, blueValue);
    
 }
  
  delay(100);  //delay program and wait for serial data
}
 
void readSerialString(char *strArray) {
  int i = 0;
  if(!Serial.available()) {
    return;
  }
  while (Serial.available()) {
    strArray[i]=Serial.read();
    i++;
  }
}
 
photo (9).JPG
0
Your rating: None
Drupal theme by Kiwi Themes.