You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: advancedio/addressable-leds.md
+16-16Lines changed: 16 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -203,7 +203,7 @@ The Adafruit NeoPixel library can be installed directly from the Arduino Library
203
203
204
204
The NeoPixel library API will feel familiar if you've completed the [OLED lesson](oled.md)—it follows the same **buffer → display** pattern:
205
205
206
-
{% highlight C++ %}
206
+
```cpp
207
207
#include<Adafruit_NeoPixel.h>
208
208
209
209
constintLED_PIN = 2; // Any digital pin works — no PWM required!
@@ -219,7 +219,7 @@ void setup() {
219
219
strip.setBrightness(50); // Set brightness (0-255). 50 is ~20% bright
220
220
strip.show(); // Initialize all pixels to 'off'
221
221
}
222
-
{% endhighlight C++ %}
222
+
```
223
223
224
224
{: .note }
225
225
> Notice the same **buffer → show** pattern from the [OLED lesson](oled.md): `setPixelColor()` writes to a buffer in RAM, and `show()` pushes the data to the LEDs. If you forget to call `show()`, nothing will change on the LEDs—just like forgetting `_display.display()` on the OLED!
@@ -247,19 +247,19 @@ Here are the most commonly used functions from the [Adafruit NeoPixel library](h
247
247
248
248
Each pixel's color is specified using RGB values from an 8-bit value—0-255 per channel—just like the [RGB LED lesson](../arduino/rgb-led.md). Some examples:
249
249
250
-
{% highlight C++ %}
250
+
```cpp
251
251
// Named colors using strip.Color(R, G, B)
252
252
uint32_t red = strip.Color(255, 0, 0);
253
253
uint32_t green = strip.Color(0, 255, 0);
254
254
uint32_t blue = strip.Color(0, 0, 255);
255
255
uint32_t white = strip.Color(255, 255, 255);
256
256
uint32_t purple = strip.Color(128, 0, 255);
257
257
uint32_t off = strip.Color(0, 0, 0);
258
-
{% endhighlight C++ %}
258
+
```
259
259
260
260
For animations that cycle through colors, the **HSV** (hue, saturation, value) color space is much more useful than RGB. Remember the [HSL crossfading lesson](../arduino/rgb-led-fade.md)? The same principle applies here. The `ColorHSV()` function lets you smoothly sweep through the entire rainbow by varying just the hue value:
> **What is `gamma32()`?** Human eyes perceive brightness non-linearly—the difference between 0 and 50 looks much bigger than the difference between 200 and 250. The `gamma32()` function applies a correction curve so that color transitions look smooth and natural to our eyes. It's optional but makes a noticeable difference in gradients and fades.
@@ -381,7 +381,7 @@ Now that we understand how addressable LEDs work and have our stick wired up, le
381
381
382
382
Let's start by simply setting each LED to a different color. This confirms that your wiring is correct and that the library is communicating with all 8 LEDs. This is our equivalent of the [shape drawing activity](oled.md#activity-draw-shapes-and-text) from the OLED lesson—the simplest possible test.
383
383
384
-
{% highlight C++ %}
384
+
```cpp
385
385
#include<Adafruit_NeoPixel.h>
386
386
387
387
constintLED_PIN = 2;
@@ -409,7 +409,7 @@ void setup() {
409
409
void loop() {
410
410
// Nothing to do — the colors persist until changed
411
411
}
412
-
{% endhighlight C++ %}
412
+
```
413
413
414
414
If your colors look wrong (*e.g.,* you asked for red but got green), try changing `NEO_GRB` to `NEO_RGB` in the strip constructor. This is the most common issue students encounter!
415
415
@@ -423,7 +423,7 @@ Try playing with the colors by changing the RGB values above. Our code for this
423
423
424
424
Now let's create a classic rainbow animation that cycles smoothly across all 8 LEDs. This introduces the concept of **animation on LED strips**: update pixel colors, call `show()`, wait a bit, repeat. It's the same pattern we used for the [bouncing ball](oled.md#activity-draw-a-bouncing-ball) on the OLED.
425
425
426
-
{% highlight C++ %}
426
+
```cpp
427
427
#include <Adafruit_NeoPixel.h>
428
428
429
429
const int LED_PIN = 2;
@@ -456,7 +456,7 @@ void loop() {
456
456
457
457
delay(20); // ~50 fps
458
458
}
459
-
{% endhighlight C++ %}
459
+
```
460
460
461
461
Try changing the `HUE_STEP` constant to `64` (slower rainbow) or `512` (faster rainbow). What happens if you change `setBrightness()` to 255? (Shield your eyes!).
462
462
@@ -490,7 +490,7 @@ Use the same LED wiring as before, and add a 10KΩ potentiometer with its wiper
490
490
491
491
#### The code
492
492
493
-
{% highlight C++ %}
493
+
```cpp
494
494
#include<Adafruit_NeoPixel.h>
495
495
496
496
constintLED_PIN = 2;
@@ -527,7 +527,7 @@ void loop() {
527
527
528
528
delay(20);
529
529
}
530
-
{% endhighlight C++ %}
530
+
```
531
531
532
532
As you turn the potentiometer, you should see all 8 LEDs smoothly cycle through the rainbow together.
533
533
@@ -549,7 +549,7 @@ Now let's add a **second potentiometer** on `A1` to independently control bright
549
549
**Video.** A circuit diagram for the potentiometer-controlled hue and brightness example. You can view and play with this example on [Tinkercad](https://www.tinkercad.com/things/53EaKIvUCsX-neopixel-strip-8-pot-controlled-hue-and-brightness).
550
550
{: .fs-1 }
551
551
552
-
{% highlight C++ %}
552
+
```cpp
553
553
#include <Adafruit_NeoPixel.h>
554
554
555
555
const int LED_PIN = 2;
@@ -590,7 +590,7 @@ void loop() {
590
590
591
591
delay(20);
592
592
}
593
-
{% endhighlight C++ %}
593
+
```
594
594
595
595
Try turning each knob independently—you can dial in any color at any brightness level. Notice how the `ColorHSV()` function's three parameters (hue, saturation, value) map perfectly to physical controls. What would you use a *third* potentiometer for? (Hint: saturation controls how vivid *vs.* pastel the color looks!)
596
596
@@ -608,7 +608,7 @@ For our final activity, let's build a **level meter** (or VU meter)—a bar-grap
608
608
609
609
We'll color the LEDs from green (low) through yellow (mid) to red (high), like a classic audio level meter.
610
610
611
-
{% highlight C++ %}
611
+
```cpp
612
612
#include <Adafruit_NeoPixel.h>
613
613
614
614
const int LED_PIN = 2;
@@ -653,7 +653,7 @@ void loop() {
653
653
654
654
delay(30);
655
655
}
656
-
{% endhighlight C++ %}
656
+
```
657
657
658
658
Turn the potentiometer and watch the LEDs fill up like a progress bar! This is a simple but satisfying example of mapping data to a physical display. Try replacing the potentiometer with a [force-sensitive resistor](../arduino/force-sensitive-resistors.md) or a [photoresistor](../sensors/photoresistors.md) for a more interactive experience.
0 commit comments