StrandtestArduinoBLECallback.ino 8.9 KB

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