Description:
I created an Arduino circuit that lights up three different LEDs based on input. Typing in multiple letters such as 'r', 'g', or 'b' will set the corresponding colored LED with different values. For example: typing in five 'r's (rrrrr) will set the red LED to 50%. I also created a diffuser for the LEDs using the cotton-based fabric from cleaning wipes. I tied a rope along the bottom to better cover the LEDs
Materials used:
1 Arduino Uno
1 Breadboard
3 LED Lights
piece of cotton-based cleaning wipe
a small rope
various wires
Code:
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
void setup() {
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
analogWrite(redPin, 127); // set them all to mid brightness
analogWrite(greenPin, 127); // set them all to mid brightness
analogWrite(bluePin, 127); // set them all to mid brightness
Serial.println("enter color command (e.g. 'r43') :");
}
void loop () {
// clear the string
memset(serInString, 0, 100);
//read the serial port and create a string out of what you read
readSerialString(serInString);
int r_count = 0;
int g_count = 0;
int b_count = 0;
for (int i=0; i < 100; i++) {
if (serInString[i] == 'r') {
r_count += 1;
if (b_count >= 10) {
analogWrite(redPin, 255);
}
else {
analogWrite(redPin, r_count*25.5);
}
analogWrite(redPin, r_count*25.5);
}
else if (serInString[i] == 'g') {
g_count += 1;
if (b_count >= 10) {
analogWrite(greenPin, 255);
}
else {
analogWrite(greenPin, g_count*25.5);
}
analogWrite(greenPin, g_count*25.5);
}
else if (serInString[i] == 'b') {
b_count += 1;
if (b_count >= 10) {
analogWrite(bluePin, 255);
}
}
}
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++;
}
}
- Login to post comments