const int greenPin = 10;
const int bluePin = 9;
int red = 0;
int green = 0;
int blue = 0;
const int leftButton = 4;
const int rightButton = 3;
int right = 0;
int left = 0;
int value = 0;
int colorValue = 0;
void setup() {
// put your setup code here, to run once:
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
pinMode(leftButton, INPUT);
pinMode(rightButton, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
right = digitalRead(rightButton);
left = digitalRead(leftButton);
if (right == HIGH){
if (value == 1529){
value = 0;
}else{
value ++;
}
}else if (left == HIGH){
if (value == 0){
value = 1529;
}else{
value --;
}
}
red = calculate(1);
green = calculate(2);
blue = calculate(3);
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
delay(5);
}
int calculate(int whichColor){
if (whichColor == 1){
if ((value > -1) && (value < 256)){
colorValue = 255;
}else if ((value > 255) && (value < 511)){
colorValue = 255 - (value - 255);
}else if ((value > 510) && (value < 766)){
colorValue = 0;
}else if ((value > 765) && (value < 1021)){
colorValue = 0;
}else if ((value > 1020) && (value < 1276)){
colorValue = value - 1020;
}else if ((value > 1275) && (value < 1530)){
colorValue = 255;
}
}else if (whichColor == 2){
if ((value > -1) && (value < 256)){
colorValue = value;
}else if ((value > 255) && (value < 511)){
colorValue = 255;
}else if ((value > 510) && (value < 766)){
colorValue = 255;
}else if ((value > 765) && (value < 1021)){
colorValue = 255 - (value - 765);
}else if ((value > 1020) && (value < 1276)){
colorValue = 0;
}else if ((value > 1275) && (value < 1530)){
colorValue = 0;
}
}else if (whichColor == 3){
if ((value > -1) && (value < 256)){
colorValue = 0;
}else if ((value > 255) && (value < 511)){
colorValue = 0;
}else if ((value > 510) && (value < 766)){
colorValue = value - 510;
}else if ((value > 765) && (value < 1021)){
colorValue = 255;
}else if ((value > 1020) && (value < 1276)){
colorValue = 255;
}else if ((value > 1275) && (value < 1530)){
colorValue = 255 - (value - 1275);
}
}
return colorValue;
}
No comments:
Post a Comment