Digital I/O with Arduino Boards + Diffuser

Assignment: Digital I/O with Arduino Boards + Diffuser

Collaborators:

Project Objective: We spend significant amount of their time on computers. Sometimes we are really busy and we don't want to be disturbed by people. So I have designed a status teller application using Arduino board and color diffuser used. If you are busy then it will display the status in red.   If you have free time and available, then it will display in green. If you are busy but still won't mind being bugged for urgent issues then it will display the status in blue color.

Materials: I used styrofoam cup.

/*
* info262: Theory and Practice of Tangible User Interfaces
Assignment: Designing Diffuser
Programmer: Dhawal Mujumdar
Email: dhawal@ischool.berkeley.edu
*/

#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 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 the first letter of your status color. You can refer following list. (e.g. Enter 'r' without quotes for red) :");
Serial.println("You can increase intensity by pressing r multiple times for red (e.g. rrrr)");
Serial.println("Busy (i.e. red)");
Serial.println("Available (i.e. green)");
Serial.println("You wanna be bugged even though you are busy (i.e. blue)");
}

void loop () {
//read the serial port and create a string out of what you read
readSerialString(serInString, 100); 

showMyStatus(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++;
}
}

void showMyStatus(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)...
if (colorCode == 'r')
{
//Increase the current red value by 25, and if you reach 255 go back to 0
redValue = (redValue + 25) % 255;
greenValue = 0;
blueValue = 0;
Serial.print("Setting color red to ");
Serial.println(redValue);   
}
//If the character is g (green)...
else if (colorCode == 'g')
{
redValue = 0;
greenValue = (greenValue + 25) % 255;
blueValue = 0;     
Serial.print("Setting color green to ");
Serial.println(greenValue);  
}
//If the character is b (blue)...
else if (colorCode == 'b')
{
redValue = 0;
greenValue = 0;
blueValue = (blueValue + 25) % 255;
Serial.print("Setting color blue to ");
Serial.println(blueValue);
}

analogWrite(redPin, redValue);
analogWrite(greenPin, greenValue);
analogWrite(bluePin, blueValue);
//Move on to the next character in the string
//From here, the code continues executing from the "while" line above...
i++;
}
}