Assignment: Digital I/O with Arduino Boards + Diffuser
Collaborators:
Assignment: Digital I/O with Arduino Boards + Diffuser
Collaborators:
Assignment: Digital I/O with Arduino Boards + Diffuser
Collaborators:
1. Project Description
: Use the Arduino to control three different LEDs by getting input from a user
2. Components Used
- Arduino
- Green LED / Red LED / Blue LED
- 3 Resistors
- Diffuser
- Mini-umbrella, which could be found on a cocktail glass
- Small purple glass
3. Arduino Code
/*
* Serial RGB LED
* ---------------
* Serial commands control the brightness of R,G,B LEDs
*
* Command structure is "<colorCode><colorValue>", where "colorCode" is
* one of "r","g",or "b" and "colorValue" 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% from red colorValue 0
* "rrr" increases the red LED brightness by 30% from red colorValue 0
* "ggb" increases the green LED brightness by 20% and the blue by 10% from each colorValue 0
*
* Modified by JinYoung, Baik
* Created 18 October 2006
* copyleft 2006 Tod E. Kurt <tod@todbot.com
*
* Adapted 5 September 2007
* copylefter 2007 Ryan Aipperspach <ryanaip@alumni.rice.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 colorValue;
char color[4];
int redPin = 9; // Red LED, connected to digital pin 11
int greenPin = 10; // Green LED, connected to digital pin 10
int bluePin = 11; // Blue LED, connected to digital pin 9
int redValue = 10;
int greenValue = 10;
int blueValue = 10;
//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); // 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 your mood of today (e.g. cold, hot, warm, tired, monotonous, positive)");
Serial.println("(but, maximum number of characters : 4)");
}
void loop () {
//read the serial port and create a string out of what you read
readSerialString(serInString, 100);
//UNCOMMENT ONE OF THE FOLLOWING COMMANDS, OR NOTHING WILL HAPPEN WHEN YOU
//RUN THE PROGRAM...
//Uncomment the following line to read commands of the form 'r245' or 'b3'
//numberCommands(serInString);
//Uncomment the following line to read commands of the form 'rrrb'
//multipleKeyCommands(serInString, 100);
//Or write your own function...
//YOUR_FUNCTION_HERE(serInString);
moodCommands(serInString);
//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';
color[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++;
}
}
//get the input 'r''g''b' with number.
//for example, r240 sets the red LED to the value 240 (out of 255)
void numberCommands(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' ) {
colorValue = atoi(serInString+1);
Serial.print("setting color ");
Serial.print(colorCode);
Serial.print(" to ");
Serial.print(colorValue);
Serial.println();
serInString[0] = 0;
if(colorCode == 'r')
analogWrite(redPin, colorValue);
else if(colorCode == 'g')
analogWrite(greenPin, colorValue);
else if(colorCode == 'b')
analogWrite(bluePin, colorValue);
}
delay(100); // wait a bit, for serial data
}
//get multiple keys to control the RGB values
//For example pressing 'r' 5 times will increase the red LED value by 50
void multipleKeyCommands(char *strArray, int maxLength) {
int i = 0;
int pr = 0;
int pg = 0;
int pb = 0;
//loop through the string (strArray)
//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 (colorCode == 'r') {
//Increase the current red value by 25.5 (25.5 is 10% of total 255)
//p value refers to increased percent compared to initial color value
pr +=10;
redValue = redValue + 25.5;
analogWrite(redPin, redValue);
Serial.print("increasing color red ");
Serial.print(pr);
Serial.println("%");
} else if (colorCode == 'g') {
pg +=10;
greenValue = greenValue + 25.5;
analogWrite(greenPin, greenValue);
Serial.print("increasing color green ");
Serial.print(pg);
Serial.println("%");
} else if (colorCode == 'b') {
pb +=10;
blueValue = blueValue + 25.5;
analogWrite(bluePin, blueValue);
Serial.print("increasing color blue ");
Serial.print(pb);
Serial.println("%");
}
//Move on to the next character in the string
i++;
}
}
//input your mood of the day, and we would provide appropriate color with message.
void moodCommands(char *strArray){
//read in the first three characters in the string
for(int i = 0; i <5; i++){
color[i] += serInString[i];
}
n: baseline; padding: 0px; border: 0px initial initial;">
if(color[0] == 'c' && color[1] == 'o' && color[2] == 'l' && color[3] == 'd' ){
analogWrite(redPin, 255);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
Serial.println("Feel Warmer?");
}
else if (color[0] == 'h' && color[1] == 'o' && color[2] == 't'){
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 255);
Serial.println("Feel cooler?");
}
else if (color[0] == 't' && color[1] == 'i' && color[2] == 'r' && color[3] == 'e' ){
analogWrite(redPin, 0);
analogWrite(greenPin, 255);
analogWrite(bluePin, 0);
Serial.println("Make your eyes refresh..");
}
else if (color[0] == 'm' && color[1] == 'o' && color[2] == 'n' && color[3] == 'o'){
analogWrite(redPin, 255);
analogWrite(greenPin, 255);
analogWrite(bluePin, 255);
Serial.println("Life is filled with various colors!");
}
else if (color[0] == 'p' && color[1] == 'o' && color[2] == 's' && color[3] == 'i'){
analogWrite(redPin, 255);
analogWrite(greenPin, 10);
analogWrite(bluePin, 255);
Serial.println("Keep thinking positive!");
}
else if (color[0] == 'w' && color[1] == 'a' && color[2] == 'r' && color[3] == 'm'){
analogWrite(redPin, 150);
analogWrite(greenPin,150);
analogWrite(bluePin, 0);
Serial.println("Spring is coming!");
}
}
4. Picture