// description
my goal for the second part of this assignment was to cause the program to examine the string of text that was entered into the serial terminal, and output power values to the three leds proportionate to the distribution of the letters "r", "g" and "b" in the text input.
I accomplished this objective using a "for" statement, which caused the program to examine each place in the serial array, from 0-100, and tally up the incidence of those three letters, with each letter increasing the ultimate brightness of the led by 15. Thus, entering 17 "r"'s would result in a brightness of 255 for the red led, which is its max.
// components used
1x arduino uno
1x breadboard
1x red, green, blue leds
3x 330ohm resistor
1x ping pong ball
// code
char serInString[100]; // array that will hold the different bytes of the string. 100=100characters;
char colorCode;
int cValR;
int cValG;
int cValB;
int red = 9; // Red LED, connected to digital pin 9
int green = 10; // Green LED, connected to digital pin 10
int blue = 11; // Blue LED, connected to digital pin 11
void setup() {
pinMode(red, OUTPUT); // sets the pins as output
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
Serial.begin(9600);
analogWrite(red, 255); // set them all to max brightness to signal initiation
analogWrite(green, 255);
analogWrite(blue, 255);
Serial.println("enter letters corresponding to the led colors (e.g 'r', 'g') for each occurence of a letter, the brightness of that led will increase :");
}
void loop () {
memset(serInString, 0, 100); // clear the string
readSerialString(serInString); //read the serial port and create a string out of what you read
colorCode = serInString[0];
cValR = 0; //set initial values for the 3 leds to 0
cValG = 0;
cValB = 0;
if( colorCode == 'r' || colorCode == 'g' || colorCode == 'b' ) {
for(int x=0; x < 100 ; x++) {
if( serInString[x] == 'r') //increase the brightness of the led by 15 for each instance of the related letter
cValR = cValR + 15;
else if( serInString[x] == 'g')
cValG = cValG + 15;
else if( serInString[x] == 'b')
cValB = cValB + 15;
}
Serial.println(); //tell the user what their input was
Serial.print("i see: ");
Serial.print(cValR / 15);
Serial.print(" r's ");
Serial.println();
Serial.print("setting color ");
Serial.print("red");
Serial.print(" to ");
Serial.print(cValR);
Serial.println();
Serial.println();
Serial.print("i see: ");
Serial.print(cValG / 15);
Serial.print(" g's ");
Serial.println();
Serial.print("setting color ");
Serial.print("green");
Serial.print(" to ");
Serial.print(cValG);
Serial.println();
Serial.println();
Serial.print("i see: ");
Serial.print(cValB / 15);
Serial.print(" b's ");
Serial.println();
Serial.print("setting color ");
Serial.print("blue");
Serial.print(" to ");
Serial.print(cValB);
Serial.println();
Serial.println("-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+");
analogWrite(red, cValR); //set the values of the leds to the levels that have been indicated
analogWrite(green, cValG);
analogWrite(blue, cValB);
}
delay(100); // 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()) {
return;
}
while (Serial.available()) {
strArray[i] = Serial.read();
i++;
}
}