StrandtestArduinoBLE.ino 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. #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("LED");
  60. BLE.setAdvertisedService(ledService);
  61. // add the characteristic to the service
  62. ledService.addCharacteristic(switchCharacteristic);
  63. // add service
  64. BLE.addService(ledService);
  65. // set the initial value for the characeristic:
  66. switchCharacteristic.writeValue(0);
  67. // start advertising
  68. BLE.advertise();
  69. strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
  70. strip.show(); // Turn OFF all pixels ASAP
  71. pinMode(PIN, OUTPUT);
  72. digitalWrite(PIN, LOW);
  73. }
  74. void loop()
  75. {
  76. BLEDevice central = BLE.central();
  77. // if a central is connected to peripheral:
  78. if (central)
  79. {
  80. Serial.print("Connected to central: ");
  81. // print the central's MAC address:
  82. Serial.println(central.address());
  83. // while the central is still connected to peripheral:
  84. while (central.connected())
  85. {
  86. // if the remote device wrote to the characteristic,
  87. // use the value to control the LED:
  88. if (switchCharacteristic.written())
  89. {
  90. switch (switchCharacteristic.value())
  91. {
  92. case 'a':
  93. colorWipe(strip.Color(255, 0, 0), 20); // Red
  94. break;
  95. case 'b':
  96. colorWipe(strip.Color(0, 255, 0), 20); // Green
  97. break;
  98. case 'c':
  99. colorWipe(strip.Color(0, 0, 255), 20); // Blue
  100. break;
  101. case 'd':
  102. theaterChase(strip.Color(255, 0, 0), 20); // Red
  103. break;
  104. case 'e':
  105. theaterChase(strip.Color(0, 255, 0), 20); // Green
  106. break;
  107. case 'f':
  108. theaterChase(strip.Color(255, 0, 255), 20); // Cyan
  109. break;
  110. case 'g':
  111. rainbow(10);
  112. break;
  113. case 'h':
  114. theaterChaseRainbow(20);
  115. break;
  116. }
  117. }
  118. }
  119. }
  120. }
  121. // Fill strip pixels one after another with a color. Strip is NOT cleared
  122. // first; anything there will be covered pixel by pixel. Pass in color
  123. // (as a single 'packed' 32-bit value, which you can get by calling
  124. // strip.Color(red, green, blue) as shown in the loop() function above),
  125. // and a delay time (in milliseconds) between pixels.
  126. void colorWipe(uint32_t color, int wait)
  127. {
  128. for (int i = 0; i < strip.numPixels(); i++)
  129. { // For each pixel in strip...
  130. strip.setPixelColor(i, color); // Set pixel's color (in RAM)
  131. strip.show(); // Update strip to match
  132. delay(wait); // Pause for a moment
  133. }
  134. }
  135. // Theater-marquee-style chasing lights. Pass in a color (32-bit value,
  136. // a la strip.Color(r,g,b) as mentioned above), and a delay time (in ms)
  137. // between frames.
  138. void theaterChase(uint32_t color, int wait)
  139. {
  140. for (int a = 0; a < 10; a++)
  141. { // Repeat 10 times...
  142. for (int b = 0; b < 3; b++)
  143. { // 'b' counts from 0 to 2...
  144. strip.clear(); // Set all pixels in RAM to 0 (off)
  145. // 'c' counts up from 'b' to end of strip in steps of 3...
  146. for (int c = b; c < strip.numPixels(); c += 3)
  147. {
  148. strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
  149. }
  150. strip.show(); // Update strip with new contents
  151. delay(wait); // Pause for a moment
  152. }
  153. }
  154. }
  155. // Rainbow cycle along whole strip. Pass delay time (in ms) between frames.
  156. void rainbow(int wait)
  157. {
  158. // Hue of first pixel runs 5 complete loops through the color wheel.
  159. // Color wheel has a range of 65536 but it's OK if we roll over, so
  160. // just count from 0 to 5*65536. Adding 256 to firstPixelHue each time
  161. // means we'll make 5*65536/256 = 1280 passes through this outer loop:
  162. for (long firstPixelHue = 0; firstPixelHue < 5 * 65536; firstPixelHue += 256)
  163. {
  164. for (int i = 0; i < strip.numPixels(); i++)
  165. { // For each pixel in strip...
  166. // Offset pixel hue by an amount to make one full revolution of the
  167. // color wheel (range of 65536) along the length of the strip
  168. // (strip.numPixels() steps):
  169. int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
  170. // strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or
  171. // optionally add saturation and value (brightness) (each 0 to 255).
  172. // Here we're using just the single-argument hue variant. The result
  173. // is passed through strip.gamma32() to provide 'truer' colors
  174. // before assigning to each pixel:
  175. strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
  176. }
  177. strip.show(); // Update strip with new contents
  178. delay(wait); // Pause for a moment
  179. }
  180. }
  181. // Rainbow-enhanced theater marquee. Pass delay time (in ms) between frames.
  182. void theaterChaseRainbow(int wait)
  183. {
  184. int firstPixelHue = 0; // First pixel starts at red (hue 0)
  185. for (int a = 0; a < 30; a++)
  186. { // Repeat 30 times...
  187. for (int b = 0; b < 3; b++)
  188. { // 'b' counts from 0 to 2...
  189. strip.clear(); // Set all pixels in RAM to 0 (off)
  190. // 'c' counts up from 'b' to end of strip in increments of 3...
  191. for (int c = b; c < strip.numPixels(); c += 3)
  192. {
  193. // hue of pixel 'c' is offset by an amount to make one full
  194. // revolution of the color wheel (range 65536) along the length
  195. // of the strip (strip.numPixels() steps):
  196. int hue = firstPixelHue + c * 65536L / strip.numPixels();
  197. uint32_t color = strip.gamma32(strip.ColorHSV(hue)); // hue -> RGB
  198. strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
  199. }
  200. strip.show(); // Update strip with new contents
  201. delay(wait); // Pause for a moment
  202. firstPixelHue += 65536 / 90; // One cycle of color wheel over 90 frames
  203. }
  204. }
  205. }