Lab 2 - Serial LED with Diffuser

Submitted by Jenton on Thu, 02/07/2013 - 23:07

Description:

This is my lab submission. I have set up my arduino board as per the basic homework assignment spec. The formula I used to determine the brightness of the LED is, x*25.5, where x is how many r's, g's, and b's were counted in the input. So if there are 5 r's, then the formula equals, 127.5, which is 50% of full brightness. If there are 10 r's, then the formula will equal 255, which is 100% full brightness. Additionally, the code will allow for multi-letter inputs, such as rrrrggbbb, which will activate all LED lights at the appropriate brightnesses. Additionally, if more than 10 of the same letters are input, then the brightness for that light will reset to 0.

Components Used:

  • 3 LED Lights
  • 3 220 ohms resister
  • Breadboard
  • Arduino
  • 4 grounding cables
  • 3 power cables
  • USB Cable
  • Packing Peanut
  • Cotton

Code:

 

 

/* 
 * Serial RGB LED
 * ---------------
 * Serial commands control the brightness of R,G,B LEDs
 *
 * Command structure is "<colorCode>*", where "colorCode" is
 * one of "r","g", or "b".
 * E.g. "r"    sets the red LED brightness to 10% (25)
 *      "rrrrr"  sets the red LED brightness to 50% (or 127)
 *      "ggb"  sets the green LED brightness to 20% and the blue to 10%
 *      if there are more than 10 rs, it will set the brightnesss to 0
 *
 * Created 18 October 2006
 * copyleft 2006 Tod E. Kurt <tod@todbot.com
 * http://todbot.com/
 *
 * Adapted 07 February 2013
 * by Jenton Lee
 *
 */
 
//include support for manipulating strings.
//for a useful string comparison function, see the bottom of this file... stringsEqual()
#include <stdio.h>
#include <string.h>
 
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 greenPin   = 9;   // Red LED,   connected to digital pin 9
int redPin = 10;  // Green LED, connected to digital pin 10
int bluePin  = 11;  // Blue LED,  connected to digital pin 11
 
int redValue = 127;
int greenValue = 127;
int blueValue = 127;
 
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 mid brightness
  analogWrite(greenPin, greenValue);   // set them all to mid brightness
  analogWrite(bluePin,  blueValue);   // set them all to mid brightness
  Serial.println("enter color command (e.g. 'r43 or rrrrrrrrbbbb') :");  
}
 
void loop () {
  //read the serial port and create a string out of what you read
  readSerialString(serInString, 100);
    
  processRepeatKeyCommands(serInString, 100);
  
  //Erase anything left in the serial string, preparing it for the 
  //next loop
  resetSerialString(serInString, 100);  
 
  delay(100);  // wait a bit, for serial data
}
 
void resetSerialString (char *strArray, int length) {
  for (int i = 0; i < length; i++) {
    strArray[i] = '\0';
  }
}
 
//read a string from the serial and store it in an array
//you must supply the array variable
void readSerialString (char *strArray, int maxLength) {
  int i = 0;
 
  if(!Serial.available()) {
    return;
  }
  while (Serial.available() && i < maxLength) {
    strArray[i] = Serial.read();
    i++;
  }
}
 
//go through the string, and count how many r, g, and b,
//the total counts of each letter will determine how bright the led.
//For example "rrrg" means 30% red brightness and 10% green brightness.
void processRepeatKeyCommands(char *strArray, int maxLength) {
  int i = 0;
  int redCount = 0;
  int greenCount = 0;
  int blueCount = 0;
  
  //loop through the string (strArray)
  //i = the current position in the string
  //Stop when either (a) i reaches the end of the string or
  //                 (b) there is an empty character '\0' in the string
  while (i < maxLength && strArray[i] != '\0') {
    //Read in the character at position i in the string
    colorCode = serInString[i];
    
    //If the character is r (red)...
    if (colorCode == 'r') {
      //Increment the redCount counter. If the redCount goes over 10, reset
      //the brightness to 0.
      redCount++;
      if (redCount <= 10) {
      redValue = (redCount * 10) * 2.55;
      } else 
      {
        redValue = 0;
      }
     
    //If the character is g (green)...
    } else if (colorCode == 'g') {
      greenCount++;
      if (greenCount <= 10) {
      greenValue = (greenCount * 10) * 2.55;
      } else 
      {
        greenValue = 0;
      }    //If the character is b (blue)...
    } else if (colorCode == 'b') {
      blueCount++;
      if (blueCount <= 10) {
      blueValue = (blueCount * 10) * 2.55;
      } else 
      {
        blueValue = 0;
      }
    }
    //Move on to the next character in the string
    //From here, the code continues executing from the "while" line above...
    i++;
  }
  //the system status
  if (redCount >= 1) {
    analogWrite(redPin, redValue);
    Serial.print("setting color r to ");
    Serial.println(redValue);            
  } 
  if (greenCount >= 1) {   
    analogWrite(greenPin, greenValue);
    Serial.print("setting color g to ");
    Serial.println(greenValue); 
  } 
  if (blueCount >= 1) {
    analogWrite(bluePin, blueValue);
    Serial.print("setting color b to ");
    Serial.println(blueValue); 
  }
     
}
 
//change the value of the red, green, or blue LED according to the command received.
//for example, r240 sets the red LED to the value 240 (out of 255)
void processNumericalCommands(char *strArray) {
  //read in the first character in the string
  colorCode = serInString[0];
  
  //if the first character is r (red), g (green) or b (blue), do the following...
  if( colorCode == 'r' || colorCode == 'g' || colorCode == 'b' ) {
    //convert the string to an integer
    //(start at the second character, or the beginning of the string '+1')
    colorVal = atoi(serInString+1);
    Serial.print("setting color ");
    Serial.print(colorCode);
    Serial.print(" to ");
    Serial.print(colorVal);
    Serial.println();
 
    if(colorCode == 'r') 
      analogWrite(redPin, colorVal);
    else if(colorCode == 'g')
      analogWrite(greenPin, colorVal);
    else if(colorCode == 'b')
      analogWrite(bluePin, colorVal);
  }
}
 
//compare two strings to see if they are equal
//compares the first 'numCharacters' characters of string1 and string2 to
//see if they are the same
//
//E.g. stringsEqual("hello","hello",5) => true
//     stringsEqual("hello","helaabbnn",3) => true
//     stringsEqual("hello","helaa",5) => false
boolean stringsEqual(char *string1, char *string2, int numCharacters) {
  if (strncmp(string1, string2, numCharacters) == 0) {
    return true;
  } else {
    return false;
  }
}
 

 

My diffuser!
0
Your rating: None
Drupal theme by Kiwi Themes.