pxt-calliope/libs/neopixel/README.md

46 lines
1.3 KiB
Markdown
Raw Normal View History

2016-04-06 03:21:24 +02:00
# NeoPixel driver
This library provides a driver for various Neo Pixel LED strips,
see https://www.adafruit.com/category/168
NeoPixels consist of a number of RGB LEDs, every one of them controlled
separately.
## Basic usage
2016-04-13 07:09:03 +02:00
```blocks
2016-04-06 03:21:24 +02:00
// Create a NeoPixel driver - specify the number of LEDs:
2016-04-13 07:09:03 +02:00
let strip = neopixel.create(DigitalPin.P0, 24)
2016-04-06 03:21:24 +02:00
// set pixel colors
2016-04-13 07:09:03 +02:00
strip.setPixelColor(0, 255, 255, 255) // white
strip.setPixelColor(1, 255, 0, 0) // red
strip.setPixelColor(2, 0, 255, 0) // green
strip.setPixelColor(3, 0, 0, 255) // blue
2016-04-06 03:21:24 +02:00
// send the data to the strip
2016-04-13 07:09:03 +02:00
strip.show()
2016-04-06 03:21:24 +02:00
```
Use `strip.setBrigthness()` to lower the brightness (it's maxed out by default).
Use `strip.shift()` or `strip.rotate()` to shift the lights around.
## Example: Using accelerometer to control colors
This little program will let the position of the microbit control the color of the first LED.
This first LED will then get shifted further away every 100ms.
2016-04-13 07:09:03 +02:00
```blocks
let strip = neopixel.create(DigitalPin.P0, 24)
2016-04-06 03:21:24 +02:00
while (true) {
let x = input.acceleration(Dimension.X) / 2
let y = input.acceleration(Dimension.Y) / 2
let z = input.acceleration(Dimension.Z) / 2
2016-04-13 07:09:03 +02:00
strip.setPixelColor(0, x, y, -z);
2016-04-06 03:21:24 +02:00
strip.shift(1);
2016-04-13 07:09:03 +02:00
strip.show();
2016-04-06 03:21:24 +02:00
basic.pause(100);
}
```