Description
This second lab was an introduction to digital input and output on the Arduino Uno.
My hardware consisted of 3 LEDs (colors: red, green and blue) placed in parallel and spatially close to one another on the breadboard. I designed a diffuser using a tennis ball packet and plastic bag. I put this diffuser over the 3 LEDs so that when on in certain brightness configurations the diffusion of those 3 colors would produce other colors.
My code utilized if loops to set the LEDs to different brightness levels. The user input choices were the following (using example color red):
‘r’ = off
‘rr’ = medium brightness
‘rrr’ = high brightness
ComponentsUsed
1 – Arduino Uno
1 – Mini Breadboard
1 – Red LED
1 – Green LED
1 – Blue LED
1 – Diffuser (tennis ball holder and plastic bag)
3 – 220 Ω Resistors
Code
/*
Serial Lab #2
*/
char serInString[100]; //creation of array
char LED; //LED is a character input
int comLength; //comLength is an integar input
int redPin = 9; //Red LED connected to pin 9
int greenPin = 10; //Green LED connected to pin 10
int bluePin = 11; //Blue LED connected to pin 11
void setup() {
pinMode(redPin, OUTPUT); //sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
analogWrite(redPin, 127); //set red pin to mid brightness
analogWrite(greenPin, 127); //set green pin to mid brightness
analogWrite(bluePin, 127); //set blue pin to mid brightness
Serial.println("enter desired brightness (ex. r for low, rr for med, rrr for high");
}
void loop () {
//clear the string
memset(serInString,0,100); //red the serial port and create a string out of what you read
readSerialString(serInString);
LED = serInString[0];
if(LED == 'r'){
comLength = strlen(serInString);
if(comLength == 1){
analogWrite(redPin, 0);
Serial.print("Turning red led off");
Serial.println();}
if(comLength == 2){
analogWrite(redPin, 127);
Serial.print("Turning red led to medium brightness");
Serial.println();}
if(comLength == 3){
analogWrite(redPin, 255);
Serial.print("Turning red led to high brightness");
Serial.println();}
}
if(LED == 'g'){
comLength = strlen(serInString);
if(comLength == 1){
analogWrite(greenPin, 0);
Serial.print("Turning green led off");
Serial.println();}
if(comLength == 2){
analogWrite(greenPin, 127);
Serial.print("Turning green led to medium brightness");
Serial.println();}
if(comLength == 3){
analogWrite(greenPin, 255);
Serial.print("Turning green led to high brightness");
Serial.println();}
}
if(LED == 'b'){
comLength = strlen(serInString);
if(comLength == 1){
analogWrite(bluePin, 0);
Serial.print("Turning blue led off");
Serial.println();}
if(comLength == 2){
analogWrite(bluePin, 127);
Serial.print("Turning blue led to medium brightness");
Serial.println();}
if(comLength == 3){
analogWrite(bluePin, 255);
Serial.print("Turning blue led to high brightness");
Serial.println();}
}
delay(100); //wait a bit 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