| 1 | /* -*- c -*- */ |
| 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. */ |
| 8 | |
| 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. */ |
| 13 | |
| 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/>. */ |
| 16 | |
| 17 | #define DITHER_BITS 1 |
| 18 | const int led_color_max = pow (2, 16) - 1; |
| 19 | const int dither_steps = pow (2, DITHER_BITS); |
| 20 | |
| 21 | typedef struct rgb_pins |
| 22 | { |
| 23 | int red; |
| 24 | int green; |
| 25 | int blue; |
| 26 | } rgb_pins; |
| 27 | |
| 28 | const int rgb_banks = 2; |
| 29 | |
| 30 | //const rgb_pins pins[rgb_banks] = { {10, 9, 11}, |
| 31 | // {5, 6, 3} }; |
| 32 | const rgb_pins pins[rgb_banks] = { {9, 6, 10}, |
| 33 | {4, 5, 3} }; |
| 34 | |
| 35 | void set_color_x (int r, int g, int b, int dither_step = 0) |
| 36 | { |
| 37 | int offset = dither_step * 256; |
| 38 | |
| 39 | for (int bank = 0; bank < rgb_banks; bank++) |
| 40 | { |
| 41 | analogWrite (pins[bank].red, r); |
| 42 | analogWrite (pins[bank].green, g); |
| 43 | analogWrite (pins[bank].blue, b); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | void setup () |
| 48 | { |
| 49 | Serial.begin (115200); |
| 50 | Serial.setTimeout (20000); |
| 51 | analogWriteResolution (16); |
| 52 | randomSeed (analogRead (14)); |
| 53 | |
| 54 | for (int bank = 0; bank < rgb_banks; bank++) |
| 55 | { |
| 56 | pinMode (pins[bank].red, OUTPUT); |
| 57 | pinMode (pins[bank].green, OUTPUT); |
| 58 | pinMode (pins[bank].blue, OUTPUT); |
| 59 | } |
| 60 | |
| 61 | // Test sequence to confirm wiring |
| 62 | set_color_x (led_color_max, led_color_max, led_color_max); |
| 63 | delay(500); |
| 64 | set_color_x (led_color_max, 0, 0); |
| 65 | delay (500); |
| 66 | set_color_x (0, led_color_max, 0); |
| 67 | delay (500); |
| 68 | set_color_x (0, 0, led_color_max); |
| 69 | delay (500); |
| 70 | } |
| 71 | |
| 72 | void loop () |
| 73 | { |
| 74 | static int r = led_color_max / 2; |
| 75 | static int g = led_color_max / 2; |
| 76 | static int b = led_color_max / 2; |
| 77 | static int step = -1; |
| 78 | |
| 79 | //step = (step + 1) % dither_steps; |
| 80 | step = 0; // dithering is bogus/i'm-not-doing-it-right |
| 81 | |
| 82 | if (Serial.available ()) |
| 83 | { |
| 84 | r = Serial.parseInt (); |
| 85 | g = Serial.parseInt (); |
| 86 | b = Serial.parseInt (); |
| 87 | |
| 88 | Serial.print ("set color "); |
| 89 | Serial.print (r); |
| 90 | Serial.print (" "); |
| 91 | Serial.print (g); |
| 92 | Serial.print (" "); |
| 93 | Serial.print (b); |
| 94 | Serial.println (); |
| 95 | |
| 96 | set_color_x (r, g, b); |
| 97 | } |
| 98 | |
| 99 | // display random colors when the serial port is closed |
| 100 | if (!Serial.dtr ()) |
| 101 | { |
| 102 | set_color_x (random (led_color_max), random (led_color_max), random (led_color_max)); |
| 103 | delay (1500); |
| 104 | } |
| 105 | |
| 106 | set_color_x (r, g, b, step); |
| 107 | |
| 108 | } |