Description
I created a rainbow ball. It allows users to type the initial character of a rainbow color, and LEDs inside the ball will graduate changes their brightness to show this color. It accepts 7 characters from users:
- ‘r’: set the ball to red;
- ‘o’: set the ball to orange;
- ‘y’: set the ball to yellow;
- ‘g’: set the ball to green;
- ‘c’: set the ball to cyan;
- ‘b’: set the ball to blue;
- ‘p’: set the ball to purple.
Components
- 1 Arduino
- 1 Diffuser (cotton ball)
- 3 LEDs
- 3 Resistors (220Ω)
- 7 Jumper wires
- 1 USB cable
- 1 Breadboard
Code
char serInString[100]; int redVal, greenVal, blueVal; int redCurr = 127; int greenCurr = 127; int blueCurr = 127; int increRed, increBlue, increGreen; int redPin = 9; int greenPin = 10; int bluePin = 11; int redPre = 127; int bluePre = 127; int greenPre = 127; void setup() { pinMode(redPin, OUTPUT); pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT); Serial.begin(9600); analogWrite(redPin, redPre); analogWrite(greenPin, bluePre); analogWrite(bluePin, greenPre); Serial.println("enter a rainbow color ('r' for Red, 'o' for Orange, 'y' for Yellow, 'g' for Green, 'c' for Cyan, 'b' for Blue, 'p' for Purple)"); } void printSerial() { Serial.print("setting color "); Serial.print("red"); Serial.print(" to "); Serial.print(redCurr); Serial.println(); Serial.print("setting color "); Serial.print("blue"); Serial.print(" to "); Serial.print(blueCurr); Serial.println(); Serial.print("setting color "); Serial.print("green"); Serial.print(" to "); Serial.print(greenCurr); Serial.println(); } void loop () { memset(serInString, 0, 100); readSerialString(serInString); if( serInString[0]== 'r') { redCurr = 255; greenCurr = 0; blueCurr = 0; printSerial(); serInString[0] = ' '; } if( serInString[0]== 'o') { redCurr = 255; greenCurr = 127; blueCurr = 0; printSerial(); serInString[0] = ' '; } if( serInString[0]== 'y') { redCurr = 255; greenCurr = 255; blueCurr = 0; printSerial(); serInString[0] = ' '; } if( serInString[0]== 'g') { redCurr = 0; greenCurr = 255; blueCurr = 0; printSerial(); serInString[0] = ' '; } if( serInString[0]== 'c') { redCurr = 0; greenCurr = 255; blueCurr = 255; printSerial(); serInString[0] = ' '; } if( serInString[0]== 'b') { redCurr = 0; greenCurr = 0; blueCurr = 255; printSerial(); serInString[0] = ' '; } if( serInString[0]== 'p') { redCurr = 127; greenCurr = 0; blueCurr = 255; printSerial(); serInString[0] = ' '; } if(redPre > redCurr) { increRed = -1; } else { increRed = 1; } redVal = redPre; while(redVal != redCurr) { analogWrite(redPin, redVal); delay(10); redVal += increRed; } redPre = redCurr; if(bluePre > blueCurr) { increBlue = -1; } else { increBlue = 1; } blueVal = bluePre; while(blueVal != blueCurr) { analogWrite(bluePin, blueVal); delay(10); blueVal += increBlue; } bluePre = blueCurr; if(greenPre > greenCurr) { increGreen = -1; } else { increGreen = 1; } greenVal = greenPre; while(greenVal != greenCurr) { analogWrite(greenPin, greenVal); greenVal += increGreen; delay(10); } greenPre = greenCurr; delay(100); } void readSerialString (char *strArray) { int j = 0; if(!Serial.available()) { return; } while (Serial.available()) { strArray[j] = Serial.read(); j++; } }