Lab 2: Multiple Key Entry RBG control
Description
The first part of the lab involves finding a suitable diffuser to blend the three LEDs. i had originally constructed an origami pyramid from the tracing paper provided in class and filled the tip with polyfill, but it did not quite blend as much as I had hoped. I then borrowed my cat's bubble wrap envelope and folded it under the pyramid, which worked much better. The multiple layers helped. (edit: The pyramid now belongs to the cat as well.)
The second part of the lab involves creating a code to allow for changes in the brightness of the LEDs through counting key inputs. (I bolded most of my changes to the serial LED code we were provided with.) I had a few conditions that I wanted to fulfill:
- Start from 0.
- The brightness should increase by 10% for each instance of the letter, reaching 255 with 10 keys.
- Serial input does not create placeholders and incrementally affect the current state of the LEDs, but starts from 0. (For example, the red brightness should remain at 76 if "rrr" + Enter is followed by "rrr" + Enter.)
- Inputs above 10 ("rrrrrrrrrr") should turn off the LED, as a quick reset.
Components Used
1 arduino uno R3 (5V)
3 220 Ω resistor
3 LED (red, blue, green)
4 wires (1 green, 3 orange)
1 USB cable
1 tracing paper origami pyramid
1 bubble wrap envelope
polyfiber fill
Code
#include<stdio.h>
#include<stdlib.h>
char serInString[100];
char colorCode;
int redPin = 9;
int greenPin = 10;
int bluePin = 11;
int redValue = 0; //Start from LOW
int greenValue = 0;
int blueValue = 0;
void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
analogWrite(redPin, redValue);
analogWrite(greenPin, greenValue);
analogWrite(bluePin, blueValue);
Serial.println("enter multiple RBGs (e.g. 'rrrrrbbggg') :");
}
void loop () {
readSerialString(serInString, 100);
processKeyEntry(serInString, 100);
resetSerialString(serInString, 100);
delay(100);
}
void resetSerialString (char *strArray, int length) {
for (int i = 0; i < length; i++) {
strArray[i] = '\0';
}
}
void readSerialString (char *strArray, int maxLength) {
int i = 0;
if(!Serial.available()) {
return;
}
while (Serial.available() && i < maxLength) {
strArray[i] = Serial.read();
i++;
}
}
/* each instance r, g, or b represents 10%; more than 10 of each character resets that color value to 0 */
void processKeyEntry(char *strArray, int maxLength) {
int i = 0;
int redKey = 0;
int blueKey = 0;
int greenKey = 0;
while (i < maxLength && strArray[i] != '\0') {
colorCode = serInString[i];
//red
if (colorCode == 'r') {
redKey++;
if (redKey <= 10){
//Increase the current red value by 10%
redValue = (redKey * 25.5);
}
// NOTE: this code allows reset to 0 if the key is pressed more than 10 times.
else if (redKey > 10){
redValue = 0;
}
analogWrite(redPin, redValue);
Serial.print("setting red LED to ");
Serial.println(redValue);
serInString[0] = 0;
}
//green
else if (colorCode == 'g') {
greenKey++;
if (greenKey <= 10){
greenValue = greenKey * 25.5;
}
else if (greenKey > 10){
greenValue = 0;
}
analogWrite(greenPin, greenValue);
Serial.print("setting green LED to ");
Serial.println(greenValue);
serInString[0] = 0;
}
//blue
else if (colorCode == 'b') {
blueKey++;
if (blueKey <= 10){
blueValue = blueKey * 25.5;
}
else if (blueKey > 10){
blueValue = 0;
}
analogWrite(bluePin, blueValue);
Serial.print("setting blue LED to ");
Serial.println(blueValue);
serInString[0] = 0;
}
i++;
}
}
- Login to post comments
Drupal theme by Kiwi Themes.