Assignment: Digital I/O with Arduino Boards + Diffuser
Collaborators:
Assignment: Digital I/O with Arduino Boards + Diffuser
Collaborators:
The purpose of this lab was to:
#include 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 int redValue = 0; int greenValue = 0; int blueValue = 0; void setup() { pinMode(redPin, OUTPUT); // sets the pins as output pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT); Serial.begin(9600); analogWrite(redPin, redValue); // set them all to mid brightness analogWrite(greenPin, greenValue); // set them all to mid brightness analogWrite(bluePin, blueValue); // set them all to mid brightness Serial.println("please enter a code to modify the color of the ghost: "); Serial.println("b = Blue"); Serial.println("g = Green"); Serial.println("y = Yellow"); Serial.println("p = Pink"); Serial.println("r = Red"); } void loop () { //read the serial port and create a string out of what you read readSerialString(serInString, 100); changeColor(serInString, 100); resetSerialString(serInString, 100); delay(100); // wait a bit, for serial data } void resetSerialString (char *strArray, int length) { for (int i = 0; i < length; i++) { strArray[i] = '\0'; } } //read a string from the serial and store it in an array //you must supply the array variable void readSerialString (char *strArray, int maxLength) { int i = 0; if(!Serial.available()) { return; } while (Serial.available() && i < maxLength) { strArray[i] = Serial.read(); i++; } } //depending on the color code, adjust the color: void changeColor(char *strArray, int maxLength) { int i = 0; while (i < maxLength && strArray[i] != '\0') { colorCode = serInString[i]; switch(colorCode) { case 'p': Serial.println("setting color to pink..."); greenValue = 0; blueValue = 30; redValue = 255; break; case 'r': Serial.println("setting color to red..."); greenValue = 0; blueValue = 0; redValue = 255; break; case 'b': Serial.println("setting color to blue..."); greenValue = 0; blueValue = 255; redValue = 0; break; case 'g': Serial.println("setting color to green..."); greenValue = 255; blueValue = 0; redValue = 0; break; case 'y': Serial.println("setting color to yellow..."); greenValue = 131; blueValue = 7; redValue = 245; break; } analogWrite(greenPin, greenValue); analogWrite(bluePin, blueValue); analogWrite(redPin, redValue); //increment counter: i++; } } //depending on the color code, adjust the color: void changeColorByName(char *strArray, int maxLength) { if(stringsEqual(strArray, "blue", 4)) { Serial.println("setting color to blue..."); greenValue = 0; blueValue = 255; redValue = 0; } else if(stringsEqual(strArray, "red", 3)) { Serial.println("setting color to red..."); greenValue = 0; blueValue = 0; redValue = 255; } else if(stringsEqual(strArray, "pink", 4)) { Serial.println("setting color to pink..."); greenValue = 0; blueValue = 30; redValue = 255; } else if(stringsEqual(strArray, "yellow", 6)) { Serial.println("setting color to yellow..."); greenValue = 131; blueValue = 7; redValue = 245; } else if(stringsEqual(strArray, "green", 5)) { Serial.println("setting color to green..."); greenValue = 255; blueValue = 0; redValue = 0; } else { Serial.println("Color code not recognized."); } analogWrite(greenPin, greenValue); analogWrite(bluePin, blueValue); analogWrite(redPin, redValue); }
Photo 1: Circuit Without Diffuser:
Photo 2: Blue Diffused Light:
Photo 3: Pink Diffused Light: