Ligne 96 : |
Ligne 96 : |
| <nowiki>strip.setPixelColor(n, red, green, blue);</nowiki> | | <nowiki>strip.setPixelColor(n, red, green, blue);</nowiki> |
| | | |
| + | The first argument — n in this example — is the pixel number along the strip, starting from 0 closest to the Arduino. If you have a strip of 30 pixels, they’re numbered 0 through 29. It’s a computer thing. You’ll see various places in the code using a for loop, passing the loop counter variable as the pixel number to this function, to set the values of multiple pixels. |
| + | |
| + | The next three arguments are the pixel color, expressed as red, green and blue brightness levels, where 0 is dimmest (off) and 255 is maximum brightness. |
| + | |
| + | To set the 12th pixel (#11, counting from 0) to magenta (red + blue), you could write: |
| + | |
| + | <nowiki>strip.setPixelColor(11, 255, 0, 255);</nowiki> |
| + | |
| + | An alternate syntax has just two arguments: |
| + | |
| + | <nowiki>strip.setPixelColor(n, color);</nowiki> |
| + | |
| + | Here, color is a 32-bit type that merges the red, green and blue values into a single number. This is sometimes easier or faster for some (but not all) programs to work with; you’ll see the strandtest code uses both syntaxes in different places. |
| + | |
| + | You can also convert separate red, green and blue values into a single 32-bit type for later use: |
| + | |
| + | <nowiki>uint32_t magenta = strip.Color(255, 0, 255);</nowiki> |
| + | |
| + | Then later you can just pass “magenta” as an argument to setPixelColor rather than the separate red, green and blue numbers every time. |
| + | |
| + | setPixelColor() does not have an immediate effect on the LEDs. To “push” the color data to the strip, call show(): |
| + | |
| + | <nowiki>strip.show();</nowiki> |
| + | |
| + | This updates the whole strip at once, and despite the extra step is actually a good thing. If every call to setPixelColor() had an immediate effect, animation would appear jumpy rather than buttery smooth. |
| + | You can query the color of a previously-set pixel using getPixelColor(): |
| + | |
| + | <nowiki>uint32_t color = strip.getPixelColor(11);</nowiki> |
| + | |
| + | This returns a 32-bit merged color value. |
| + | |
| + | The number of pixels in a previously-declared strip can be queried using numPixels(): |
| + | |
| + | <nowiki>uint16_t n = strip.numPixels();</nowiki> |
| + | |
| + | The overall brightness of all the LEDs can be adjusted using setBrightness(). This takes a single argument, a number in the range 0 (off) to 255 (max brightness). For example, to set a strip to 1/4 brightness: |
| + | |
| + | <nowiki>strip.setBrightness(64);</nowiki> |
| + | |
| + | Just like setPixel(), '''this does not have an immediate effect'''. You need to follow this with a call to show(). |
| + | |
| + | You can’t move from a lower brightness to a higher setting without some loss in fidelity. Certain animation effects are better served by leaving the brightness at max and calling setPixel() repeatedly to fill the strip. |
| + | |
| + | === I’m calling setPixel() but nothing’s happening! === |
| + | There are two main culprits for this: |
| + | # forgetting to call strip.begin() in setup(). |
| + | # forgetting to call strip.show() after setting pixel colors. |
| + | |
| + | Another (less common) possibility is running out of RAM — see the last section below. If the program ''sort of'' works but has unpredictable results, consider that. |
| + | |
| + | === Can I have multiple NeoPixel objects on different pins? === |
| + | |
| + | Certainly! Each requires its own declaration with a unique name: |
| + | |
| + | <nowiki>Adafruit_NeoPixel strip_a = Adafruit_NeoPixel(16, 5); |
| + | Adafruit_NeoPixel strip_b = Adafruit_NeoPixel(16, 6);</nowiki> |
| + | |
| + | The above declares two distinct NeoPixel objects, one each on pins 5 and 6, each containing 16 pixels and using the implied default type (NEO_KHZ800 + NEO_GRB). |
| | | |
| {{NeoPixel-UserGuide-TRAILER}} | | {{NeoPixel-UserGuide-TRAILER}} |