1 | /* This example shows how to display a moving rainbow pattern on
|
2 | * an APA102-based LED strip. */
|
3 |
|
4 | /* By default, the APA102 library uses pinMode and digitalWrite
|
5 | * to write to the LEDs, which works on all Arduino-compatible
|
6 | * boards but might be slow. If you have a board supported by
|
7 | * the FastGPIO library and want faster LED updates, then install
|
8 | * the FastGPIO library and uncomment the next two lines: */
|
9 | // #include <FastGPIO.h>
|
10 | // #define APA102_USE_FAST_GPIO
|
11 |
|
12 | #include <APA102.h>
|
13 |
|
14 | // Define which pins to use.
|
15 | const uint8_t dataPin = 11;
|
16 | const uint8_t clockPin = 12;
|
17 |
|
18 | // Create an object for writing to the LED strip.
|
19 | APA102<dataPin, clockPin> ledStrip;
|
20 |
|
21 | // Set the number of LEDs to control.
|
22 | const uint16_t ledCount = 5;
|
23 |
|
24 | // Create a buffer for holding the colors (3 bytes per color).
|
25 | rgb_color colors[ledCount];
|
26 |
|
27 | // Set the brightness to use (the maximum is 31).
|
28 | const uint8_t brightness = 1;
|
29 |
|
30 | void setup()
|
31 | {
|
32 | }
|
33 |
|
34 | /* Converts a color from HSV to RGB.
|
35 | * h is hue, as a number between 0 and 360.
|
36 | * s is the saturation, as a number between 0 and 255.
|
37 | * v is the value, as a number between 0 and 255. */
|
38 | rgb_color hsvToRgb(uint16_t h, uint8_t s, uint8_t v)
|
39 | {
|
40 | uint8_t f = (h % 60) * 255 / 60;
|
41 | uint8_t p = (255 - s) * (uint16_t)v / 255;
|
42 | uint8_t q = (255 - f * (uint16_t)s / 255) * (uint16_t)v / 255;
|
43 | uint8_t t = (255 - (255 - f) * (uint16_t)s / 255) * (uint16_t)v / 255;
|
44 | uint8_t r = 0, g = 0, b = 0;
|
45 | switch((h / 60) % 6){
|
46 | case 0: r = v; g = t; b = p; break;
|
47 | case 1: r = q; g = v; b = p; break;
|
48 | case 2: r = p; g = v; b = t; break;
|
49 | case 3: r = p; g = q; b = v; break;
|
50 | case 4: r = t; g = p; b = v; break;
|
51 | case 5: r = v; g = p; b = q; break;
|
52 | }
|
53 | return rgb_color(r, g, b);
|
54 | }
|
55 |
|
56 | void loop()
|
57 | {
|
58 | uint8_t time = millis() >> 4;
|
59 |
|
60 | for(uint16_t i = 0; i < ledCount; i++)
|
61 | {
|
62 | uint8_t p = time - i * 8;
|
63 | colors[i] = hsvToRgb((uint32_t)p * 359 / 256, 255, 255);
|
64 | }
|
65 |
|
66 | ledStrip.write(colors, ledCount, brightness);
|
67 |
|
68 | delay(10);
|
69 | }
|