StrandtestBLE.ino 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /****************************************************************************
  2. * This example was developed by the Hackerspace San Salvador to demonstrate
  3. * the simultaneous use of the NeoPixel library and the Bluetooth SoftDevice.
  4. * To compile this example you'll need to add support for the NRF52 based
  5. * following the instructions at:
  6. * https://github.com/sandeepmistry/arduino-nRF5
  7. * Or adding the following URL to the board manager URLs on Arduino preferences:
  8. * https://sandeepmistry.github.io/arduino-nRF5/package_nRF5_boards_index.json
  9. * Then you can install the BLEPeripheral library avaiable at:
  10. * https://github.com/sandeepmistry/arduino-BLEPeripheral
  11. * To test it, compile this example and use the UART module from the nRF
  12. * Toolbox App for Android. Edit the interface and send the characters
  13. * 'a' to 'i' to switch the animation.
  14. * There is a delay because this example blocks the thread of execution but
  15. * the change will be shown after the current animation ends. (This might
  16. * take a couple of seconds)
  17. * For more info write us at: info _at- teubi.co
  18. */
  19. #include <SPI.h>
  20. #include <BLEPeripheral.h>
  21. #include "BLESerial.h"
  22. #include <Adafruit_NeoPixel.h>
  23. #define PIN 15 // Pin where NeoPixels are connected
  24. // Declare our NeoPixel strip object:
  25. Adafruit_NeoPixel strip(64, PIN, NEO_GRB + NEO_KHZ800);
  26. // Argument 1 = Number of pixels in NeoPixel strip
  27. // Argument 2 = Arduino pin number (most are valid)
  28. // Argument 3 = Pixel type flags, add together as needed:
  29. // NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
  30. // NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
  31. // NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
  32. // NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
  33. // NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
  34. // NEOPIXEL BEST PRACTICES for most reliable operation:
  35. // - Add 1000 uF CAPACITOR between NeoPixel strip's + and - connections.
  36. // - MINIMIZE WIRING LENGTH between microcontroller board and first pixel.
  37. // - NeoPixel strip's DATA-IN should pass through a 300-500 OHM RESISTOR.
  38. // - AVOID connecting NeoPixels on a LIVE CIRCUIT. If you must, ALWAYS
  39. // connect GROUND (-) first, then +, then data.
  40. // - When using a 3.3V microcontroller with a 5V-powered NeoPixel strip,
  41. // a LOGIC-LEVEL CONVERTER on the data line is STRONGLY RECOMMENDED.
  42. // (Skipping these may work OK on your workbench but can fail in the field)
  43. // define pins (varies per shield/board)
  44. #define BLE_REQ 10
  45. #define BLE_RDY 2
  46. #define BLE_RST 9
  47. // create ble serial instance, see pinouts above
  48. BLESerial BLESerial(BLE_REQ, BLE_RDY, BLE_RST);
  49. uint8_t current_state = 0;
  50. uint8_t rgb_values[3];
  51. void setup() {
  52. Serial.begin(115200);
  53. Serial.println("Hello World!");
  54. // custom services and characteristics can be added as well
  55. BLESerial.setLocalName("UART_HS");
  56. BLESerial.begin();
  57. strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
  58. strip.show(); // Turn OFF all pixels ASAP
  59. //pinMode(PIN, OUTPUT);
  60. //digitalWrite(PIN, LOW);
  61. current_state = 'a';
  62. }
  63. void loop() {
  64. while(BLESerial.available()) {
  65. uint8_t character = BLESerial.read();
  66. switch(character) {
  67. case 'a':
  68. case 'b':
  69. case 'c':
  70. case 'd':
  71. case 'e':
  72. case 'f':
  73. case 'g':
  74. case 'h':
  75. current_state = character;
  76. break;
  77. };
  78. }
  79. switch(current_state) {
  80. case 'a':
  81. colorWipe(strip.Color(255, 0, 0), 20); // Red
  82. break;
  83. case 'b':
  84. colorWipe(strip.Color( 0, 255, 0), 20); // Green
  85. break;
  86. case 'c':
  87. colorWipe(strip.Color( 0, 0, 255), 20); // Blue
  88. break;
  89. case 'd':
  90. theaterChase(strip.Color(255, 0, 0), 20); // Red
  91. break;
  92. case 'e':
  93. theaterChase(strip.Color( 0, 255, 0), 20); // Green
  94. break;
  95. case 'f':
  96. theaterChase(strip.Color(255, 0, 255), 20); // Cyan
  97. break;
  98. case 'g':
  99. rainbow(10);
  100. break;
  101. case 'h':
  102. theaterChaseRainbow(20);
  103. break;
  104. }
  105. }
  106. // Fill strip pixels one after another with a color. Strip is NOT cleared
  107. // first; anything there will be covered pixel by pixel. Pass in color
  108. // (as a single 'packed' 32-bit value, which you can get by calling
  109. // strip.Color(red, green, blue) as shown in the loop() function above),
  110. // and a delay time (in milliseconds) between pixels.
  111. void colorWipe(uint32_t color, int wait) {
  112. for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
  113. strip.setPixelColor(i, color); // Set pixel's color (in RAM)
  114. strip.show(); // Update strip to match
  115. delay(wait); // Pause for a moment
  116. }
  117. }
  118. // Theater-marquee-style chasing lights. Pass in a color (32-bit value,
  119. // a la strip.Color(r,g,b) as mentioned above), and a delay time (in ms)
  120. // between frames.
  121. void theaterChase(uint32_t color, int wait) {
  122. for(int a=0; a<10; a++) { // Repeat 10 times...
  123. for(int b=0; b<3; b++) { // 'b' counts from 0 to 2...
  124. strip.clear(); // Set all pixels in RAM to 0 (off)
  125. // 'c' counts up from 'b' to end of strip in steps of 3...
  126. for(int c=b; c<strip.numPixels(); c += 3) {
  127. strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
  128. }
  129. strip.show(); // Update strip with new contents
  130. delay(wait); // Pause for a moment
  131. }
  132. }
  133. }
  134. // Rainbow cycle along whole strip. Pass delay time (in ms) between frames.
  135. void rainbow(int wait) {
  136. // Hue of first pixel runs 5 complete loops through the color wheel.
  137. // Color wheel has a range of 65536 but it's OK if we roll over, so
  138. // just count from 0 to 5*65536. Adding 256 to firstPixelHue each time
  139. // means we'll make 5*65536/256 = 1280 passes through this outer loop:
  140. for(long firstPixelHue = 0; firstPixelHue < 5*65536; firstPixelHue += 256) {
  141. for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
  142. // Offset pixel hue by an amount to make one full revolution of the
  143. // color wheel (range of 65536) along the length of the strip
  144. // (strip.numPixels() steps):
  145. int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
  146. // strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or
  147. // optionally add saturation and value (brightness) (each 0 to 255).
  148. // Here we're using just the single-argument hue variant. The result
  149. // is passed through strip.gamma32() to provide 'truer' colors
  150. // before assigning to each pixel:
  151. strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
  152. }
  153. strip.show(); // Update strip with new contents
  154. delay(wait); // Pause for a moment
  155. }
  156. }
  157. // Rainbow-enhanced theater marquee. Pass delay time (in ms) between frames.
  158. void theaterChaseRainbow(int wait) {
  159. int firstPixelHue = 0; // First pixel starts at red (hue 0)
  160. for(int a=0; a<30; a++) { // Repeat 30 times...
  161. for(int b=0; b<3; b++) { // 'b' counts from 0 to 2...
  162. strip.clear(); // Set all pixels in RAM to 0 (off)
  163. // 'c' counts up from 'b' to end of strip in increments of 3...
  164. for(int c=b; c<strip.numPixels(); c += 3) {
  165. // hue of pixel 'c' is offset by an amount to make one full
  166. // revolution of the color wheel (range 65536) along the length
  167. // of the strip (strip.numPixels() steps):
  168. int hue = firstPixelHue + c * 65536L / strip.numPixels();
  169. uint32_t color = strip.gamma32(strip.ColorHSV(hue)); // hue -> RGB
  170. strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
  171. }
  172. strip.show(); // Update strip with new contents
  173. delay(wait); // Pause for a moment
  174. firstPixelHue += 65536 / 90; // One cycle of color wheel over 90 frames
  175. }
  176. }
  177. }