Lab 2 - RGB control + Diffuser

Submitted by vstarostenko on Mon, 02/11/2013 - 14:17

The code below will control red, green, and blue LEDs. For every letter 'r', 'g', or 'b' the intensity of corresponding LED will increase by 10%. If the value goes over 255, the brightness will be reset to 0.

There is also an OFF command 'o', which turns all LEDs off.

CODE:

 

 
#include <stdio.h>
#include <string.h>
 
char serInString[100];  // array that will hold the different bytes of the string. 100=100characters;
 
char colorCode;
int colorVal;
 
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 redValue = 0;
int greenValue = 0;
int blueValue = 0;
 
void setup() {
  pinMode(redPin,   OUTPUT);   // sets the pins as output
  pinMode(greenPin, OUTPUT);   
  pinMode(bluePin,  OUTPUT);
  Serial.begin(9600);
  analogWrite(redPin,   redValue);  
  analogWrite(greenPin, greenValue);  
  analogWrite(bluePin,  blueValue);   
  Serial.println("enter color command:");  
}
 
void loop () {
  //read the serial port and create a string out of what you read
  readSerialString(serInString, 100);
 
  //Function
 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 increase the red value for each 'r',
//the green value for each 'g', and the blue value for each 'b'.
//For example "rrrg" increases red by 30 and green by 10.
void processRepeatKeyCommands(char *strArray, int maxLength) {
  int i = 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) increase 10 % for every letter. If over 225, reset to 0.
    if (colorCode == 'r') {
      if (redValue > 225) {
        redValue = 0;
      }
      redValue = redValue + 25;
      analogWrite(redPin, redValue);
      Serial.print("setting color r to ");
      Serial.println(redValue);
      
    //If the character is g (green) increase 10 % for every letter. If over 225, reset to 0.
    } else if (colorCode == 'g') {
      if (greenValue > 225) {
        greenValue = 0;
      }
      greenValue = greenValue + 25;
      analogWrite(greenPin, greenValue);
      Serial.print("setting color g to ");
      Serial.println(greenValue);
    
    //If the character is b (blue) increase 10 % for every letter. If over 225, reset to 0.
    } else if (colorCode == 'b') {
      if (blueValue > 225) {
        blueValue = 0;
      }
      blueValue = blueValue + 25;
      analogWrite(bluePin, blueValue);
      Serial.print("setting color b to ");
      Serial.println(blueValue);
      
    //If the character is o (off), turn all LEDs off.
    } else if (colorCode == 'o') {
        redValue = 0;
        greenValue = 0;
        blueValue = 0;
        analogWrite(redPin, redValue);
        analogWrite(greenPin, greenValue);
        analogWrite(bluePin, blueValue);
        Serial.print("setting all LEDs to OFF ");
            
    }
    
    //Move on to the next character in the string
    //From here, the code continues executing from the "while" line above...
    i++;
  }
}
 
WP_20130206_009.jpg
WP_20130206_011.jpg
0
Your rating: None
Drupal theme by Kiwi Themes.