Description
In this lab, we explored Pulse Width Modulation (PWM) as a means to "fake" analog behavior, and serial communication to control 3 LEDs with our Arduino microcontroller. The LEDs were connected in parallel.
For homework, my code utilizes several commands- "r," "b," "g" to turn on the red, blue, or green LED respectively. To change the brightness of these LEDs, you can enter up to four letters corresponding to each specific LED color.
For example, to control the red LED:
"r"= 25% brightness
"rr"= 50% brightness
"rrr"= 75% brightness
"rrrr"= 100% brightness
"off"= off
For my diffuser, I cut a white plastic bag and attached it to the bottom part of a plastic water bottle.
Components Used
1- Arduino Uno
1- Breadboard
1- USB Cable
1- Diffuser
4- Jumper wires
3- 220 Ohm resistors
3- LEDs (Red, Blue, Green)
1- plastic water bottle bottom (for diffuser)
1- cut up plastic bag (for diffuser)
Code
/*
* Kristina Hart Lab 2
* ---------------
*/
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, 0); // set them all to mid brightness
analogWrite(greenPin, 0); // set them all to mid brightness
analogWrite(bluePin, 0); // set them all to mid brightness
Serial.println("enter color command (e.g. 'rrrr' = 100% brightness) :");
}
void loop () {
// clear the string
memset(serInString, 0, 100);
//read the serial port and create a string out of what you read
readSerialString(serInString);
char Input0 = serInString[0];
char Input1 = serInString[1];
char Input2 = serInString[2];
char Input3 = serInString[3];
if(Input0 == 'r' && Input1 == 'r' && Input2 == 'r' && Input3 == 'r'){
analogWrite(redPin, 255);
}
else if(Input0 == 'g' && Input1 == 'g' && Input2 == 'g' && Input3 == 'g'){
analogWrite(greenPin, 255);
}
else if(Input0 == 'b' && Input1 == 'b' && Input2 == 'b' && Input3 == 'b'){
analogWrite(bluePin, 255);
}
else if(Input0 == 'r' && Input1 == 'r' && Input2 == 'r'){
analogWrite(redPin, 191);
}
else if(Input0 == 'g' && Input1 == 'g' && Input2 == 'g'){
analogWrite(greenPin, 191);
}
else if(Input0 == 'b' && Input1 == 'b' && Input2 == 'b'){
analogWrite(bluePin, 191);
}
else if(Input0 == 'r' && Input1 == 'r'){
analogWrite(redPin, 127);
}
else if(Input0 == 'g' && Input1 == 'g'){
analogWrite(greenPin, 127);
}
else if(Input0 == 'b' && Input1 == 'b'){
analogWrite(bluePin, 127);
}
else if(Input0 == 'r'){
analogWrite(redPin, 64);
}
else if(Input0 == 'g'){
analogWrite(greenPin, 64);
}
else if(Input0 == 'b'){
analogWrite(bluePin, 64);
}
else if(Input0 == 'o' && Input1 == 'f' && Input2 == 'f'){
analogWrite(redPin, 0);
analogWrite(bluePin, 0);
analogWrite(greenPin, 0);
}
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