PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StrandtestBLE.ino 6.0KB

3 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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
  24. // Parameter 1 = number of pixels in strip
  25. // Parameter 2 = Arduino pin number (most are valid)
  26. // Parameter 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. Adafruit_NeoPixel strip = Adafruit_NeoPixel(64, PIN, NEO_GRB + NEO_KHZ800);
  33. // IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
  34. // pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
  35. // and minimize distance between Arduino and first pixel. Avoid connecting
  36. // on a live circuit...if you must, connect GND first.
  37. // define pins (varies per shield/board)
  38. #define BLE_REQ 10
  39. #define BLE_RDY 2
  40. #define BLE_RST 9
  41. // create ble serial instance, see pinouts above
  42. BLESerial BLESerial(BLE_REQ, BLE_RDY, BLE_RST);
  43. uint8_t current_state = 0;
  44. uint8_t rgb_values[3];
  45. void setup() {
  46. Serial.begin(115200);
  47. Serial.println("Hello World!");
  48. // custom services and characteristics can be added as well
  49. BLESerial.setLocalName("UART_HS");
  50. BLESerial.begin();
  51. strip.begin();
  52. changeColor(strip.Color(0, 0, 0));
  53. //pinMode(PIN, OUTPUT);
  54. //digitalWrite(PIN, LOW);
  55. current_state = 'a';
  56. }
  57. void loop() {
  58. while(BLESerial.available()) {
  59. uint8_t character = BLESerial.read();
  60. switch(character) {
  61. case 'a':
  62. case 'b':
  63. case 'c':
  64. case 'd':
  65. case 'e':
  66. case 'f':
  67. case 'g':
  68. case 'h':
  69. case 'i':
  70. current_state = character;
  71. break;
  72. };
  73. }
  74. switch(current_state) {
  75. case 'a':
  76. colorWipe(strip.Color(255, 0, 0), 20); // Red
  77. break;
  78. case 'b':
  79. colorWipe(strip.Color(0, 255, 0), 20); // Green
  80. break;
  81. case 'c':
  82. colorWipe(strip.Color(0, 0, 255), 20); // Blue
  83. break;
  84. case 'd':
  85. theaterChase(strip.Color(255, 0, 0), 20); // Red
  86. break;
  87. case 'e':
  88. theaterChase(strip.Color(0, 255, 0), 20); // Green
  89. break;
  90. case 'f':
  91. theaterChase(strip.Color(255, 0, 255), 20); // Green
  92. break;
  93. case 'g':
  94. rainbowCycle(20);
  95. break;
  96. case 'h':
  97. rainbow(20);
  98. break;
  99. case 'i':
  100. theaterChaseRainbow(20);
  101. break;
  102. }
  103. }
  104. void changeColor(uint32_t c) {
  105. for(uint16_t i=0; i<strip.numPixels(); i++) {
  106. strip.setPixelColor(i, c);
  107. }
  108. strip.show();
  109. }
  110. // Fill the dots one after the other with a color
  111. void colorWipe(uint32_t c, uint8_t wait) {
  112. for(uint16_t i=0; i<strip.numPixels(); i++) {
  113. strip.setPixelColor(i, c);
  114. delay(wait);
  115. strip.show();
  116. delay(wait);
  117. }
  118. }
  119. void rainbow(uint8_t wait) {
  120. uint16_t i, j;
  121. for(j=0; j<256; j++) {
  122. for(i=0; i<strip.numPixels(); i++) {
  123. strip.setPixelColor(i, Wheel((i+j) & 255));
  124. }
  125. strip.show();
  126. delay(wait);
  127. }
  128. }
  129. // Slightly different, this makes the rainbow equally distributed throughout
  130. void rainbowCycle(uint8_t wait) {
  131. uint16_t i, j;
  132. for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
  133. for(i=0; i< strip.numPixels(); i++) {
  134. strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
  135. }
  136. strip.show();
  137. delay(wait);
  138. }
  139. }
  140. //Theatre-style crawling lights.
  141. void theaterChase(uint32_t c, uint8_t wait) {
  142. for (int j=0; j<10; j++) { //do 10 cycles of chasing
  143. for (int q=0; q < 3; q++) {
  144. for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
  145. strip.setPixelColor(i+q, c); //turn every third pixel on
  146. }
  147. strip.show();
  148. delay(wait);
  149. for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
  150. strip.setPixelColor(i+q, 0); //turn every third pixel off
  151. }
  152. }
  153. }
  154. }
  155. //Theatre-style crawling lights with rainbow effect
  156. void theaterChaseRainbow(uint8_t wait) {
  157. for (int j=0; j < 256; j++) { // cycle all 256 colors in the wheel
  158. for (int q=0; q < 3; q++) {
  159. for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
  160. strip.setPixelColor(i+q, Wheel( (i+j) % 255)); //turn every third pixel on
  161. }
  162. strip.show();
  163. delay(wait);
  164. for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
  165. strip.setPixelColor(i+q, 0); //turn every third pixel off
  166. }
  167. }
  168. }
  169. }
  170. // Input a value 0 to 255 to get a color value.
  171. // The colours are a transition r - g - b - back to r.
  172. uint32_t Wheel(byte WheelPos) {
  173. WheelPos = 255 - WheelPos;
  174. if(WheelPos < 85) {
  175. return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  176. }
  177. if(WheelPos < 170) {
  178. WheelPos -= 85;
  179. return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  180. }
  181. WheelPos -= 170;
  182. return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  183. }