/*
* I262 - Assingment 2
* ---------------
* Function : Key press controls the brightness of R,G,B LEDs
* Date : 9.24.2013
* Author : Suhaib Saqib Syed
* Reference : copyleft 2006 Tod E. Kurt <tod@todbot.com
* http://todbot.com/
*/
char serInString[10]; // 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 rcount; //To keep a count of the number of times 'r' was pressed
int gcount; //To keep a count of the number of times 'r' was pressed
int bcount; //To keep a count of the number of times 'r' was pressed
int redPin = 11; // Red LED, connected to digital pin 9
int greenPin = 10; // Green LED, connected to digital pin 10
int bluePin = 9; // 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 zero brightness
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
Serial.println("enter color ten characters consisting or 'r', 'g' and 'b' :");
}
void loop () {
// clear the string
memset(serInString, 0, 10);
//read the serial port and create a string out of what you read
readSerialString(serInString);
for (int i = 0; i < 10; i++){
//perform for all the ten characters in the string
colorCode = serInString[i];
if( colorCode == 'r' ){
rcount = increment(rcount);
colorVal = rcount * 25.5;
analogWrite(redPin, colorVal);
printstatus(colorCode, colorVal);
} else if( colorCode == 'g' ) {
gcount = increment(gcount);
colorVal = gcount * 25.5;
analogWrite(greenPin, colorVal);
printstatus(colorCode, colorVal);
} else if( colorCode == 'b' ) {
bcount = increment(bcount);
colorVal = bcount * 25.5;
analogWrite(bluePin, colorVal);
printstatus(colorCode, colorVal);
}}
delay(100); // wait a bit, for serial data
}
//Function to print the status
void printstatus(char x, int y){
Serial.print("setting color ");
Serial.print(x);
Serial.print(" to ");
Serial.print(y);
Serial.println();
}
//Function to Increment till 10 and then reset to zero
int increment(int x){
if( x < 10 ) {
x = x + 1;
} else {
x = 0; }
return x;
}
//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++;
}
}
- Login to post comments