Lizzy- Lab 2

Posted by lizzy

Anonymous's picture

Description
Use the Arduino serial communication to control three light emitting diodes (red, green, and blue) via keyboard commands.
When user enter lowercase letter (r,g,b), value increases. When user enters uppercase letter (R,G,B), value decreases. When LED value reaches max, user will not be able to control LED with lowercase letters. Only decreasing can occur, meaing user must input uppercase letters. When LED value reaches min, user will not be able to control LED with uppercase letters. Only increasing can occur, meaing user must input lowercase letters.

Components Used
    •    Light Emitting Diodes (LEDs)
    •    Resistors (220 ohms)
    •    White piece of paper, folded into tube, was used as diffuser

Code

/*
 * Serial RGB LED
 * ---------------
 * Serial commands control the brightness of R,G,B LEDs
 *
 * Command structure is "<colorCode><colorVal>", where "colorCode" is
 * one of "r","g",or "b" and "colorVal" is a number 0 to 255.
 * E.g. "r0"   turns the red LED off.  
 *      "g127" turns the green LED to half brightness
 *      "b64"  turns the blue LED to 1/4 brightness
 *
 * Created 18 October 2006
 * copyleft 2006 Tod E. Kurt <tod@todbot.com
 * http://todbot.com/
 */

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 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

int redVal = 125; // what LED value starts out at.
int greenVal =125;
int blueVal = 125;

int input = 0; // will later return i from readSerialString

void setup() {
  pinMode(redPin,   OUTPUT);   // sets the pins as output
  pinMode(greenPin, OUTPUT);   
  pinMode(bluePin,  OUTPUT);
  Serial.begin(9600);
  analogWrite(redPin,   125);   // start all LED at mid brightness
  analogWrite(greenPin, 125);   // start all LED at mid brightness
  analogWrite(bluePin,  125);   // start all LED at mid brightness
  Serial.println("enter color command (e.g. 'r', 'rr', 'RRR')");
  Serial.println("Lowercase will increase LED and uppercase will decrease LED :");  
}

void loop () {
  // clear the string
  memset(serInString, 0, 100);
  //read the serial port and create a string out of what you read
  input = readSerialString(serInString); // returns length of string(i)
    
  colorCode = serInString[0];
 
  for(int i=0;i < input; i++) {  // for loop going through input
     if(colorCode == 'r' && redVal < 250) { //Whenever there is a r and redVal is less than max, increase by 25.
       redVal = (redVal+25);
       analogWrite(redPin, redVal);
     if(redVal >= 250){ //if redVal is more than or equal to 250 (the max)
         redVal == 250; // then automatically set value to 250. Once redVal reaches max, LED will not do anything. User can only decrease value with 'R'.
         analogWrite(redPin, redVal);
         }     
Serial.println("red value: ");
Serial.println(redVal);
     }
     if(colorCode=='R' && redVal > 0){ //whenever there is a R and vale is greater than zero, decrease by 25.
       redVal = (redVal-25);
       analogWrite(redPin, redVal);
     if(redVal < 0){ // if redVal is less than zero
       redVal == 0; // automatically set value to zero, which means LED will be off. Once redVal reaches 0, LED will not do anything. User can only increase value with 'r'.
       analogWrite(redPin, redVal);
       }
Serial.println("red value: ");
Serial.println(redVal);
     }
     if(colorCode == 'g' && greenVal < 250){ //Whenever there is a g and green value is less than 250, increase by 25.
       greenVal = (greenVal+25);
       analogWrite(greenPin, greenVal);
     if(greenVal >= 250){ // if greenVal is more than or equal to 250
         greenVal == 250; // automatically set value at 250.  Once greenVal reaches max, LED will not do anything. User can only decrease value with 'G'.
         analogWrite(greenPin, greenVal);
         }     
Serial.println("green value: ");
Serial.println(greenVal);
     }
     if(colorCode=='G' && greenVal > 0){ //whenever there is a G and greenValue is more than min, decrease by 25.
       greenVal = (greenVal-25);
       analogWrite(greenPin, greenVal);
     if(greenVal < 0){ // if greenVal is less than 0
       greenVal == 0; //automatically set greenVal to zero. Once greenVal reaches 0, LED will not do anything. User can only increase value with 'g'.
       analogWrite(greenPin, greenVal);
       }
Serial.println("green value: ");
Serial.println(greenVal);
     }
     if(colorCode == 'b' && blueVal < 250){ //whenever there is a b and blueVal is less than max, increase by 25.
       blueVal = (blueVal+25);
       analogWrite(bluePin, blueVal);
     if(blueVal >= 250){ //if blueVal is more than or equal to 250
         blueVal == 250; // automatically set value to 250. Once blueVal reaches max, LED will not do anything. User can only decrease value with 'B'.
         analogWrite(bluePin, blueVal);
         }     
Serial.println("blue value: ");
Serial.println(blueVal);
      }
      if(colorCode=='B' && blueVal > 0){ //whenever there is a B and blueVal is more than min, decrease by 25.
       blueVal = (blueVal-25);
       analogWrite(bluePin, blueVal);
      if(blueVal < 0){ // of blueVal is less than 0
       blueVal == 0; // automatically set blue to 0. Once blueVal reaches 0, LED will not do anything. User can only increase value with 'b'.
       analogWrite(bluePin, blueVal);
       }
Serial.println("blue value: ");
Serial.println(blueVal);
      }    
  }

 
  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
int readSerialString (char *strArray) {
  int i = 0;
  if(!Serial.available()) {
    return 0;
  }
  while (Serial.available()) {
    strArray[i] = Serial.read();
    i++;
  }
  return i; // lets you store the string i will eventually loop through.
}
 

lizzy- lab 2 (dark)
Lizzy- lab 2
0
Your rating: None