Description:
The first part of the lab was to create a diffuser. I liked the idea of creating something with texture so I crumpled up a white paper bag I had and created an airy dome for my diffuser. The second part was to fix the serial code so that when you type in any number of "r", "g" or "b"s, a certain reaction happens and that light is lit however the parameters are set. I didn't want to type the letters many times so I incremented by 20%, starting from zero. If we input "r" 5x, we have full brightness. Each press of the letter increases by 51.
Materials used:
Breadboard
Arduino
Cables
3 LED Lights
Paper Bag
Code:
/*
* Serial RGB LED
*/
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 redValue = 0;
int greenValue = 0;
int blueValue = 0;
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, redValue); // set them all to 0
analogWrite(greenPin, greenValue); // set them all to 0
analogWrite(bluePin, blueValue); // set them all to 0
Serial.println("enter color command (e.g. 'r43' or 'rrrrgb') :");
}
void loop () {
// clear the string
memset(serInString, 0, 100);
//read the serial port and create a string out of what you read
readSerialString(serInString);
colorCode = serInString[0]; //keep track of colorCode in array
redValue = 0; //start color values at 0
greenValue = 0;
blueValue = 0;
//if enter 'r', 'g', 'b' perform for loop
if (colorCode == 'r' || colorCode == 'g' || colorCode == 'b'){
for(int i=0; i<100; i++){ //this is a for loop to go through the array
if(serInString[i]== 'r') //increase brightness by 51
redValue = redValue + 51;
else if(serInString[i]== 'g') //g increases greenpin by 51
greenValue = greenValue + 51;
else if(serInString[i]== 'b')
blueValue = blueValue + 51;
}
Serial.println(); //tell the user what their input was
Serial.print("Red Input:");
Serial.print(redValue / 51);
Serial.println();
Serial.print("Green Input: ");
Serial.print(greenValue / 51);
Serial.println();
Serial.print("Blues: ");
Serial.print(blueValue / 51);
Serial.println();
analogWrite(redPin, redValue); //set the values of the leds to the levels that have been indicated
analogWrite(greenPin, greenValue);
analogWrite(bluePin, blueValue);
}
delay(100); //delay program and wait for serial data
}
void readSerialString(char *strArray) {
int i = 0;
if(!Serial.available()) {
return;
}
while (Serial.available()) {
strArray[i]=Serial.read();
i++;
}
}
- Login to post comments