Prerequisites:
A RGB-LED is attached and wired to the Arduino Uno-board.
Process description:
In the beginning the LED is turned off.
A loop starts and an output-signal is put on the red-part of the LED.
With every iteration the output-signal becomes increased in the following way: 0, 1, 2, 3, ...
The loop stops after 255 (maximal red) is reached. After a delay the output-signal becomes decreased (by using a loop again) in the way: 255, 254, 253 ... until 0 is reached.
Then the increasing and decreasing starts again but this time with the green-part of the LED.
After the blue-part of the LED has been increased and decreased the whole process starts again with the red-part.
GitHub-README with Image of the board-setup
Implementation:
// -------- Declarations --------------
const int GREEN_LED_PIN = 9;
const int RED_LED_PIN = 11;
const int BLUE_LED_PIN = 10;
const int DELAY_WITHIN_LOOP = 10;
// ---------------------------------------
// ------- Own Functions -----------------
void setLed(int valRed, int valGreen, int valBlue) {
analogWrite(RED_LED_PIN, valRed);
analogWrite(GREEN_LED_PIN, valGreen);
analogWrite(BLUE_LED_PIN, valBlue);
}
void changeColorIntensity(int position, int step, int i, int stop) {
while (i != stop) {
switch (position) {
case 0:
setLed(i, 0, 0);
break;
case 1:
setLed(0, i, 0);
break;
case 2:
setLed(0, 0, i);
break;
}
i += step;
delay(DELAY_WITHIN_LOOP);
}
}
// -------------------------------------------
void setup() {
pinMode(GREEN_LED_PIN, OUTPUT);
pinMode(RED_LED_PIN, OUTPUT);
pinMode(BLUE_LED_PIN, OUTPUT);
}
void loop() {
for (int i = 0; i < 3; i++) {
changeColorIntensity(i, 1, 0, 256);
changeColorIntensity(i, -1, 255, -1);
}
}
The sketch works just fine and solves exactly the described task.
What annoys me is the part with the switch-statement and the multiple function-invocations.
If someone has an idea how to solve that part then his/her answer would be very appreciated.
Generally: What would you have done different and why?
Looking forward to reading your comments, recommendations and hints!
1 Answer 1
I agree that the switch statement smells. One way to avoid it is to abstract LED values into the array, and let change_color_intensity
operate on it:
void change_color_intensity(int which, int start, int stop, int delta)
{
int values[] = { 0, 0, 0 };
for (values[which] = start; values[which] != stop; values[which] += delta) {
set_leds(values[0], values[1], values[2]);
delay(DELAY_WITHIN_LOOP);
}
}
Note that I change a while
loop into a IMHO more readable for
. I also changed some names for the same reason.
Another approach is to abstract LED names, and get rid of setLed
altogether:
void change_color_intensity(int which, int start, int stop, int delta)
{
for (in value = start; value != stop; value += delta) {
analogWrite(which, value);
delay(DELAY_WITHIN_LOOP);
}
}
with the caller defining a pin array:
int LED = { RED_LED_PIN, GREEN_RED_PIN, BLUE_RED_PIN };
for (pin = 0; pin < 3; pin++) {
change_color_intensity(LED[pin], 0, 256, 1);
....
}
I am not sure I understand what you mean by multiple function invocations. If you are concerned with invoking change_color_intensity
twice in a loop, I see no problem with it.
-
\$\begingroup\$ I meant the three times setLed() with different parameter within the switch. Anyway: Thanks a lot for your answer. It‘s just great. \$\endgroup\$michael.zech– michael.zech2018年08月20日 15:03:04 +00:00Commented Aug 20, 2018 at 15:03