-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcirc12.html
492 lines (459 loc) · 18.8 KB
/
circ12.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
<!doctype html>
<html class="no-js" lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>ardx:circ-12[.:wcrs:.]</title>
<meta name="description" content="Arduino Experimentation Kit RGB LED circuit">
<!-- #include «head.html» -->
<meta name="viewport" content="width=device-width, initial-scale=1">
<!--[if IE]><link rel="shortcut icon" href="favicon.ico"><![endif]-->
<link rel="apple-touch-icon" href="apple-touch-icon.png">
<link rel="icon" href="favicon.png">
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/main.css">
<script src="js/vendor/modernizr-2.8.3.min.js"></script>
</head>
<body>
<!--[if lt IE 8]>
<p class="browserupgrade">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
<![endif]-->
<!-- #include «topnav.html» -->
<nav id="topnav">
<a id="logo" href="https://wcrsyyc.github.io/"><img
src="img/wcrs_logo.png" alt="logo"></a>
<h2><a href="index.html">WCRS</a></h2>
<ul>
<li><a href="https://github.com/WCRSyyc/ardx">about</a> |</li>
<li><a href="https://github.com/WCRSyyc/ardx/issues">contact</a> |</li>
<li><a href="https://github.com/WCRSyyc/ardx/wiki">wiki</a></li>
</ul>
<hr>
<p><!-- current message text --></p>
</nav>
<!-- #include «sidebar.html» -->
<nav id="sidebar">
<section id="menu">
<ul>
<li><h2>PRODUCTS</h2>
<ul>
<li><a href="https://github.com/WCRSyyc/ardx/"><h3>(WCRS)</h3>ARDX additions</a>
<ul>
<li><a href="preface.html">
<h4>ABOUT</h4>A Few Words</a></li>
<!-- <li><a href="assembling.html">
<h4>ASSMB</h4>Putting It Together</a></li> -->
<li><a href="starthere.html">
<h4>START</h4>Startup Links</a></li>
<li><a href="programming.html">
<h4>PROG</h4>Programming Primer</a></li>
<li><a href="electronics.html">
<h4>ELEC</h4>Electronics Primer</a></li>
<li><a href="fritzing.html">
<h4>FRITZING</h4>Fritzing with ARDX</a></li>
<li><a href="circ01.html">
<h4>CIRC-01</h4>Blinking LED</a></li>
<li><a href="circ02.html">
<h4>CIRC-02</h4>Multiple LEDs</a></li>
<li><a href="circ03.html">
<h4>CIRC-03</h4>Transistors & Motors</a></li>
<li><a href="circ04.html">
<h4>CIRC-04</h4>Servos</a></li>
<li><a href="circ05.html">
<h4>CIRC-05</h4>74HC595 Shift Registers</a></li>
<li><a href="circ06.html">
<h4>CIRC-06</h4>Piezo Speaker</a></li>
<li><a href="circ07.html">
<h4>CIRC-07</h4>Pushbuttons</a></li>
<li><a href="circ08.html">
<h4>CIRC-08</h4>Potentiometers</a></li>
<li><a href="circ09.html">
<h4>CIRC-09</h4>Photo-Resistors</a></li>
<li><a href="circ10.html">
<h4>CIRC-10</h4>Temperature</a></li>
<li><a href="circ11.html">
<h4>CIRC-11</h4>Relays</a></li>
<li><a href="circ12.html">
<h4>CIRC-12</h4>RGB LED blink</a></li>
<li><a href="circ15.html">
<h4>CIRC-15</h4>Dice Simulator</a></li>
</ul>
</li>
<li><a href="https://github.com/WCRSyyc/ardx/"><h3>(WCRS)</h3>Python ARDX</a>
<ul>
<!-- <li><a href="preface.html">
<h4>ABOUT</h4>A Few Words</a></li> -->
<!-- <li><a href="assembling.html">
<h4>ASSMB</h4>Putting It Together</a></li> -->
<!-- <li><a href="starthere.html">
<h4>START</h4>Startup Links</a></li> -->
<!-- <li><a href="programming.html">
<h4>PROG</h4>Programming Primer</a></li> -->
<li><a href="electronics.html">
<h4>ELEC</h4>Electronics Primer</a></li>
<li><a href="fritzing.html">
<h4>FRITZING</h4>Fritzing with ARDX</a></li>
<li><a href="ArdxPythonHowTo.html">
<h4>GUIDE</h4>Python Guide</a></li>
<!--
<li><a href="circ01p.html">
<h4>CIRC-01</h4>Blinking LED</a></li>
<li><a href="circ02p.html">
<h4>CIRC-02</h4>Multiple LEDs</a></li>
<li><a href="circ03p.html">
<h4>CIRC-03</h4>Transistors & Motors</a></li>
<li><a href="circ04p.html">
<h4>CIRC-04</h4>Servos</a></li>
<li><a href="circ05p.html">
<h4>CIRC-05</h4>74HC595 Shift Registers</a></li>
<li><a href="circ06p.html">
<h4>CIRC-06</h4>Piezo Speaker</a></li>
<li><a href="circ07p.html">
<h4>CIRC-07</h4>Pushbuttons</a></li>
<li><a href="circ08p.html">
<h4>CIRC-08</h4>Potentiometers</a></li>
<li><a href="circ09p.html">
<h4>CIRC-09</h4>Photo-Resistors</a></li>
<li><a href="circ10p.html">
<h4>CIRC-10</h4>Temperature</a></li>
<li><a href="circ11p.html">
<h4>CIRC-11</h4>Relays</a></li>
<li><a href="circ12p.html">
<h4>CIRC-12</h4>RGB LED blink</a></li>
<li><a href="circ15p.html">
<h4>CIRC-15</h4>Dice Simulator</a></li> -->
</ul>
</li>
<li><a href="http://www.oomlout.com/a/products/ardx/"><h3>(ARDX)</h3>arduino / netduino experimenter´s kit</a>
</li>
</ul>
</li>
</ul>
</section>
<section>
<h3><a rel="license" href="http://creativecommons.org/licenses/by/4.0/">
<img alt="Creative Commons License"
src="img/80x15.png"></a></h3>
This work is licensed under a <a rel="license"
href="http://creativecommons.org/licenses/by/4.0/">Creative Commons
Attribution 4.0 International License</a>.
</section>
</nav>
<main id="circ-12">
<header>
<h1>CIRC<br/>12</h1>
<h2>.:LED Colours:.<br/>.:single RGB LED:.</h2>
<img src="img/rgbled-100.jpg" width="100" height="100" alt="RGB LED">
</header>
<h2>(ARDX) .:Arduino Experimenter´s Kit:. (ARDX)</h2>
<section>
<h2>What We're Doing</h2>
<p>We previously caused a single colour LED to blink. Now we will blink an
RGB LED in each of its possible (full brightness) colours. An RGB LED is
really 3 LEDs (Red, Green, Blue) in a single package. Each can be turned on
(and off) separately. With 3 LEDs, there are 8 possible states:</p>
<ol>
<li>Red off, Green off, Blue off (black)</li>
<li>Red off, Green off, Blue on (blue)</li>
<li>Red off, Green on, Blue off (green)</li>
<li>Red off, Green on, Blue on (cyan)</li>
<li>Red on, Green off, Blue off (red)</li>
<li>Red on, Green off, Blue on (purple)</li>
<li>Red on, Green on, Blue off (yellow)</li>
<li>Red on, Green on, Blue on (white)</li>
</ol>
<p>Along with controlling the RGB LED, we look at some more programming methods
to keep your programs small, organized, and easier to change later.</p>
</section>
<section id="circuit">
<h2>The Circuit</h2>
<div id="CIRC-theparts">
<h3>The Parts</h3>
<table>
<tr>
<td><img src="img/rgbled-36x60.png"
width="60" height="36" alt="RGB led"/></td>
<td>RGB LED<br/>Common Cathode<br/>x1</td>
<td><img src="img/parts/WIRE-01-60.png"
width="60" height="36" alt="wire"/></td>
<td>Wire</td>
<td><img src="img/parts/RES-561-60.png"
width="60" height="36" alt="resistor"/></td>
<td>560 Ohm Resistor<br/>Green-Blue-Brown<br/>x3</td>
</tr>
</table>
</div>
<span class="bb-svg-container"><svg
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
viewBox="41 13 58.5 58.5"
preserveAspectRatio="xMaxYMin meet">
<image height="100" width="100"
xlink:href="img/CIRC12_bb.svg"/>
</svg></span>
<div>
<h3>Schematic</h3>
<span class="sch-svg-container"><svg
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
viewBox="63.7 44 44 44"
preserveAspectRatio="xMidYMin meet">
<image height="100" width="100"
xlink:href="img/CIRC12c_schem.svg" />
</svg></span>
</div>
<div>
<h3>Schematic</h3>
<span class="sch-svg-container"><svg
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
viewBox="64.5 26 40 40"
preserveAspectRatio="xMidYMin meet">
<image height="100" width="100"
xlink:href="img/CIRC12a_schem.svg" />
</svg></span>
</div>
<div>
<h3>Resources</h3>
<h4>.:download:.</h4>
<p>
Fritzing Diagram<br/>
<a href="fritzing/CIRC12.fzz">https://wcrsyyc.github.io/ardx/fritzing/CIRC12.fzz</a>
</p>
</div>
</section>
<section id="code">
<h2>Code <span>(no need to type everything in just)</span></h2>
<p>Download the Code from ( <a
href="code/CIRC12-code.txt">
https://wcrsyyc.github.io/ardx/code/CIRC12-code.txt</a> )<br/>
(and then copy the text and paste it into an empty Arduino Sketch)</p>
<pre>/*
RGB_Blink
Cycle through colours on an RGB LED.
This example code is in the public domain.
This code is setup to cycle the colours for either a common cathode or a common
anode RGB LED. To switch the code from one to the other, it is only necessary to
change the lines initializing the COLOUR_ON and COLOUR_OFF values. The circuit
difference is whether the common lead is connected to ground (for common
cathode), or to +5 (for common anode) line. This has the effect of inverting
the LED states. A HIGH signal turns on one colour of a common cathode RGB LED,
but a HIGH signal turns off one colour of a common anode RGB LED. Reversing
the COLOUR_xxx values compensates for that.
modified 2017/01/24
by H. Phil Duby
Reference:
http://www.learningaboutelectronics.com/Articles/Common-cathode-RGB-LED-circuit.php
http://www.learningaboutelectronics.com/Articles/Common-anode-RGB-LED-circuit.php
*/
// Instead of specifying (and repeating) some common values in the actual code,
// put them in variables with informative names at the beginning of the file,
// then use the variables in the sketch. This can both make the code easier to
// understand, and allow changes involving the named values much simpler. They
// would only need changing in one place.
// Setup the values needed to turn an LED (colour) on and off. To use a common
// anode RGB LED, comment (add "//" to the start of) the next 2, and uncomment
// (remove the "//" from) the following 2 lines.
const unsigned int COLOUR_ON = HIGH; // for common cathode
const unsigned int COLOUR_OFF = LOW;
//const unsigned int COLOUR_ON = LOW; // for common anode
//const unsigned int COLOUR_OFF = HIGH;
// LED pin array colour index values
const unsigned int RED = 0;
const unsigned int GREEN = 1;
const unsigned int BLUE = 2;
// For the initial blink logic, any pins will work. PWM capable pins were
// selected, so that the same wiring can be used when using analogWrite to
// using diming and colour mixing to 'make it better'.
const unsigned int rgbPin[] = { 11 , 10 , 9 }; // pin numbers for Red, Green, and Blue
// A *good* way to get the number of entries actually in an array
const unsigned int rgbPinCount = sizeof ( rgbPin ) / sizeof ( unsigned int );
// The time to show a single colour before changing to the next combination
const unsigned long COLOUR_TIME = 500; // milliseconds (0.5 seconds)
void setup () {
// initialize all of the RGB control pins as outputs.
for ( unsigned int colour = 0; colour < rgbPinCount; colour++ ) {
pinMode ( rgbPin [ colour ] , OUTPUT );
}
// An alternate way to do the same thing
// pinMode ( rgb [ RED ] , OUTPUT );
// pinMode ( rgb [ GREEN ] , OUTPUT );
// pinMode ( rgb [ BLUE ] , OUTPUT );
// turn all of the led pins off (black) to start
digitalWrite ( rgbPin [ RED ] , COLOUR_OFF );
digitalWrite ( rgbPin [ GREEN ] , COLOUR_OFF );
digitalWrite ( rgbPin [ BLUE ] , COLOUR_OFF );
}// ./void setup()
void loop () {
cycleManual ();
// cycleRGBset ();
}// ./void loop()
/**
* Cycle through the RGB colours available when turning each colour either full
* on or full off, in all possible combinations.
*
* This version explictly turns each colour on and off as needed.
*/
void cycleManual () {
// Set RGB LED to Red
digitalWrite( rgbPin [ RED ] , COLOUR_ON );
delay ( COLOUR_TIME );
// Set RGB LED to Green
digitalWrite ( rgbPin [ RED ] , COLOUR_OFF );
digitalWrite ( rgbPin [ GREEN ] , COLOUR_ON );
delay ( COLOUR_TIME );
// Set RGB LED to Blue
digitalWrite ( rgbPin [ GREEN ] , COLOUR_OFF );
digitalWrite ( rgbPin [ BLUE ] , COLOUR_ON );
delay ( COLOUR_TIME );
// Set RGB LED to Yellow ( RED + GREEN )
digitalWrite ( rgbPin [ BLUE ] , COLOUR_OFF );
digitalWrite ( rgbPin [ RED ] , COLOUR_ON );
digitalWrite ( rgbPin [ GREEN ] , COLOUR_ON );
delay ( COLOUR_TIME );
// Set RGB LED to Purple ( RED + BLUE )
digitalWrite ( rgbPin [ GREEN ] , COLOUR_OFF );
digitalWrite ( rgbPin [ BLUE ] , COLOUR_ON );
delay ( COLOUR_TIME );
// Set RGB LED to Cyan ( GREEN + BLUE )
digitalWrite ( rgbPin [ RED ] , COLOUR_OFF );
digitalWrite ( rgbPin [ GREEN ] , COLOUR_ON );
delay ( COLOUR_TIME );
// Set RGB LED to White ( RED + GREEN + BLUE )
digitalWrite ( rgbPin [ RED ] , COLOUR_ON );
delay ( COLOUR_TIME );
// Set RGB LED to Black ( all off )
digitalWrite ( rgbPin [ RED ] , COLOUR_OFF );
digitalWrite ( rgbPin [ GREEN ] , COLOUR_OFF );
digitalWrite ( rgbPin [ BLUE ] , COLOUR_OFF );
delay ( COLOUR_TIME );
}// ./void cycleManual ()
/**
* Use a function to set the RGB colour, and wait.
*/
void cycleRGBset () {
setLedColour ( COLOUR_ON , COLOUR_OFF , COLOUR_OFF ); // Red
setLedColour ( COLOUR_OFF , COLOUR_ON , COLOUR_OFF ); // Green
setLedColour ( COLOUR_OFF , COLOUR_OFF , COLOUR_ON ); // Blue
setLedColour ( COLOUR_ON , COLOUR_ON , COLOUR_OFF ); // Yellow
setLedColour ( COLOUR_ON , COLOUR_OFF , COLOUR_ON ); // Purple
setLedColour ( COLOUR_OFF , COLOUR_ON , COLOUR_ON ); // Cyan
setLedColour ( COLOUR_ON , COLOUR_ON , COLOUR_ON ); // White
setLedColour ( COLOUR_OFF , COLOUR_OFF , COLOUR_OFF ); // Black
}// ./void cycleRGBset ()
/**
* set the RGB LED colour and wait for awhile
*
* @param red the ON/OFF state for the red LED
* @param green the ON/OFF state for the green LED
* @param blue the ON/OFF state for the blue LED
*/
void setLedColour ( const unsigned int red , const unsigned int green , const unsigned int blue ) {
digitalWrite ( rgbPin [ RED ] , red );
digitalWrite ( rgbPin [ GREEN ] , green );
digitalWrite ( rgbPin [ BLUE ] , blue );
delay ( COLOUR_TIME );
}// ./void setLedColour ( const unsigned int red , const unsigned int green , const unsigned int blue )
</pre>
</section>
<section id="CIRC-notworking">
<h2>Not Working? <span>3 things to try</span></h2>
<div>
<h3>The LED does not light</h3>
<p>It is easy to insert the LED backwards. Check that it is inserted the
right way around. The longest lead is common.</p>
<p>The schematic and wiring diagram are for a common cathode RGB LED. If
using a common anode device, connect the common lead to +5V instead of
GND.</p>
</div>
<div>
<h3>The colour sequence is wrong</h3>
<p>The example code is for a common cathode RGB LED. If using a common
anode device, change the <code>COLOUR_ON</code> and
<code>COLOUR_OFF</code> initialization to:</p>
<pre>
COLOUR_ON = LOW;
COLOUR_OFF = HIGH;</pre>
</div>
<div>
<h3>Blue and Green light in reverse order</h3>
<p>Some RGB LEDs may have the pins that control the Blue and Green LEDs
reversed. Either reverse the wires for pins 9 and 10, or change the
rgbPin array from { 11 , 10 , 9 } to { 11 , 9 , 10 }. Either change
will fix the problem.</p>
</div>
</section>
<section id="CIRC-makingBetter">
<h2>Making it Better?</h2>
<h3>Use colour setting function</h3>
<p>In the <code>loop()</code> function there are 2 lines. The last starts
with <code>'//'</code>. This means the line is treated as a comment (not
run). To switch the sketch to use the colour setting function, change the
<code>void loop()</code> code to:</p>
<pre>
// cycleManual ();
cycleRGBset ();</pre>
<p>Upload the sketch, and notice that nothing has changed. Each function
does the same thing, but uses different approaches (hint: the second
one calls another function to set all of the RGB LED pins).
<h4>Discussion</h4>
<p>The <code>setLedColour()</code> function always sets all 3 leds, whether
they are already correct or not. However, using it also makes the code
easier to read, and simpler to change. To change the order of the
displayed colours, it is only necessary to change the order of the
<code>setLedColour()</code> calls in <code>cycleRGBset()</code>. There is
no need to consider how the previous colour left the pins set, or what the
following colour expects.</p>
<h3>Array of colour patterns</h3>
<p>Store the colour combinations in an array, then call
<code>setLedColour()</code> with the combinations, one after the other.
This becomes a flexible way to animate fairly long sequences of colours.</p>
<p>First, create an array to
hold the colour combinations. At the top of the file, just after
<code>rgbPinCount</code> is initialized, add:</p>
<pre>
unsigned const int pattern [][ 3 ] = {
{ COLOUR_ON , COLOUR_OFF , COLOUR_OFF },
{ COLOUR_OFF , COLOUR_OFF , COLOUR_OFF },
};
unsigned const int patternCount = sizeof ( pattern ) / ( sizeof ( pattern [ 0 ]));</pre>
<p>That is a 2 dimensional array. Think of it as a table, or an array where
each entry in the array is another array. In this case, the main array
contains patterns, and each pattern is an array of colour settings. The
array will be expanded a bit later, but this is enough to setup the rest
of the needed code.</p>
<p>Now, comment out both existing lines in <code>void loop()</code>, and
add:</p>
<pre>
rgbAnimate ();</pre>
<p>Finally, after the end of <code>void loop()</code>, create the
<code>rgbAnimate ()</code> function:</p>
<pre>
void rgbAnimate () {
for ( unsigned int ptn = 0; ptn < patternCount; ptn++ ) {
setLedColour (
pattern [ ptn ][ RED ] ,
pattern [ ptn ][ GREEN ] ,
pattern [ ptn ][ BLUE ]);
}
}</pre>
<p>That is enough to be able to load the new program. It won't do much yet.
It just blinks the red LED. To get something more to see, add some more
patterns to the array. Each pattern needs to be an array of the 3 colour
settings <code>{ RED_setting, GREEN_setting, BLUE_setting },</code></p>
<p>Don't forget the comma after each set of colours. To get the same pattern
as was seen initially, the patterns need to be the same as the values
passed to <code>setLedColour ()</code> in <code>cycleRGBset ()</code>.</p>
<p>IDEA: changing the value of <code>COLOUR_TIME</code> will change the
cycling speed for any of the versions.</p>
<p>IDEA: to get real colour <strong>blinking</strong>, make every other colour in
the pattern black (all colours OFF).</p>
<h3>External References</h3>
<ul>
<li><a href="https://cdn-learn.adafruit.com/downloads/pdf/adafruit-arduino-lesson-3-rgb-leds.pdf">Adafruit RGB lesson</a> https://cdn-learn.adafruit.com/downloads/pdf/adafruit-arduino-lesson-3-rgb-leds.pdf</li>
</ul>
</section>
</main>
<footer>.:Welcome to the Bottom:.</footer>
</body>
</html>