| 1 | /**************************************************************************** |
| 2 | * This example is based on StrandtestArduinoBLE example to make use of |
| 3 | * callbacks features of the 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: |
| 23 | Adafruit_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 | |
| 43 | uint8_t rgb_values[3]; |
| 44 | |
| 45 | #include <ArduinoBLE.h> |
| 46 | |
| 47 | BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service |
| 48 | |
| 49 | // BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central |
| 50 | BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite); |
| 51 | |
| 52 | void 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("LEDCallback"); |
| 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 | // assign event handlers for connected, disconnected to peripheral |
| 80 | BLE.setEventHandler(BLEConnected, blePeripheralConnectHandler); |
| 81 | BLE.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler); |
| 82 | |
| 83 | // assign event handlers for characteristic |
| 84 | switchCharacteristic.setEventHandler(BLEWritten, switchCharacteristicWritten); |
| 85 | // set the initial value for the characeristic: |
| 86 | switchCharacteristic.writeValue(0); |
| 87 | |
| 88 | // start advertising |
| 89 | BLE.advertise(); |
| 90 | |
| 91 | strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED) |
| 92 | strip.show(); // Turn OFF all pixels ASAP |
| 93 | |
| 94 | pinMode(PIN, OUTPUT); |
| 95 | digitalWrite(PIN, LOW); |
| 96 | } |
| 97 | |
| 98 | void loop() |
| 99 | { |
| 100 | // poll for BLE events |
| 101 | BLE.poll(); |
| 102 | } |
| 103 | |
| 104 | void blePeripheralConnectHandler(BLEDevice central) |
| 105 | { |
| 106 | // central connected event handler |
| 107 | Serial.print("Connected event, central: "); |
| 108 | Serial.println(central.address()); |
| 109 | } |
| 110 | |
| 111 | void blePeripheralDisconnectHandler(BLEDevice central) |
| 112 | { |
| 113 | // central disconnected event handler |
| 114 | Serial.print("Disconnected event, central: "); |
| 115 | Serial.println(central.address()); |
| 116 | } |
| 117 | |
| 118 | void switchCharacteristicWritten(BLEDevice central, BLECharacteristic characteristic) |
| 119 | { |
| 120 | // central wrote new value to characteristic, update LED |
| 121 | Serial.print("Characteristic event, written: "); |
| 122 | |
| 123 | switch (switchCharacteristic.value()) |
| 124 | { |
| 125 | case 'a': |
| 126 | colorWipe(strip.Color(255, 0, 0), 20); // Red |
| 127 | break; |
| 128 | case 'b': |
| 129 | colorWipe(strip.Color(0, 255, 0), 20); // Green |
| 130 | break; |
| 131 | case 'c': |
| 132 | colorWipe(strip.Color(0, 0, 255), 20); // Blue |
| 133 | break; |
| 134 | case 'd': |
| 135 | theaterChase(strip.Color(255, 0, 0), 20); // Red |
| 136 | break; |
| 137 | case 'e': |
| 138 | theaterChase(strip.Color(0, 255, 0), 20); // Green |
| 139 | break; |
| 140 | case 'f': |
| 141 | theaterChase(strip.Color(255, 0, 255), 20); // Cyan |
| 142 | break; |
| 143 | case 'g': |
| 144 | rainbow(10); |
| 145 | break; |
| 146 | case 'h': |
| 147 | theaterChaseRainbow(20); |
| 148 | break; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | // Fill strip pixels one after another with a color. Strip is NOT cleared |
| 153 | // first; anything there will be covered pixel by pixel. Pass in color |
| 154 | // (as a single 'packed' 32-bit value, which you can get by calling |
| 155 | // strip.Color(red, green, blue) as shown in the loop() function above), |
| 156 | // and a delay time (in milliseconds) between pixels. |
| 157 | void colorWipe(uint32_t color, int wait) |
| 158 | { |
| 159 | for (int i = 0; i < strip.numPixels(); i++) |
| 160 | { // For each pixel in strip... |
| 161 | strip.setPixelColor(i, color); // Set pixel's color (in RAM) |
| 162 | strip.show(); // Update strip to match |
| 163 | delay(wait); // Pause for a moment |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | // Theater-marquee-style chasing lights. Pass in a color (32-bit value, |
| 168 | // a la strip.Color(r,g,b) as mentioned above), and a delay time (in ms) |
| 169 | // between frames. |
| 170 | void theaterChase(uint32_t color, int wait) |
| 171 | { |
| 172 | for (int a = 0; a < 10; a++) |
| 173 | { // Repeat 10 times... |
| 174 | for (int b = 0; b < 3; b++) |
| 175 | { // 'b' counts from 0 to 2... |
| 176 | strip.clear(); // Set all pixels in RAM to 0 (off) |
| 177 | // 'c' counts up from 'b' to end of strip in steps of 3... |
| 178 | for (int c = b; c < strip.numPixels(); c += 3) |
| 179 | { |
| 180 | strip.setPixelColor(c, color); // Set pixel 'c' to value 'color' |
| 181 | } |
| 182 | strip.show(); // Update strip with new contents |
| 183 | delay(wait); // Pause for a moment |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | // Rainbow cycle along whole strip. Pass delay time (in ms) between frames. |
| 189 | void rainbow(int wait) |
| 190 | { |
| 191 | // Hue of first pixel runs 5 complete loops through the color wheel. |
| 192 | // Color wheel has a range of 65536 but it's OK if we roll over, so |
| 193 | // just count from 0 to 5*65536. Adding 256 to firstPixelHue each time |
| 194 | // means we'll make 5*65536/256 = 1280 passes through this outer loop: |
| 195 | for (long firstPixelHue = 0; firstPixelHue < 5 * 65536; firstPixelHue += 256) |
| 196 | { |
| 197 | for (int i = 0; i < strip.numPixels(); i++) |
| 198 | { // For each pixel in strip... |
| 199 | // Offset pixel hue by an amount to make one full revolution of the |
| 200 | // color wheel (range of 65536) along the length of the strip |
| 201 | // (strip.numPixels() steps): |
| 202 | int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels()); |
| 203 | // strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or |
| 204 | // optionally add saturation and value (brightness) (each 0 to 255). |
| 205 | // Here we're using just the single-argument hue variant. The result |
| 206 | // is passed through strip.gamma32() to provide 'truer' colors |
| 207 | // before assigning to each pixel: |
| 208 | strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue))); |
| 209 | } |
| 210 | strip.show(); // Update strip with new contents |
| 211 | delay(wait); // Pause for a moment |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | // Rainbow-enhanced theater marquee. Pass delay time (in ms) between frames. |
| 216 | void theaterChaseRainbow(int wait) |
| 217 | { |
| 218 | int firstPixelHue = 0; // First pixel starts at red (hue 0) |
| 219 | for (int a = 0; a < 30; a++) |
| 220 | { // Repeat 30 times... |
| 221 | for (int b = 0; b < 3; b++) |
| 222 | { // 'b' counts from 0 to 2... |
| 223 | strip.clear(); // Set all pixels in RAM to 0 (off) |
| 224 | // 'c' counts up from 'b' to end of strip in increments of 3... |
| 225 | for (int c = b; c < strip.numPixels(); c += 3) |
| 226 | { |
| 227 | // hue of pixel 'c' is offset by an amount to make one full |
| 228 | // revolution of the color wheel (range 65536) along the length |
| 229 | // of the strip (strip.numPixels() steps): |
| 230 | int hue = firstPixelHue + c * 65536L / strip.numPixels(); |
| 231 | uint32_t color = strip.gamma32(strip.ColorHSV(hue)); // hue -> RGB |
| 232 | strip.setPixelColor(c, color); // Set pixel 'c' to value 'color' |
| 233 | } |
| 234 | strip.show(); // Update strip with new contents |
| 235 | delay(wait); // Pause for a moment |
| 236 | firstPixelHue += 65536 / 90; // One cycle of color wheel over 90 frames |
| 237 | } |
| 238 | } |
| 239 | } |