Lab 2

Posted by yoon

yoon's picture

/* Components Used: leds, Arduino board, piece of bridal vail for diffuser */

/* Diffuser is made with bridal vail, in rose shape so that it covers the leds with many layers  */

/*The user can command the color red, yellow or blue at first by 'R', 'Y', 'B', and adjust the color by increasing or decreasing rgb value. */

/* to increase   red: 'r ' ; green: 'g'  ; blue: 'b' */

/* to decrease    red: 'x'  ; green:'y' ; blue: 'z' */

 

 

#include <stdio.h>
#include <string.h>
 
char serInString[100]; 
 
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 = 10;        //initial brightness of all leds = 10
int greenValue = 10;
int blueValue = 10;
 
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. 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';
  }
}
 
 
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 yellow 
  if (colorCode == 'R'){
   analogWrite(redPin, 255);
   analogWrite(bluePin, 0);
   analogWrite(greenPin, 0);
   Serial.println(redValue);
   Serial.println(blueValue);
   Serial.println(greenValue);
  }    
 
  else if (colorCode == 'Y'){
   analogWrite(bluePin, 0);
   analogWrite(redPin, 255);
   analogWrite(greenPin, 255);
   Serial.println(redValue);
   Serial.println(blueValue);
   Serial.println(greenValue);
  }    
  
  else if (colorCode =='B'){
    analogWrite (bluePin, 255);
    analogWrite (redPin, 0);
    analogWrite (greenPin, 0);
   Serial.print("setting color to ");
   Serial.println(redValue);
   Serial.println(blueValue);
   Serial.println(greenValue);
  }
 
 //user can adjust the color by incresing/decreasing RGB values
   //increase red
   if (colorCode == 'r') {
    //Increase the current red value by 10, and if you reach 255 go back to 0
      redValue = (redValue + 10) % 255;
      analogWrite(redPin, redValue);
      Serial.print("setting color r to ");
      Serial.println(redValue);
      }
      
   //decrease red
   else if (colorCode == 'x') {
     redValue = (redValue - 10) % 255;
     analogWrite(redPin, redValue);
     Serial.print("setting color r to ");
     Serial.println(redValue);
   }
   
    //increase green
    else if (colorCode == 'g') {
      greenValue = (greenValue + 10) % 255;
      analogWrite(greenPin, greenValue);
      Serial.print("setting color g to ");
      Serial.println(greenValue);
     }
     
    //decrease green 
    else if (colorCode == 'y') {
     greenValue = (greenValue - 10) % 255;
     analogWrite(greenPin, greenValue);
     Serial.print("setting color g to ");
     Serial.println(redValue);
   }
   
    
    //increase blue
     else if (colorCode == 'b') {
      blueValue = (blueValue + 10) % 255;
      analogWrite(bluePin, blueValue);
      Serial.print("setting color b to ");
      Serial.println(blueValue);
    }
    
    //decrease blue
   else if (colorCode == 'z') {
     blueValue = (blueValue - 10) % 255;
     analogWrite(bluePin, blueValue);
     Serial.print("setting color b to ");
     Serial.println(blueValue);
   }
 
 
    i++;
  }
}
Purlpe
Diffuser : rose
clear
0
Your rating: None