The circuit is built with:
3 LED's (R G B)
3 resistors (220)
A ping-pong ball with the text "Go Bears"
Code:
char serInString[100]; // Array with different bytes of string, up to 100
char colorCode;
int colorVal;
int redPin = 9; // Red LED on 9
int greenPin = 10; // Green LED on 10
int bluePin = 11; // Blue LED on 11
int redVal = 0;
int greenVal = 0;
int blueVal = 0;
void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600); // Write to output log
// Set all LED's to mid brightness (0.5 * 255)
analogWrite(redPin, 255);
analogWrite(greenPin, 255);
analogWrite(bluePin, 255);
// Print first statement so the user knows what to do
Serial.println("Enter color command (e.g. 'r43') :");
}
void loop() {
// clear the string
memset(serInString, 0, 100);
// Read the serial port
readSerialString(serInString);
colorCode = serInString[0];
// If the user either types in r, g, or b
if(colorCode == 'r' || colorCode == 'g' || colorCode == 'b')
{
// If the length of the char array is just 2 than the input from the user was just
// r, g or b. In that case I want to raise the value by 10%
if(strlen(serInString) > 2) {
colorVal = atoi(serInString+1);
}
else
{
if(colorCode == 'r') {
redVal += 10;
colorVal = (redVal * 255) / 100;
if(redVal >= 100) {
redVal = 0;
}
}
else if(colorCode == 'g') {
greenVal += 10;
colorVal = (greenVal * 255) / 100;
if(greenVal >= 100) {
greenVal = 0;
}
}
else if(colorCode == 'b') {
blueVal += 10;
colorVal = (blueVal * 255) / 100;
if(blueVal >= 100) {
blueVal = 0;
}
}
}
// Let the user know that the system is processing the input
Serial.print("setting color ");
Serial.print(colorCode);
Serial.print(" to ");
Serial.print(colorVal);
Serial.println();
serInString[0] = 0; // Overwrite value with 0 so that it is not used again
if(colorCode == 'r') {
analogWrite(redPin, colorVal);
}
else if(colorCode == 'g') {
analogWrite(greenPin, colorVal);
}
else if(colorCode == 'b') {
analogWrite(bluePin, colorVal);
}
}
delay(100);
}
void readSerialString(char *strArray) {
int i = 0;
// Stop if the serial is not available (serial.begin() was not called)
if(!Serial.available()) {
return;
}
// As long as the Serial is available read from the serial port
while(Serial.available()) {
strArray[i] = Serial.read();
i++;
}
}
I also built an eclipse project in Java which sends commands such as: r0 or r255 and g10 etc. to Arduino.
This is ofcourse with a different user interface.
This code does the following:
When either, r, g, or b is inserted withouth an additional value the system will raise the desired color with 10% (starting from 0). Otherwise the system will use the value inserted by the user.
- Login to post comments