const int RED_PIN = 11;
const int GREEN_PIN = 10;
const int BLUE_PIN = 9;
const int R = 0;
const int G = 1;
const int B = 2;
int colors[][3] = { { 255, 0, 0 }, { 225, 127, 0 }, { 225, 255, 0 }, { 0, 255, 0 }, { 0, 0, 255 }, { 75, 0, 130 }, { 148, 0, 211 } };
int curentColor[3] = {};
const int COLORS_COUNT = sizeof(colors) / sizeof(colors[0]);
void setup() {
Serial.begin(9600);
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
for (int i = R; i <= B; i++) {
curentColor[i] = colors[0][i];
}
}
int nextColorIndex = 1;
void loop() {
setColor(curentColor[R], curentColor[G], curentColor[B]);
/* change current color to next color */
for (int i = R; i <= B; i++) {
curentColor[i] = curentColor[i] > colors[nextColorIndex][i] ? curentColor[i] - 1 : curentColor[i] < colors[nextColorIndex][i] ? curentColor[i] + 1 : curentColor[i];
}
bool colorsComplete = curentColor[R] == colors[nextColorIndex][R] && curentColor[G] == colors[nextColorIndex][G] && curentColor[B] == colors[nextColorIndex][B];
if (colorsComplete) {
nextColorIndex = nextColorIndex == COLORS_COUNT - 1 ? 0 : ++nextColorIndex;
}
delay(10);
}
int random(int min, int max) {
return min + rand() % ((max - min) + 1);
}
void setColor(int redValue, int greenValue, int blueValue) {
analogWrite(RED_PIN, redValue);
analogWrite(GREEN_PIN, greenValue);
analogWrite(BLUE_PIN, blueValue);
}