/
apa102.ino
108 lines (94 loc) · 3.45 KB
/
apa102.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/// Fast Adressable Bitbang LED Library
/// Copyright (c)2015, 2017 Dan Truong
///
/// This is the simplest exmple to use the library.
///
/// This example is for an Arduino Uno board with a LED strip connected to
/// port D5-D6. Targetting any other board requires you to change something.
/// The program sends an array of pixels to display on the strip.
/// "strip" represents the hardware: LED types and port configuration,
/// "pixels" represents the data sent to the LEDs: a series of colors.
///
/// Wiring:
///
/// The LED strip DI (data input) line should be on port D6 (Digital pin 6 on
/// Arduino Uno). The LED strip CI (clock input) should be on port D5 (Digital
/// pin 5). If you need to change the port, change all declarations below
/// from, for example from "apa102<D,6,D,4> strip" to "apa102<B,4,B,3> strip"
/// if you wanted to use port B3-B4.
/// The LED power (GND) and (+5V) should be connected on the Arduino Uno's GND
/// and +5V.
///
/// Visual results:
///
/// If the hardware you use matches this program you will see the LEDs blink
/// repeatedly red, green, blue, white, in that order.
///
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
#include <FAB_LED.h>
// Declare the LED protocol and the port
apa102<D,6,D,5> strip;
// How many pixels to control
const uint8_t numPixels = 2;
// How bright the LEDs will be (max 255)
const uint8_t maxBrightness = 16;
// The pixel array to display
hbgr pixels[numPixels] = {};
////////////////////////////////////////////////////////////////////////////////
// Sets the array to specified color
////////////////////////////////////////////////////////////////////////////////
void updateColors(char r, char g, char b)
{
for(int i = 0; i < numPixels; i++)
{
pixels[i].h = 0xFF;
pixels[i].r = r;
pixels[i].g = g;
pixels[i].b = b;
}
}
////////////////////////////////////////////////////////////////////////////////
// This method is automatically called once when the board boots.
////////////////////////////////////////////////////////////////////////////////
void setup()
{
// Turn off the LEDs
strip.clear(2 * numPixels);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief This method is automatically called repeatedly after setup() has run.
/// It is the main loop.
////////////////////////////////////////////////////////////////////////////////
void loop()
{
// Write the pixel array red
updateColors(maxBrightness, 0 , 0);
// Display the pixels on the LED strip
strip.sendPixels(numPixels, pixels);
// Wait 0.1 seconds
delay(100);
// Write the pixel array green
updateColors(0, maxBrightness, 0);
// Display the pixels on the LED strip
strip.sendPixels(numPixels, pixels);
// Wait 0.1 seconds
delay(100);
// Write the pixel array blue
updateColors(0, 0, maxBrightness);
// Display the pixels on the LED strip
strip.sendPixels(numPixels, pixels);
// Wait 0.1 seconds
delay(100);
// Write the pixel array white
updateColors( maxBrightness, maxBrightness, maxBrightness);
// Display the pixels on the LED strip
strip.sendPixels(numPixels, pixels);
// Wait 0.1 seconds
delay(100);
// Turn off the LEDs
strip.clear(numPixels);
delay(600);
}