Lab 2 :: Weather Orb

Submitted by ajeeta on Tue, 02/12/2013 - 23:59
ajeeta's picture

 

Description

For this assignment, I wanted to create a "weather orb" that visualizes the weather conditions outside using  mapping between qualitative descriptions of weather (e.g. hot, bright, sunny, cold, etc.) and colors. 

where:

red hot/ warm sultry sunny bright summer

blue cold chilly cloudy gloomy/ dreary winter

green = cool balmy spring

 
I mashed up the stock codes from "fading LEDs" and "Serial Port communication" to create a fading effect between the color transitions.
 
However, I haven't been able to figure out how to compare multiple strings; so at this time, I'm using single input strings (summer, winter, spring) that map to each of the three colors. (Code Part 1 below).
 
I've also modified the stock code from class to execute the first part of the assignment, i.e. modulate color brightness n 10% intervals using keyboard character inputs. (Code Part 2 below). Entering R, G, or B (in upper case) increases brightness, and entering r, g, or b (in lower case) decreases brightness.
 
I built the diffuser out of carry-out containers and cardboard. I lined the inside of the diffuser with aluminum foil for better illuminance, and used cotton cover around the LEDs for better mixing.

 

 

Components Used

1- Arduino Uno

1- Breadboard

3- 220 Ω Resistor

1- Green LED

1- Blue LED

1- Red LED

Carry-out Containers

Cardboard

Cotton

Aluminum Foil

 

Code Part 1

 

/* 
 * Serial RGB LED
 * ---------------
 * Serial commands control the brightness of R,G,B LEDs
 
 *
 * Created 18 October 2006
 * copyleft 2006 Tod E. Kurt <tod@todbot.com
 * http://todbot.com/
 *
 * Adapted 5 September 2007
 * copylefter 2007 Ryan Aipperspach <ryanaip@alumni.rice.edu>
 *
 * Adapted 12 February 2013
 * copylefter 2013 Ajeeta Dhole <ajeeta@ischool.berkeley.edu>
 */
 
//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 str[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;   // Green LED,   connected to digital pin 9
int bluePin = 10;  // Blue LED, connected to digital pin 10
int redPin  = 11;  // Red LED,  connected to digital pin 11
 
int redVal = 0;
int greenVal = 0;
int blueVal = 0;
 
int j = 0;     // Loop counter  
 
 
void setup() {
  pinMode(redPin,   OUTPUT);   // sets the pins as output
  pinMode(greenPin, OUTPUT);   
  pinMode(bluePin,  OUTPUT);
  Serial.begin(9600);
  analogWrite(redPin,   redVal);   // set them all to low brightness
  analogWrite(greenPin, greenVal);   // set them all to low brightness
  analogWrite(bluePin,  blueVal);   // set them all to low brightness
  Serial.println("What's the weather like outside?");  
  Serial.println("Enter 'summer' for hot, warm, sultry, sunny, bright, summer..");
  Serial.println("Enter 'winter' for cold, chilly, cloudy, gloomy, dreary, winter..");
  Serial.println("Enter 'spring' for cool, balmy spring..");
}
 
void loop () {
  //read the serial port and create a string out of what you read
  readSerialString(str, 100);
  
  processRepeatKeyCommands(str, 100);
 
 
 
  //Erase anything left in the serial string, preparing it for the 
  //next loop
  resetSerialString(str, 100);  
 
  delay(10);  // 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++;
  }
}
 
void processRepeatKeyCommands(char *strArray, int maxLength) {
  int i = 0;
    
     if(stringsEqual(strArray, "summer", 6)) {
      j = 0;
      while (j < 255) {
        if (redVal != 255) {
          redVal   += 1;}
        if (greenVal != 0) {
          greenVal -= 1;}
        if (blueVal != 0) {
          blueVal  -= 1;}
        analogWrite(redPin,   redVal);   // Write current values to LED pins
        analogWrite(greenPin, greenVal); 
        analogWrite(bluePin,  blueVal); 
         // Blue low
        j += 1;
        delay(8);
      }
      Serial.print("Red Value");
      Serial.println(redVal); 
      Serial.print("Blue Value");
      Serial.println(blueVal);
      Serial.print("Green Value");
      Serial.println(greenVal);
    }
    
    else if(stringsEqual(strArray, "winter", 6)) {
      j = 0;
      while (j < 255) {
        if (redVal != 0) {
          redVal   -= 1;}
        if (greenVal != 0) {
          greenVal -= 1;}
        if (blueVal != 255) {
          blueVal  += 1;}
        analogWrite(redPin,   redVal);   // Write current values to LED pins
        analogWrite(greenPin, greenVal); 
        analogWrite(bluePin,  blueVal); 
         // Blue low
        j += 1;
        delay(12);
      }
      Serial.print("Red Value");
      Serial.println(redVal); 
      Serial.print("Blue Value");
      Serial.println(blueVal);
      Serial.print("Green Value");
      Serial.println(greenVal);
    }
    
    else if(stringsEqual(strArray, "spring", 6)) {
      j = 0;
      while (j < 255) {
        if (redVal != 0) {
          redVal   -= 1;}
        if (greenVal != 255) {
          greenVal += 1;}
        if (blueVal != 0) {
          blueVal  -= 1;}
        analogWrite(redPin,   redVal);   // Write current values to LED pins
        analogWrite(greenPin, greenVal); 
        analogWrite(bluePin,  blueVal); 
         // Blue low
        j += 1;
        delay(12);
      }
      Serial.print("Red Value");
      Serial.println(redVal); 
      Serial.print("Blue Value");
      Serial.println(blueVal);
      Serial.print("Green Value");
      Serial.println(greenVal);
    }
    
    //Move on to the next character in the string
    //From here, the code continues executing from the "while" line above...
    i++;
  }
 
 
 
