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