Krolyxon
Commit 3120783

Branches

Tags

Refs →

orion-rf.piolibdepsesp32-s3-n16r8Adafruit NeoPixelexamplesStrandtestBLE_nodelayStrandtestBLE_nodelay.ino

198 lines · 7.4 KB

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 no delay because this example does not block the threads execution
15 * so the change will be shown immediately and will not need to wait for the current
16 * animation to end.
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:
27Adafruit_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
53BLESerial BLESerial(BLE_REQ, BLE_RDY, BLE_RST);
54
55uint8_t current_state = 0;
56uint8_t rgb_values[3];
57
58void 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
74void 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// Some functions of our own for creating animated effects -----------------
119
120// Fill strip pixels one after another with a color. Strip is NOT cleared
121// first; anything there will be covered pixel by pixel. Pass in color
122// (as a single 'packed' 32-bit value, which you can get by calling
123// strip.Color(red, green, blue) as shown in the loop() function above),
124// and a delay time (in milliseconds) between pixels.
125void colorWipe(uint32_t color, int wait) {
126 if(pixelInterval != wait)
127 pixelInterval = wait; // Update delay time
128 strip.setPixelColor(pixelCurrent, color); // Set pixel's color (in RAM)
129 strip.show(); // Update strip to match
130 pixelCurrent++; // Advance current pixel
131 if(pixelCurrent >= pixelNumber) // Loop the pattern from the first LED
132 pixelCurrent = 0;
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.
138void theaterChase(uint32_t color, int wait) {
139 if(pixelInterval != wait)
140 pixelInterval = wait; // Update delay time
141 for(int i = 0; i < pixelNumber; i++) {
142 strip.setPixelColor(i + pixelQueue, color); // Set pixel's color (in RAM)
143 }
144 strip.show(); // Update strip to match
145 for(int i=0; i < pixelNumber; i+3) {
146 strip.setPixelColor(i + pixelQueue, strip.Color(0, 0, 0)); // Set pixel's color (in RAM)
147 }
148 pixelQueue++; // Advance current pixel
149 if(pixelQueue >= 3)
150 pixelQueue = 0; // Loop the pattern from the first LED
151}
152
153// Rainbow cycle along whole strip. Pass delay time (in ms) between frames.
154void rainbow(uint8_t wait) {
155 if(pixelInterval != wait)
156 pixelInterval = wait;
157 for(uint16_t i=0; i < pixelNumber; i++) {
158 strip.setPixelColor(i, Wheel((i + pixelCycle) & 255)); // Update delay time
159 }
160 strip.show(); // Update strip to match
161 pixelCycle++; // Advance current cycle
162 if(pixelCycle >= 256)
163 pixelCycle = 0; // Loop the cycle back to the begining
164}
165
166//Theatre-style crawling lights with rainbow effect
167void theaterChaseRainbow(uint8_t wait) {
168 if(pixelInterval != wait)
169 pixelInterval = wait; // Update delay time
170 for(int i=0; i < pixelNumber; i+3) {
171 strip.setPixelColor(i + pixelQueue, Wheel((i + pixelCycle) % 255)); // Update delay time
172 }
173 strip.show();
174 for(int i=0; i < pixelNumber; i+3) {
175 strip.setPixelColor(i + pixelQueue, strip.Color(0, 0, 0)); // Update delay time
176 }
177 pixelQueue++; // Advance current queue
178 pixelCycle++; // Advance current cycle
179 if(pixelQueue >= 3)
180 pixelQueue = 0; // Loop
181 if(pixelCycle >= 256)
182 pixelCycle = 0; // Loop
183}
184
185// Input a value 0 to 255 to get a color value.
186// The colours are a transition r - g - b - back to r.
187uint32_t Wheel(byte WheelPos) {
188 WheelPos = 255 - WheelPos;
189 if(WheelPos < 85) {
190 return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
191 }
192 if(WheelPos < 170) {
193 WheelPos -= 85;
194 return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
195 }
196 WheelPos -= 170;
197 return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
198}