Skip to content

Commit 6fdf92c

Browse files
committed
Replaced all for-loop iterators types to byte, and rename LED to LED_PIN
LED_COUNT was misleading because there are n * (n-1) LEDs. LED_PIN_COUNT is a byte (makes sense because most Arduinos have less than 255 pins) - so the iterators can be bytes as well.
1 parent 21d9582 commit 6fdf92c

File tree

1 file changed

+68
-68
lines changed

1 file changed

+68
-68
lines changed

pages/guides/charlieplexing.mdx

Lines changed: 68 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,9 @@ for us to extend later:
289289
#define SCAN_DELAY 500
290290
291291
/* gac: The port pins are defined as an array. In this case it will be two port pins: 2, 3 */
292-
byte LEDS[] = { 2, 3 };
293-
/* gac: to derive the number of port pins. LED_COUNT will get a value of 2 */
294-
const byte LED_COUNT = sizeof(LEDS);
292+
byte LED_PINS[] = { 2, 3 };
293+
/* gac: to derive the number of port pins. LED_PIN_COUNT will get a value of 2 */
294+
const byte LED_PIN_COUNT = sizeof(LED_PINS);
295295
296296
void setup() {
297297
}
@@ -300,32 +300,32 @@ void loop() {
300300
/* gac:
301301
Variables `i` and `j` represent port pins of the Arduino.
302302
In first iteration, <br />
303-
`i` will have value of 0 --> LEDS[i] will be 2 (first element in the array) <br />
304-
`j` will have value of 0 --> LEDS[j] will be 2.
303+
`i` will have value of 0 --> LED_PINS[i] will be 2 (first element in the array) <br />
304+
`j` will have value of 0 --> LED_PINS[j] will be 2.
305305
306306
`if` condition will fail.
307307
308-
In the second iteration of inner for-loop, LEDS[j] will be 3.
308+
In the second iteration of inner for-loop, LED_PINS[j] will be 3.
309309
The `if` condition will pass.
310310
311311
Since there are only two port pins, go through the iterations manually to see the logic by yourself.
312312
you can leave a comment if there is need for more clarity
313313
*/
314-
for (int i = 0; i < LED_COUNT; i++) {
315-
for (int j = 0; j < LED_COUNT; j++) {
314+
for (byte i = 0; i < LED_PIN_COUNT; i++) {
315+
for (byte j = 0; j < LED_PIN_COUNT; j++) {
316316
if (i != j) { //The LED port pins are always connected to two different pins
317-
pinMode(LEDS[i], OUTPUT); //port 2
318-
pinMode(LEDS[j], OUTPUT); //port 3
317+
pinMode(LED_PINS[i], OUTPUT); //port 2
318+
pinMode(LED_PINS[j], OUTPUT); //port 3
319319
/* gac: writing port pins with the below sequence, will turn on LED(i,j)
320320
In first iteration, the LED(2,3) will be turned on.
321321
In the second iteration, LED(3,2) will be turned on.
322322
*/
323-
digitalWrite(LEDS[i], LOW);
324-
digitalWrite(LEDS[j], HIGH);
323+
digitalWrite(LED_PINS[i], LOW);
324+
digitalWrite(LED_PINS[j], HIGH);
325325
delay(SCAN_DELAY);
326326
/* gac: We will always take the pins to high impedance state before driving the next pins */
327-
pinMode(LEDS[i], INPUT);
328-
pinMode(LEDS[j], INPUT);
327+
pinMode(LED_PINS[i], INPUT);
328+
pinMode(LED_PINS[j], INPUT);
329329
}
330330
}
331331
}
@@ -361,24 +361,24 @@ And the 6 LEDs arranged as follows:
361361
362362
#define SCAN_DELAY 500
363363
364-
/* gac: Life is simple now - we can support as many pins as we want by adding values to the `LEDS` array */
365-
byte LEDS[] = { 2, 3, 4 };
366-
const byte LED_COUNT = sizeof(LEDS);
364+
/* gac: Life is simple now - we can support as many pins as we want by adding values to the `LED_PINS` array */
365+
byte LED_PINS[] = { 2, 3, 4 };
366+
const byte LED_PIN_COUNT = sizeof(LED_PINS);
367367
368368
void setup() {
369369
}
370370
371371
void loop() {
372-
for (int i = 0; i < LED_COUNT; i++) {
373-
for (int j = 0; j < LED_COUNT; j++) {
372+
for (byte i = 0; i < LED_PIN_COUNT; i++) {
373+
for (byte j = 0; j < LED_PIN_COUNT; j++) {
374374
if (i != j) {
375-
pinMode(LEDS[i], OUTPUT);
376-
pinMode(LEDS[j], OUTPUT);
377-
digitalWrite(LEDS[i], LOW);
378-
digitalWrite(LEDS[j], HIGH);
375+
pinMode(LED_PINS[i], OUTPUT);
376+
pinMode(LED_PINS[j], OUTPUT);
377+
digitalWrite(LED_PINS[i], LOW);
378+
digitalWrite(LED_PINS[j], HIGH);
379379
delay(SCAN_DELAY);
380-
pinMode(LEDS[i], INPUT);
381-
pinMode(LEDS[j], INPUT);
380+
pinMode(LED_PINS[i], INPUT);
381+
pinMode(LED_PINS[j], INPUT);
382382
}
383383
}
384384
}
@@ -398,7 +398,7 @@ void loop() {
398398
#### Code Summary
399399

400400
Since we made our code modular, we only had to change a single place: add the
401-
new pin number (4) to the `LEDS[]` array.
401+
new pin number (4) to the `LED_PINS[]` array.
402402

403403
### Case 3: Driving twelve LEDs with four Arduino Pins-Charlieplexing
404404

@@ -431,23 +431,23 @@ You have 12 LEDs in this case
431431
432432
#define SCAN_DELAY 500
433433
434-
byte LEDS[] = { 2, 3, 4, 5 };
435-
const byte LED_COUNT = sizeof(LEDS);
434+
byte LED_PINS[] = { 2, 3, 4, 5 };
435+
const byte LED_PIN_COUNT = sizeof(LED_PINS);
436436
437437
void setup() {
438438
}
439439
440440
void loop() {
441-
for (int i = 0; i < LED_COUNT; i++) {
442-
for (int j = 0; j < LED_COUNT; j++) {
441+
for (byte i = 0; i < LED_PIN_COUNT; i++) {
442+
for (byte j = 0; j < LED_PIN_COUNT; j++) {
443443
if (i != j) {
444-
pinMode(LEDS[i], OUTPUT);
445-
pinMode(LEDS[j], OUTPUT);
446-
digitalWrite(LEDS[i], LOW);
447-
digitalWrite(LEDS[j], HIGH);
444+
pinMode(LED_PINS[i], OUTPUT);
445+
pinMode(LED_PINS[j], OUTPUT);
446+
digitalWrite(LED_PINS[i], LOW);
447+
digitalWrite(LED_PINS[j], HIGH);
448448
delay(SCAN_DELAY);
449-
pinMode(LEDS[i], INPUT);
450-
pinMode(LEDS[j], INPUT);
449+
pinMode(LED_PINS[i], INPUT);
450+
pinMode(LED_PINS[j], INPUT);
451451
}
452452
}
453453
}
@@ -494,23 +494,23 @@ You have 20 LEDs in this case:
494494
495495
#define SCAN_DELAY 500
496496
497-
byte LEDS[] = { 2, 3, 4, 5, 6 };
498-
const byte LED_COUNT = sizeof(LEDS);
497+
byte LED_PINS[] = { 2, 3, 4, 5, 6 };
498+
const byte LED_PIN_COUNT = sizeof(LED_PINS);
499499
500500
void setup() {
501501
}
502502
503503
void loop() {
504-
for (int i = 0; i < LED_COUNT; i++) {
505-
for (int j = 0; j < LED_COUNT; j++) {
504+
for (byte i = 0; i < LED_PIN_COUNT; i++) {
505+
for (byte j = 0; j < LED_PIN_COUNT; j++) {
506506
if (i != j) {
507-
pinMode(LEDS[i], OUTPUT);
508-
pinMode(LEDS[j], OUTPUT);
509-
digitalWrite(LEDS[i], LOW);
510-
digitalWrite(LEDS[j], HIGH);
507+
pinMode(LED_PINS[i], OUTPUT);
508+
pinMode(LED_PINS[j], OUTPUT);
509+
digitalWrite(LED_PINS[i], LOW);
510+
digitalWrite(LED_PINS[j], HIGH);
511511
delay(SCAN_DELAY);
512-
pinMode(LEDS[i], INPUT);
513-
pinMode(LEDS[j], INPUT);
512+
pinMode(LED_PINS[i], INPUT);
513+
pinMode(LED_PINS[j], INPUT);
514514
}
515515
}
516516
}
@@ -549,7 +549,7 @@ And the LEDs:
549549
</figure>
550550

551551
The code was omitted for brevity. We basically add the number `7` at the end of the
552-
`LEDS` array.
552+
`LED_PINS` array.
553553

554554
### Case 6: Driving 42 LEDs with 7 Arduino Pins-Charlieplexing
555555

@@ -629,23 +629,23 @@ So what's the trick?
629629
LEDs are all lit at once. Even though we still light them just one at a time! */
630630
#define SCAN_DELAY 1
631631
632-
byte LEDS[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10 };
633-
const byte LED_COUNT = sizeof(LEDS);
632+
byte LED_PINS[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10 };
633+
const byte LED_PIN_COUNT = sizeof(LED_PINS);
634634
635635
void setup() {
636636
}
637637
638638
void loop() {
639-
for (int i = 0; i < LED_COUNT; i++) {
640-
for (int j = 0; j < LED_COUNT; j++) {
639+
for (byte i = 0; i < LED_PIN_COUNT; i++) {
640+
for (byte j = 0; j < LED_PIN_COUNT; j++) {
641641
if (i != j) {
642-
pinMode(LEDS[i], OUTPUT);
643-
pinMode(LEDS[j], OUTPUT);
644-
digitalWrite(LEDS[i], LOW);
645-
digitalWrite(LEDS[j], HIGH);
642+
pinMode(LED_PINS[i], OUTPUT);
643+
pinMode(LED_PINS[j], OUTPUT);
644+
digitalWrite(LED_PINS[i], LOW);
645+
digitalWrite(LED_PINS[j], HIGH);
646646
delay(SCAN_DELAY);
647-
pinMode(LEDS[i], INPUT);
648-
pinMode(LEDS[j], INPUT);
647+
pinMode(LED_PINS[i], INPUT);
648+
pinMode(LED_PINS[j], INPUT);
649649
}
650650
}
651651
}
@@ -665,17 +665,17 @@ of one LED at the time. This way, each LED will get power for ~1/9 of the time:
665665
666666
#define SCAN_DELAY 1
667667
668-
byte LEDS[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10 };
669-
const byte LED_COUNT = sizeof(LEDS);
668+
byte LED_PINS[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10 };
669+
const byte LED_PIN_COUNT = sizeof(LED_PINS);
670670
671671
void setup() {
672672
}
673673
674674
void loop() {
675-
for (int i = 0; i < LED_COUNT; i++) {
676-
pinMode(LEDS[i], OUTPUT);
677-
digitalWrite(LEDS[i], LOW);
678-
for (int j = 0; j < LED_COUNT; j++) {
675+
for (byte i = 0; i < LED_PIN_COUNT; i++) {
676+
pinMode(LED_PINS[i], OUTPUT);
677+
digitalWrite(LED_PINS[i], LOW);
678+
for (byte j = 0; j < LED_PIN_COUNT; j++) {
679679
if (i != j) {
680680
/* gac:start
681681
if you wanted just some specific LEDs to be on, we'd need to add another condition
@@ -685,15 +685,15 @@ void loop() {
685685
the pin is set both to `OUTPUT` and `LOW`. Otherwise, some current may flow from
686686
other `HIGH` pins to this pin, briefly lighting up LEDs we didn't want to.
687687
*/
688-
digitalWrite(LEDS[j], HIGH);
689-
pinMode(LEDS[j], OUTPUT);
688+
digitalWrite(LED_PINS[j], HIGH);
689+
pinMode(LED_PINS[j], OUTPUT);
690690
/* gac:end */
691691
}
692692
}
693693
delay(SCAN_DELAY);
694-
for (int j = 0; j < LED_COUNT; j++) {
695-
digitalWrite(LEDS[j], LOW);
696-
pinMode(LEDS[j], INPUT);
694+
for (byte j = 0; j < LED_PIN_COUNT; j++) {
695+
digitalWrite(LED_PINS[j], LOW);
696+
pinMode(LED_PINS[j], INPUT);
697697
}
698698
}
699699
}

0 commit comments

Comments
 (0)