Assignment: Digital I/O with Arduino Boards + Diffuser
Collaborators:
Assignment: Digital I/O with Arduino Boards + Diffuser
Collaborators:
Description
Create a diffuser and a system in which entering r, g, or b increases the intensity by 10.
I created the diffuser with an empty opaque white medicine bottle stuffed with the fluffy filling from class.
For bonus, I also include code that lets the user choose red, blue, green, cyan, yellow, or magenta.
Components Used
- 3 x Light Emitting Diode (LED)
- 3 x Resistor
Arduino Code
/*
* Serial RGB LED
* ---------------
* Serial commands control the brightness of R,G,B LEDs
*
* Command structure is "<colorCode>", where "colorCode" is
* combinations of "r","g",or "b." Each letter will raise the corresponding color by 10.
*
* Created 18 October 2006
* copyleft 2006 Tod E. Kurt <tod@todbot.com
* http://todbot.com/
*/
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 redVal = 127;
int greenVal = 127;
int blueVal = 127;
int percentage = 10; // how much the color value goes up for each letter pressed
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
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 mid brightness
analogWrite(greenPin, greenVal); // set them all to mid brightness
analogWrite(bluePin, blueVal); // set them all to mid brightness
Serial.println("enter color command (e.g. 'rrr') :");
}
void loop () {
//read the serial port and create a string out of what you read
readSerialString(serInString);
for (int i = 0; i < 100; i++) {
colorCode = serInString[i];
if (colorCode == '\0') {
break;
} else if (colorCode == 'r') {
redVal = (redVal + percentage) % 255;
analogWrite(redPin, redVal);
Serial.print("Setting r to ");
Serial.print(redVal);
Serial.println();
} else if (colorCode == 'g') {
greenVal = (greenVal + percentage) % 255;
analogWrite(greenPin, greenVal);
Serial.print("Setting g to ");
Serial.print(greenVal);
Serial.println();
} else if (colorCode == 'b') {
blueVal = (blueVal + percentage) % 255;
analogWrite(bluePin, blueVal);
Serial.print("Setting b to ");
Serial.print(blueVal);
Serial.println();
}
}
for (int i = 0; i < 100; i++) {
serInString[i] = '\0'; // indicates we've used this string
}
delay(100); // wait a bit, for serial data
}
//read a string from the serial and store it in an array
//you must supply the array variable
void readSerialString (char *strArray) {
int i = 0;
if(!Serial.available()) {
return;
}
while (Serial.available()) {
strArray[i] = Serial.read();
i++;
}
}
/*
* Serial RGB LED
* ---------------
* Serial commands control the brightness of R,G,B LEDs
*
* Command structure is "<colorCode>", where "colorCode" is
* "red," "green," "blue," "black," "white," "cyan," "magenta," or
* "yellow."
*
* Created 18 October 2006
* copyleft 2006 Tod E. Kurt <tod@todbot.com
* http://todbot.com/
*/
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
int redVal = 127;
int greenVal = 127;
int blueVal = 127;
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
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 mid brightness
analogWrite(greenPin, greenVal); // set them all to mid brightness
analogWrite(bluePin, blueVal); // set them all to mid brightness
Serial.print("enter color command ");
Serial.println("red, green, blue, cyan, yellow, or magenta) :");
}
void loop () {
//read the serial port and create a string out of what you read
readSerialString(serInString);
if (strcmp(serInString, "red") == 0) {
analogWrite(redPin, 255);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
} else if (strcmp(serInString, "green") == 0) {
analogWrite(redPin, 0);
analogWrite(greenPin, 255);
analogWrite(bluePin, 0);
} else if (strcmp(serInString, "blue") == 0) {
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 255);
} else if (strcmp(serInString, "cyan") == 0) {
analogWrite(redPin, 0);
analogWrite(greenPin, 255);
analogWrite(bluePin, 255);
} else if (strcmp(serInString, "yellow") == 0) {
analogWrite(redPin, 255);
analogWrite(greenPin, 255);
analogWrite(bluePin, 0);
} else if (strcmp(serInString, "magenta") == 0) {
analogWrite(redPin, 255);
analogWrite(greenPin, 0);
analogWrite(bluePin, 255);
}
for (int i = 0; i < 100; i++) {
serInString[i] = '\0'; // indicates we've used this string
}
delay(100); // wait a bit, for serial data
}
//read a string from the serial and store it in an array
//you must supply the array variable
void readSerialString (char *strArray) {
int i = 0;
if(!Serial.available()) {
return;
}
while (Serial.available()) {
strArray[i] = Serial.read();
i++;
}
}