|
| 1 | +/* |
| 2 | + Serial RGB controller |
| 3 | + |
| 4 | + Reads a serial input string looking for three comma-separated |
| 5 | + integers with a newline at the end. Values should be between |
| 6 | + 0 and 255. The sketch uses those values to set the color |
| 7 | + of an RGB LED attached to pins 9 - 11. |
| 8 | + |
| 9 | + The circuit: |
| 10 | + * Common-anode RGB LED cathodes attached to pins 9 - 11 |
| 11 | + * LED anode connected to pin 13 |
| 12 | + |
| 13 | + To turn on any given channel, set the pin LOW. |
| 14 | + To turn off, set the pin HIGH. The higher the analogWrite level, |
| 15 | + the lower the brightness. |
| 16 | + |
| 17 | + created 29 Nov 2010 |
| 18 | + by Tom Igoe |
| 19 | + |
| 20 | + This example code is in the public domain. |
| 21 | + */ |
| 22 | + |
| 23 | +// include the character conversion functions: |
| 24 | +#include <WCharacter.h> |
| 25 | + |
| 26 | +String inString = ""; // string to hold input |
| 27 | +int currentColor = 0; |
| 28 | +int red, green, blue = 0; |
| 29 | + |
| 30 | +void setup() { |
| 31 | + // Initialize serial communications: |
| 32 | + Serial.begin(9600); |
| 33 | + // set LED cathode pins as outputs: |
| 34 | + pinMode(9, OUTPUT); |
| 35 | + pinMode(10, OUTPUT); |
| 36 | + pinMode(11, OUTPUT); |
| 37 | + // turn on pin 13 to power the LEDs: |
| 38 | + pinMode(13, OUTPUT); |
| 39 | + digitalWrite(13, HIGH); |
| 40 | +} |
| 41 | + |
| 42 | +void loop() { |
| 43 | + int inChar; |
| 44 | + |
| 45 | + // Read serial input: |
| 46 | + if (Serial.available() > 0) { |
| 47 | + inChar = Serial.read(); |
| 48 | + } |
| 49 | + |
| 50 | + if (isDigit(inChar)) { |
| 51 | + // convert the incoming byte to a char |
| 52 | + // and add it to the string: |
| 53 | + inString += (char)inChar; |
| 54 | + } |
| 55 | + |
| 56 | + // if you get a comma, convert to a number, |
| 57 | + // set the appropriate color, and increment |
| 58 | + // the color counter: |
| 59 | + if (inChar == ',') { |
| 60 | + // do something different for each value of currentColor: |
| 61 | + switch (currentColor) { |
| 62 | + case 0: // 0 = red |
| 63 | + red = inString.toInt(); |
| 64 | + // clear the string for new input: |
| 65 | + inString = ""; |
| 66 | + break; |
| 67 | + case 1: // 1 = green: |
| 68 | + green = inString.toInt(); |
| 69 | + // clear the string for new input: |
| 70 | + inString = ""; |
| 71 | + break; |
| 72 | + } |
| 73 | + currentColor++; |
| 74 | + } |
| 75 | + // if you get a newline, you know you've got |
| 76 | + // the last color, i.e. blue: |
| 77 | + if (inChar == '\n') { |
| 78 | + blue = inString.toInt(); |
| 79 | + |
| 80 | + // set the levels of the LED. |
| 81 | + // subtract value from 255 because a higher |
| 82 | + // analogWrite level means a dimmer LED, since |
| 83 | + // you're raising the level on the anode: |
| 84 | + analogWrite(11, 255 - red); |
| 85 | + analogWrite(9, 255 - green); |
| 86 | + analogWrite(10, 255 - blue); |
| 87 | + |
| 88 | + // print the colors: |
| 89 | + Serial.print("Red: "); |
| 90 | + Serial.print(red); |
| 91 | + Serial.print(", Green: "); |
| 92 | + Serial.print(green); |
| 93 | + Serial.print(", Blue: "); |
| 94 | + Serial.println(blue); |
| 95 | + |
| 96 | + // clear the string for new input: |
| 97 | + inString = ""; |
| 98 | + // reset the color counter: |
| 99 | + currentColor = 0; |
| 100 | + |
| 101 | + } |
| 102 | + |
| 103 | +} |
| 104 | + |
| 105 | + |
| 106 | +/* |
| 107 | +Here's a Processing sketch that will draw a color wheel and send a serial |
| 108 | +string with the color you click on: |
| 109 | +
|
| 110 | +// Subtractive Color Wheel with Serial |
| 111 | +// Based on a Processing example by Ira Greenberg. |
| 112 | +// Serial output added by Tom Igoe |
| 113 | +// |
| 114 | +// The primaries are red, yellow, and blue. The secondaries are green, |
| 115 | +// purple, and orange. The tertiaries are yellow-orange, red-orange, |
| 116 | +// red-purple, blue-purple, blue-green, and yellow-green. |
| 117 | +// |
| 118 | +// Create a shade or tint of the subtractive color wheel using |
| 119 | +// SHADE or TINT parameters. |
| 120 | +
|
| 121 | +// Updated 29 November 2010. |
| 122 | +
|
| 123 | +
|
| 124 | +
|
| 125 | +import processing.serial.*; |
| 126 | +
|
| 127 | +int segs = 12; |
| 128 | +int steps = 6; |
| 129 | +float rotAdjust = TWO_PI / segs / 2; |
| 130 | +float radius; |
| 131 | +float segWidth; |
| 132 | +float interval = TWO_PI / segs; |
| 133 | +
|
| 134 | +Serial myPort; |
| 135 | +
|
| 136 | +void setup() { |
| 137 | + size(200, 200); |
| 138 | + background(127); |
| 139 | + smooth(); |
| 140 | + ellipseMode(RADIUS); |
| 141 | + noStroke(); |
| 142 | + // make the diameter 90% of the sketch area |
| 143 | + radius = min(width, height) * 0.45; |
| 144 | + segWidth = radius / steps; |
| 145 | +
|
| 146 | + // swap which line is commented out to draw the other version |
| 147 | + // drawTintWheel(); |
| 148 | + drawShadeWheel(); |
| 149 | + // open the first serial port in your computer's list |
| 150 | + myPort = new Serial(this, Serial.list()[0], 9600); |
| 151 | +} |
| 152 | +
|
| 153 | +
|
| 154 | +void drawShadeWheel() { |
| 155 | + for (int j = 0; j < steps; j++) { |
| 156 | + color[] cols = { |
| 157 | + color(255-(255/steps)*j, 255-(255/steps)*j, 0), |
| 158 | + color(255-(255/steps)*j, (255/1.5)-((255/1.5)/steps)*j, 0), |
| 159 | + color(255-(255/steps)*j, (255/2)-((255/2)/steps)*j, 0), |
| 160 | + color(255-(255/steps)*j, (255/2.5)-((255/2.5)/steps)*j, 0), |
| 161 | + color(255-(255/steps)*j, 0, 0), |
| 162 | + color(255-(255/steps)*j, 0, (255/2)-((255/2)/steps)*j), |
| 163 | + color(255-(255/steps)*j, 0, 255-(255/steps)*j), |
| 164 | + color((255/2)-((255/2)/steps)*j, 0, 255-(255/steps)*j), |
| 165 | + color(0, 0, 255-(255/steps)*j), |
| 166 | + color(0, 255-(255/steps)*j, (255/2.5)-((255/2.5)/steps)*j), |
| 167 | + color(0, 255-(255/steps)*j, 0), |
| 168 | + color((255/2)-((255/2)/steps)*j, 255-(255/steps)*j, 0) |
| 169 | + }; |
| 170 | + for (int i = 0; i < segs; i++) { |
| 171 | + fill(cols[i]); |
| 172 | + arc(width/2, height/2, radius, radius, |
| 173 | + interval*i+rotAdjust, interval*(i+1)+rotAdjust); |
| 174 | + } |
| 175 | + radius -= segWidth; |
| 176 | + } |
| 177 | +} |
| 178 | +
|
| 179 | +
|
| 180 | +void drawTintWheel() { |
| 181 | + for (int j = 0; j < steps; j++) { |
| 182 | + color[] cols = { |
| 183 | + color((255/steps)*j, (255/steps)*j, 0), |
| 184 | + color((255/steps)*j, ((255/1.5)/steps)*j, 0), |
| 185 | + color((255/steps)*j, ((255/2)/steps)*j, 0), |
| 186 | + color((255/steps)*j, ((255/2.5)/steps)*j, 0), |
| 187 | + color((255/steps)*j, 0, 0), |
| 188 | + color((255/steps)*j, 0, ((255/2)/steps)*j), |
| 189 | + color((255/steps)*j, 0, (255/steps)*j), |
| 190 | + color(((255/2)/steps)*j, 0, (255/steps)*j), |
| 191 | + color(0, 0, (255/steps)*j), |
| 192 | + color(0, (255/steps)*j, ((255/2.5)/steps)*j), |
| 193 | + color(0, (255/steps)*j, 0), |
| 194 | + color(((255/2)/steps)*j, (255/steps)*j, 0) |
| 195 | + }; |
| 196 | + for (int i = 0; i < segs; i++) { |
| 197 | + fill(cols[i]); |
| 198 | + arc(width/2, height/2, radius, radius, |
| 199 | + interval*i+rotAdjust, interval*(i+1)+rotAdjust); |
| 200 | + } |
| 201 | + radius -= segWidth; |
| 202 | + } |
| 203 | +} |
| 204 | +
|
| 205 | +void draw() { |
| 206 | + // nothing happens here |
| 207 | +} |
| 208 | +
|
| 209 | +void mouseReleased() { |
| 210 | + // get the color of the mouse position's pixel: |
| 211 | + color targetColor = get(mouseX, mouseY); |
| 212 | + // get the component values: |
| 213 | + int r = int(red(targetColor)); |
| 214 | + int g = int(green(targetColor)); |
| 215 | + int b = int(blue(targetColor)); |
| 216 | + // make a comma-separated string: |
| 217 | + String colorString = r + "," + g + "," + b + "\n"; |
| 218 | + // send it out the serial port: |
| 219 | + myPort.write(colorString ); |
| 220 | +} |
| 221 | +
|
| 222 | +
|
| 223 | +*/ |
| 224 | + |
| 225 | + |
| 226 | + |
| 227 | + |
| 228 | + |
| 229 | + |
| 230 | + |
| 231 | + |
| 232 | + |
| 233 | + |
0 commit comments