Krolyxon
Commit 3120783

Branches

Tags

Refs →

orion-rf.piolibdepsesp32-s3-n16r8Adafruit NeoPixelexamplesStrandtestArduinoBLEStrandtestArduinoBLE.ino

231 lines · 8.8 KB

1/****************************************************************************
2 * This example is based on StrandtestBLE example and adapts it to use
3 * the new ArduinoBLE library.
4 *
5 * https://github.com/arduino-libraries/ArduinoBLE
6 *
7 * Supported boards:
8 * Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT,
9 Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board.
10 *
11 * You can use a generic BLE central app, like LightBlue (iOS and Android) or
12 * nRF Connect (Android), to interact with the services and characteristics
13 * created in this sketch.
14 *
15 * This example code is in the public domain.
16 *
17 */
18#include <Adafruit_NeoPixel.h>
19
20#define PIN 15 // Pin where NeoPixels are connected
21
22// Declare our NeoPixel strip object:
23Adafruit_NeoPixel strip(64, PIN, NEO_GRB + NEO_KHZ800);
24// Argument 1 = Number of pixels in NeoPixel strip
25// Argument 2 = Arduino pin number (most are valid)
26// Argument 3 = Pixel type flags, add together as needed:
27// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
28// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
29// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
30// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
31// NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
32
33// NEOPIXEL BEST PRACTICES for most reliable operation:
34// - Add 1000 uF CAPACITOR between NeoPixel strip's + and - connections.
35// - MINIMIZE WIRING LENGTH between microcontroller board and first pixel.
36// - NeoPixel strip's DATA-IN should pass through a 300-500 OHM RESISTOR.
37// - AVOID connecting NeoPixels on a LIVE CIRCUIT. If you must, ALWAYS
38// connect GROUND (-) first, then +, then data.
39// - When using a 3.3V microcontroller with a 5V-powered NeoPixel strip,
40// a LOGIC-LEVEL CONVERTER on the data line is STRONGLY RECOMMENDED.
41// (Skipping these may work OK on your workbench but can fail in the field)
42
43uint8_t rgb_values[3];
44
45#include <ArduinoBLE.h>
46
47BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service
48
49// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
50BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
51
52void setup()
53{
54 Serial.begin(115200);
55 Serial.println("Hello World!");
56
57 // custom services and characteristics can be added as well
58 // begin initialization
59 if (!BLE.begin())
60 {
61 Serial.println("starting BLE failed!");
62
63 while (1)
64 ;
65 }
66
67 Serial.print("Peripheral address: ");
68 Serial.println(BLE.address());
69
70 // set advertised local name and service UUID:
71 BLE.setLocalName("LED");
72 BLE.setAdvertisedService(ledService);
73
74 // add the characteristic to the service
75 ledService.addCharacteristic(switchCharacteristic);
76
77 // add service
78 BLE.addService(ledService);
79
80 // set the initial value for the characeristic:
81 switchCharacteristic.writeValue(0);
82
83 // start advertising
84 BLE.advertise();
85
86 strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
87 strip.show(); // Turn OFF all pixels ASAP
88
89 pinMode(PIN, OUTPUT);
90 digitalWrite(PIN, LOW);
91
92}
93
94void loop()
95{
96 BLEDevice central = BLE.central();
97
98 // if a central is connected to peripheral:
99 if (central)
100 {
101 Serial.print("Connected to central: ");
102 // print the central's MAC address:
103 Serial.println(central.address());
104
105 // while the central is still connected to peripheral:
106 while (central.connected())
107 {
108 // if the remote device wrote to the characteristic,
109 // use the value to control the LED:
110 if (switchCharacteristic.written())
111 {
112 switch (switchCharacteristic.value())
113 {
114 case 'a':
115 colorWipe(strip.Color(255, 0, 0), 20); // Red
116 break;
117 case 'b':
118 colorWipe(strip.Color(0, 255, 0), 20); // Green
119 break;
120 case 'c':
121 colorWipe(strip.Color(0, 0, 255), 20); // Blue
122 break;
123 case 'd':
124 theaterChase(strip.Color(255, 0, 0), 20); // Red
125 break;
126 case 'e':
127 theaterChase(strip.Color(0, 255, 0), 20); // Green
128 break;
129 case 'f':
130 theaterChase(strip.Color(255, 0, 255), 20); // Cyan
131 break;
132 case 'g':
133 rainbow(10);
134 break;
135 case 'h':
136 theaterChaseRainbow(20);
137 break;
138 }
139 }
140 }
141 }
142}
143
144// Fill strip pixels one after another with a color. Strip is NOT cleared
145// first; anything there will be covered pixel by pixel. Pass in color
146// (as a single 'packed' 32-bit value, which you can get by calling
147// strip.Color(red, green, blue) as shown in the loop() function above),
148// and a delay time (in milliseconds) between pixels.
149void colorWipe(uint32_t color, int wait)
150{
151 for (int i = 0; i < strip.numPixels(); i++)
152 { // For each pixel in strip...
153 strip.setPixelColor(i, color); // Set pixel's color (in RAM)
154 strip.show(); // Update strip to match
155 delay(wait); // Pause for a moment
156 }
157}
158
159// Theater-marquee-style chasing lights. Pass in a color (32-bit value,
160// a la strip.Color(r,g,b) as mentioned above), and a delay time (in ms)
161// between frames.
162void theaterChase(uint32_t color, int wait)
163{
164 for (int a = 0; a < 10; a++)
165 { // Repeat 10 times...
166 for (int b = 0; b < 3; b++)
167 { // 'b' counts from 0 to 2...
168 strip.clear(); // Set all pixels in RAM to 0 (off)
169 // 'c' counts up from 'b' to end of strip in steps of 3...
170 for (int c = b; c < strip.numPixels(); c += 3)
171 {
172 strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
173 }
174 strip.show(); // Update strip with new contents
175 delay(wait); // Pause for a moment
176 }
177 }
178}
179
180// Rainbow cycle along whole strip. Pass delay time (in ms) between frames.
181void rainbow(int wait)
182{
183 // Hue of first pixel runs 5 complete loops through the color wheel.
184 // Color wheel has a range of 65536 but it's OK if we roll over, so
185 // just count from 0 to 5*65536. Adding 256 to firstPixelHue each time
186 // means we'll make 5*65536/256 = 1280 passes through this outer loop:
187 for (long firstPixelHue = 0; firstPixelHue < 5 * 65536; firstPixelHue += 256)
188 {
189 for (int i = 0; i < strip.numPixels(); i++)
190 { // For each pixel in strip...
191 // Offset pixel hue by an amount to make one full revolution of the
192 // color wheel (range of 65536) along the length of the strip
193 // (strip.numPixels() steps):
194 int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
195 // strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or
196 // optionally add saturation and value (brightness) (each 0 to 255).
197 // Here we're using just the single-argument hue variant. The result
198 // is passed through strip.gamma32() to provide 'truer' colors
199 // before assigning to each pixel:
200 strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
201 }
202 strip.show(); // Update strip with new contents
203 delay(wait); // Pause for a moment
204 }
205}
206
207// Rainbow-enhanced theater marquee. Pass delay time (in ms) between frames.
208void theaterChaseRainbow(int wait)
209{
210 int firstPixelHue = 0; // First pixel starts at red (hue 0)
211 for (int a = 0; a < 30; a++)
212 { // Repeat 30 times...
213 for (int b = 0; b < 3; b++)
214 { // 'b' counts from 0 to 2...
215 strip.clear(); // Set all pixels in RAM to 0 (off)
216 // 'c' counts up from 'b' to end of strip in increments of 3...
217 for (int c = b; c < strip.numPixels(); c += 3)
218 {
219 // hue of pixel 'c' is offset by an amount to make one full
220 // revolution of the color wheel (range 65536) along the length
221 // of the strip (strip.numPixels() steps):
222 int hue = firstPixelHue + c * 65536L / strip.numPixels();
223 uint32_t color = strip.gamma32(strip.ColorHSV(hue)); // hue -> RGB
224 strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
225 }
226 strip.show(); // Update strip with new contents
227 delay(wait); // Pause for a moment
228 firstPixelHue += 65536 / 90; // One cycle of color wheel over 90 frames
229 }
230 }
231}