2 /* Arduino rgb led driver */
3 /* Copyright (c) 2014 Clinton Ebadi <clinton@unknownlamer.org> */
4 /* This program is free software: you can redistribute it and/or modify */
5 /* it under the terms of the GNU General Public License as published by */
6 /* the Free Software Foundation, either version 3 of the License, or */
7 /* (at your option) any later version. */
9 /* This program is distributed in the hope that it will be useful, */
10 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
11 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
12 /* GNU General Public License for more details. */
14 /* You should have received a copy of the GNU General Public License */
15 /* along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 const int led_color_max = pow (2, 16) - 1;
19 const int dither_steps = pow (2, DITHER_BITS);
21 typedef struct rgb_pins
28 const int rgb_banks = 2;
30 //const rgb_pins pins[rgb_banks] = { {10, 9, 11},
32 const rgb_pins pins[rgb_banks] = { {9, 6, 10},
35 void set_color_x (int r, int g, int b, int dither_step = 0)
37 int offset = dither_step * 256;
39 for (int bank = 0; bank < rgb_banks; bank++)
41 analogWrite (pins[bank].red, r);
42 analogWrite (pins[bank].green, g);
43 analogWrite (pins[bank].blue, b);
49 Serial.begin (115200);
50 Serial.setTimeout (20000);
51 analogWriteResolution (16);
52 randomSeed (analogRead (14));
54 for (int bank = 0; bank < rgb_banks; bank++)
56 pinMode (pins[bank].red, OUTPUT);
57 pinMode (pins[bank].green, OUTPUT);
58 pinMode (pins[bank].blue, OUTPUT);
61 // Test sequence to confirm wiring
62 set_color_x (led_color_max, led_color_max, led_color_max);
64 set_color_x (led_color_max, 0, 0);
66 set_color_x (0, led_color_max, 0);
68 set_color_x (0, 0, led_color_max);
74 static int r = led_color_max / 2;
75 static int g = led_color_max / 2;
76 static int b = led_color_max / 2;
79 //step = (step + 1) % dither_steps;
80 step = 0; // dithering is bogus/i'm-not-doing-it-right
82 if (Serial.available ())
84 r = Serial.parseInt ();
85 g = Serial.parseInt ();
86 b = Serial.parseInt ();
88 Serial.print ("set color ");
96 set_color_x (r, g, b);
99 // display random colors when the serial port is closed
102 set_color_x (random (led_color_max), random (led_color_max), random (led_color_max));
106 set_color_x (r, g, b, step);