//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;
  }
}
 
Code Part 2
 
/* 
 * 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
 *
 * Alternate command structure is "<colorCode>*", where "colorCode" is
 * one of "r","g", or "b".
 * E.g. "r"    increases the red LED brightness by 10
 *      "rrr"  increases the red LED brightness by 30
 *      "ggb"  increases the green LED brightness by 20 and the blue by 10
 *
 * Created 18 October 2006
 * copyleft 2006 Tod E. Kurt <tod@todbot.com
 * http://todbot.com/
 *
 * Adapted 5 September 2007
 * copylefter 2007 Ryan Aipperspach <ryanaip@alumni.rice.edu>
 *
 * Adapted 12 February 2013
 * copylefter 2013 Ajeeta Dhole <ajeeta@ischool.berkeley.edu>
 */
 
//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;   // Green LED,   connected to digital pin 9
int bluePin = 10;  // Blue LED, connected to digital pin 10
int redPin  = 11;  // Red LED,  connected to digital pin 11
 
float redValue = 0.0;
float greenValue = 0.0;
float blueValue = 0.0;
 
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 low brightness
  analogWrite(greenPin, greenValue);   // set them all to low brightness
  analogWrite(bluePin,  blueValue);   // set them all to low brightness
  Serial.println("Entering 'R', 'G' or 'B' in upper case, increases color brightness in 10% intervals."); 
  Serial.println("Entering 'r', 'g', or 'b' in lower case, decreases color brightness in 10% intervals.");
  Serial.println("Enter color command (e.g. 'RRRRRBB') :");  
}
 
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 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%. Likewise,
//decrease the red value for each 'r', the green value for each 'g', 
//and the blue value for each 'b'.
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 (increase red)...
    if (colorCode == 'R') {
      //Increase the current red value by 10% (25.5), while the red value is less than/ equal to 255...
      if (redValue <= 229.5) {
         redValue += 25.5;
         analogWrite(redPin, redValue);
         Serial.print("setting color red to ");
         Serial.println(redValue);}
      if (redValue >= 255) {
         Serial.println("color red is at its maximum brightness. ");}
    
    //If the character is r (decrease red)...
    } else if (colorCode == 'r') {
      if (redValue >= 25.5) {
        redValue -= 25.5;
        analogWrite(redPin, redValue);
        Serial.print("setting color red to ");
        Serial.println(redValue);}
      if (redValue <=0) {
        Serial.println("color red is at its minimum brightness. ");}
      
    //If the character is G (increase green)...
    } else if (colorCode == 'G') {
      if (greenValue <= 229.5) {
        greenValue += 25.5;
        analogWrite(greenPin, greenValue);
        Serial.print("setting color green to ");
        Serial.println(greenValue);}
      if (greenValue >= 255) {
        Serial.println("color green is at its maximum brightness. ");}
        
     //If the character is g (decrease green)...
    } else if (colorCode == 'g') {
      if (greenValue >= 25.5) {
        greenValue -= 25.5;
        analogWrite(greenPin, greenValue);
        Serial.print("setting color green to ");
        Serial.println(greenValue);}
      if (greenValue <=0 ) {
        Serial.println("color green is at its minimum brightness. ");}
    
    //If the character is B (increase blue)...
    } else if (colorCode == 'B') {
      if (blueValue <= 229.5) {
        blueValue += 25.5;
        analogWrite(bluePin, blueValue);
        Serial.print("setting color blue to ");
        Serial.println(blueValue);}
      if (blueValue >= 255) {
        Serial.println("color blue is at its maximum brightness. ");}
        
    //If the character is b (decrease blue)...
    } else if (colorCode == 'b') {
      if (blueValue >= 25.5) {
        blueValue -= 25.5;
        analogWrite(bluePin, blueValue);
        Serial.print("setting color blue to ");
        Serial.println(blueValue);}
      if (blueValue <= 0) {
        Serial.println("color blue is at its minimum brightness. ");}
    }
    
    //Move on to the next character in the string
    //From here, the code continues executing from the "while" line above...
    i++;
  }
}
 
 
//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;
  }
}

 

 

Spring
Summer
Winter
0
Your rating: None
Drupal theme by Kiwi Themes.