Description
In this lab we explored the digital and serial capabilities of the Arduino Uno microcontroller. I used pulse width modulation (PWM) to power three LEDs connected to digital pins 9, 10, and 11 respectively. Serial communication with the microcontroller was accomplished through the USB connection with the computer. The serial communication allowed me to alter the pulse width (with respect to time) between the square waves sent to the LEDs. Through this manner, I was able to influence the brightness level of the LEDs. Initially, an example sketch was uploaded to the microcontroller that faded the three in-parallel LEDs. Building on this, I loaded a sketch which enabled setting the brightness of the LEDs through serial communication. I made my own edits to this sketch to change some of the functional output. The changes I implemented allow for different commands to be entered that have differing effects on the LED output. The command names are fairly self-explanatory and are as follows: dim (for 3 LEDs), half (sets brightness level to half maximum for 3 LEDs), bright (for 3 LEDs), red, green, blue, purple, r/g/b + dim/half/bright, and fade (fades the three LEDs in a specific pattern). Finally, to accentuate the effects of the lighting variations, I made a light diffuser out of the plastic bottom of a tennis ball container and some yellow-tinged gift tissue paper.
Components
1- Arduino Uno Microcontroller
1- Breadboard
3- 220 Ω Resistors
3 - Light Emmitting Diodes (LEDs) (Red, Green, Blue)
1- USB Cable
1- Apple Laptop Computer (OSX)
1- Tennis ball contaoner bottom (diffuser)
1- Sheet of gift tissue paper (light yellow)
Code
//Stuart Altman
//Lab 2 Submission
//Serial control of RGB LEDs, with various input/output opportunities
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 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);
Serial.println("Select an illumination mode by typing: 'dim', 'half', 'bright', 'off', 'purple', 'r/g/b + dim/half/bright' or 'fade'.");
}
char command1;
char command2;
char command3;
char command4;
char command5;
char command6;
char command7;
int redVal = 255; // Variables to store the values to send to the pins
int greenVal = 1; // Initial values are Red full, Green and Blue off
int blueVal = 1;
int i = 0; // Loop counter
int DEBUG = 0; // DEBUG counter; if set to 1, will write values back via serial
int n = 1;
void loop () {
memset(serInString, 0, 100);
readSerialString(serInString);
command1 = serInString[0];
command2 = serInString[1];
command3 = serInString[2];
command4 = serInString[3];
command5 = serInString[4];
command6 = serInString[5];
command7 = serInString[6];
Serial.print(command1);
Serial.print(command2);
Serial.print(command3);
if( command1 == 'd' && command2 == 'i' && command3 == 'm'){
analogWrite(redPin, 20);
analogWrite(greenPin, 20);
analogWrite(bluePin, 20);
}
else if ( command1 == 'h' && command2 == 'a' && command3 == 'l' && command4 == 'f') {
analogWrite(redPin, 127);
analogWrite(greenPin, 127);
analogWrite(bluePin, 127);
}
else if ( command1 == 'b' && command2 == 'r' && command3 == 'i' && command4 == 'g' && command5 == 'h' && command6 == 't'){
analogWrite(redPin, 255);
analogWrite(greenPin, 255);
analogWrite(bluePin, 255);
}
else if ( command1 == 'o' && command2 == 'f' && command3 == 'f'){
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
}
else if ( command1 == 'r' && command2 == 'd' && command3 == 'i' && command4 == 'm'){
analogWrite(redPin, 20);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
}
else if ( command1 == 'g' && command2 == 'd' && command3 == 'i' && command4 == 'm'){
analogWrite(redPin, 0);
analogWrite(greenPin, 20);
analogWrite(bluePin, 0);
}
else if ( command1 == 'b' && command2 == 'd' && command3 == 'i' && command4 == 'm'){
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 20);
}
else if ( command1 == 'r' && command2 == 'h' && command3 == 'a' && command4 == 'l' && command5 == 'f'){
analogWrite(redPin, 127);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
}
else if ( command1 == 'g' && command2 == 'h' && command3 == 'a' && command4 == 'l' && command5 == 'f'){
analogWrite(redPin, 0);
analogWrite(greenPin, 127);
analogWrite(bluePin, 0);
}
else if ( command1 == 'b' && command2 == 'h' && command3 == 'a' && command4 == 'l' && command5 == 'f'){
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 127);
}
else if ( command1 == 'r' && command2 == 'b' && command3 == 'r' && command4 == 'i' && command5 == 'g' && command6 == 'h' && command7 == 't'){
analogWrite(redPin, 255);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
}
else if ( command1 == 'g' && command2 == 'b' && command3 == 'r' && command4 == 'i' && command5 == 'g' && command6 == 'h' && command7 == 't'){
analogWrite(redPin, 0);
analogWrite(greenPin, 255);
analogWrite(bluePin, 0);
}
else if ( command1 == 'b' && command2 == 'b' && command3 == 'r' && command4 == 'i' && command5 == 'g' && command6 == 'h' && command7 == 't'){
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 255);
}
else if ( command1 == 'p' && command2 == 'u' && command3 == 'r' && command4 == 'p' && command5 == 'l' && command6 == 'e' ){
analogWrite(redPin, 255);
analogWrite(greenPin, 0);
analogWrite(bluePin, 255);
}
else if ( command1 == 'f' && command2 == 'a' && command3 == 'd' && command4 == 'e'){
// 3 LED fade script: For 4 cycles, fades the LEDs in this pattern: rgrgbgbgr
while (n < 5) {
if (i < 255) { // First phase of fades
i += 1; // Increment counter
redVal -= 2; // Red down
greenVal += 2; // Green up
blueVal = 2; // Blue low
}
else if (i < 509) // Second phase of fades
{ i += 1;
redVal = 2; // Red low
greenVal -= 2; // Green down
blueVal += 2; // Blue up
}
else if (i < 763) // Third phase of fades
{ i += 1;
redVal += 2; // Red up
greenVal = 2; // Green low
blueVal -= 2; // Blue down
}
else // Re-set the counter, and start the fades again
{
i = 1;
n = n+ 1;
}
analogWrite(redPin, redVal); // Write current values to LED pins
analogWrite(greenPin, greenVal);
analogWrite(bluePin, blueVal);
delay(25);
//n+1;
}
}
delay(100);
}
void readSerialString (char *strArray) {
int i = 0;
if(!Serial.available()) {
return;
}
while (Serial.available()) {
strArray[i] = Serial.read();
i++;
}
}
Images
- Login to post comments