This lab focused on Pulse Width Modulation (PWM) to simulate analog behavior as well as serial communication between the computer and Arduino Uno Microcontroller.
First the single LED circuit from the previous lab was loaded with the “Fading” sketchbook code. This modulated the brightness of the LED by altering the duration of the square on/off wave. This created a fading effect, where the signal started with a relatively “slow” frequency for a dim LED and advanced to a “fast” frequency to appear bright. While the “Blink” code from lab 1 used pin 13, this code used pin 9 because it supported PWM which pin 13 is unable to do.
Second, the circuit was expanded to include all three LEDs in parallel, all connected to PWM pins. The provided code “DimmingLEDs” was uploaded to the Arduino. This code performed either a positive fade, negative fade, or maintained a brightness level of 1 depending on the iteration count of the code. The three situations for the LED rotated every 255 iteration, allowing for full fade to brightness and dimness. After the 762 iteration the iteration counter was reset and a new cycle was begun.
For the third part of the lab, the provided “serial_led_rgb” code was uploaded. This allowed the user to input data for the uploaded program to act on. This code took the first element in the serial string and set it as a color code and then took the rest of the elements in the serial string and converted it into a single integer which was set as a brightness value. These where then expressed using PWM method to show different brightness levels.
For the first part of the homework, I experimented with a frosted glass shot glass and covered it with a white grocery plastic bag while maintaining a separating layer. This resulted from observing that by only using one of the two materials still had very localized diffusion of the light but with the combination of the two with a separation layer a higher level of diffusion was achieved. Then I discovered a thin white plastic cup. Again experimenting with different combinations of the three materials the best combination was determined to be a small sample of plastic grocery bag covered by the larger white plastic cup.
For the second part of the homework, I took the initial “serial_led_rgb” code and modified it that it would read a serial string and count the number of ‘r’, ‘g’, and ‘b’ characters and modulate the brightness on a 10% relationship to each occurrence of the character and reset to 0% on the 11th occurance. In addition I added a serial ‘on’ and serial ‘off’ command. When a ‘p’ character is entered into the serial string the light brightness is turned to 0. To turn the lights back on, when a ‘o’ character is read, the lights return to their previous values. There is also a clear command which returns all the lights back to 0 and their previous value is lost. This occurs when a ‘c’ and ‘l’ are entered consecutively.
Components:
1 – Arduino Uno Microcontroller
1 – Breadboard
1 – USB to serial cable
3 – Light Emitting Diodes (LEDs) (1 red, 1 green, 1 blue)
1 – White plastic cup
1 – White grocery bag
7 – gauge 22 solid core wires
3 – 220 ohm resistor
Code:
/*Serial LED Modulation
Tangible User Interfaces
Lab 2
by Kevin Lessard
9/24/13
Modified off "serial_led_rgb"
*/
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 rbright = 0; //brightness values
int gbright = 0;
int bbright = 0;
int rcount = 0; //character counters
int gcount = 0;
int bcount = 0;
int i = 0; //for loop counter
int strlength = 0; //so that only input characters are read in the for loop
int on = 1; //on/off command variable
char cl = 'a'; // clear command flag
char serprint = 'w'; //one time print flag
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 off
analogWrite(greenPin, 0); // set them all to off
analogWrite(bluePin, 0); // set them all to off
}
void loop () {
memset(serInString, 0, 100); // set all the values in the array to zero
readSerialString(serInString); //read in serial string
strlength = strlen(serInString); //counts the elements in serInString
if(serprint == 'w'){ //prints the below text one time
Serial.print("enter the characters 'r', 'g', and 'b' to increase brightness by 10% ");
Serial.println();
Serial.print("enter 'p' to temporarly turn off the lights and 'o' to turn them back on");
Serial.println();
Serial.print("enter 'cl' to clear the light values");
serprint = 'q'; //stops from printing in every loop
}
for(i=0; i < strlength; i++){ //counts the number of 'r's 'g's and 'b's in the serial string
if (serInString[i] == 'p'){ //if a 'p' character is read, then the 'on' int is changed to 0 turing brightness to 0
on = 0;}
if (serInString[i] == 'o'){ //if a 'c' character is read, then the 'on' int is changed to 1 keeping the same brightness
on = 1;}
if (serInString[i] == 'r'){ //counts 'r's
rcount++;}
if (serInString[i] == 'g'){ //counts 'g's
gcount++;}
if (serInString[i] == 'b'){ //counts 'b's
bcount++;}
if (serInString[i] == 'c' && serInString[i+1] == 'l'){ //if 'cl' apprears then the cl variable changes
cl = 'z';}
}
serInString[0] = 0; // indicates we've used this string
while (rcount > 10){ //resets the count values to 0 if they go over 10
rcount = rcount - 11;}
while (gcount > 10){
gcount = gcount -11;}
while (bcount > 10){
bcount = bcount -11;}
if(cl == 'z'){ //clears the count values before they are converted into bright values if cl was changed above
rcount = 0;
gcount = 0;
bcount = 0;
cl = 'a'; //once the count values are cleared the clear variable resets
}
rbright = (rcount*.1)*255*on; //brightnest percentage, each time a letter is entered it incraments 10%
gbright = (gcount*.1)*255*on; //the on variable varies between 1 and 0 to turn the lights on and off while maintaining the count values
bbright = (bcount*.1)*255*on;
analogWrite(redPin, rbright); //turn the lights on
analogWrite(greenPin, gbright);
analogWrite(bluePin, bbright);
delay(10); // 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()) {
//Laura: If there is nothing in the serial port (enter has not been hit) then send nothing back
return;
}
//Laura: If there is something in the serial port...
while (Serial.available()) {
//..read each character into the array, one at a time
strArray[i] = Serial.read();
i++;
}
}
- Login to post comments