|
| 1 | +//Ada_remoteFXTrigger_RX_NeoPixel |
| 2 | +//Remote Effects Trigger Box Receiver |
| 3 | +//by John Park & Erin St Blaine |
| 4 | +//for Adafruit Industries |
| 5 | +// |
| 6 | +// Button box receiver with NeoPixels using FastLED |
| 7 | +// |
| 8 | +// |
| 9 | +//MIT License |
| 10 | + |
| 11 | +#include <FastLED.h> |
| 12 | + |
| 13 | +#define LED_PIN 12 |
| 14 | +#define NUM_LEDS 20 |
| 15 | +#define LED_TYPE WS2812B |
| 16 | +#define COLOR_ORDER GRB |
| 17 | + |
| 18 | +CRGBArray<NUM_LEDS> leds; |
| 19 | + |
| 20 | +#include <SPI.h> |
| 21 | +#include <RH_RF69.h> |
| 22 | +#include <Wire.h> |
| 23 | + |
| 24 | +#define LED 13 |
| 25 | + |
| 26 | +/********** NeoPixel Setup *************/ |
| 27 | + |
| 28 | +#define UPDATES_PER_SECOND 100 |
| 29 | +CRGBPalette16 currentPalette( CRGB::Black); |
| 30 | +CRGBPalette16 targetPalette( PartyColors_p ); |
| 31 | +TBlendType currentBlending; |
| 32 | + |
| 33 | +int SPEEDO = 25; |
| 34 | +int STEPS = 20; |
| 35 | +int HUE = 200; // starting color |
| 36 | +int SATURATION = 255; |
| 37 | +int BRIGHTNESS = 200; |
| 38 | +int glitter = 0; |
| 39 | + |
| 40 | +/************ Radio Setup ***************/ |
| 41 | + |
| 42 | +// Change to 434.0 or other frequency, must match RX's freq! |
| 43 | +#define RF69_FREQ 915.0 |
| 44 | + |
| 45 | +#if defined (__AVR_ATmega32U4__) // Feather 32u4 w/Radio |
| 46 | + #define RFM69_CS 8 |
| 47 | + #define RFM69_INT 7 |
| 48 | + #define RFM69_RST 4 |
| 49 | +#endif |
| 50 | + |
| 51 | +#if defined(ARDUINO_SAMD_FEATHER_M0) // Feather M0 w/Radio |
| 52 | + #define RFM69_CS 8 |
| 53 | + #define RFM69_INT 3 |
| 54 | + #define RFM69_RST 4 |
| 55 | +#endif |
| 56 | + |
| 57 | +#if defined (__AVR_ATmega328P__) // Feather 328P w/wing |
| 58 | + #define RFM69_INT 3 // |
| 59 | + #define RFM69_CS 4 // |
| 60 | + #define RFM69_RST 2 // "A" |
| 61 | +#endif |
| 62 | + |
| 63 | +#if defined(ESP32) // ESP32 feather w/wing |
| 64 | + #define RFM69_RST 13 // same as LED |
| 65 | + #define RFM69_CS 33 // "B" |
| 66 | + #define RFM69_INT 27 // "A" |
| 67 | +#endif |
| 68 | + |
| 69 | + |
| 70 | +// Singleton instance of the radio driver |
| 71 | +RH_RF69 rf69(RFM69_CS, RFM69_INT); |
| 72 | + |
| 73 | + |
| 74 | +bool oldState = HIGH; |
| 75 | + |
| 76 | + |
| 77 | +void setup() { |
| 78 | + delay( 3000 ); // power-up safety delay |
| 79 | + FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip ); |
| 80 | + FastLED.setBrightness( BRIGHTNESS ); |
| 81 | + pinMode(LED, OUTPUT); |
| 82 | + |
| 83 | + pinMode(RFM69_RST, OUTPUT); |
| 84 | + digitalWrite(RFM69_RST, LOW); |
| 85 | + |
| 86 | + Serial.println("Feather RFM69 RX/TX Test!"); |
| 87 | + |
| 88 | + // manual reset |
| 89 | + digitalWrite(RFM69_RST, HIGH); |
| 90 | + delay(10); |
| 91 | + digitalWrite(RFM69_RST, LOW); |
| 92 | + delay(10); |
| 93 | + |
| 94 | + if (!rf69.init()) { |
| 95 | + Serial.println("RFM69 radio init failed"); |
| 96 | + while (1); |
| 97 | + } |
| 98 | + Serial.println("RFM69 radio init OK!"); |
| 99 | + |
| 100 | + // Defaults after init are 434.0MHz, modulation GFSK_Rb250Fd250, +13dbM (for low power module) |
| 101 | + // No encryption |
| 102 | + if (!rf69.setFrequency(RF69_FREQ)) { |
| 103 | + Serial.println("setFrequency failed"); |
| 104 | + } |
| 105 | + |
| 106 | + // If you are using a high power RF69 eg RFM69HW, you *must* set a Tx power with the |
| 107 | + // ishighpowermodule flag set like this: |
| 108 | + rf69.setTxPower(14, true); |
| 109 | + |
| 110 | + // The encryption key has to be the same as the one in the server |
| 111 | + uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 112 | + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; |
| 113 | + rf69.setEncryptionKey(key); |
| 114 | + |
| 115 | + pinMode(LED, OUTPUT); |
| 116 | + |
| 117 | + Serial.print("RFM69 radio @"); Serial.print((int)RF69_FREQ); Serial.println(" MHz"); |
| 118 | + |
| 119 | + delay(500); |
| 120 | + Gradient(); //So the lights come un upon startup, even if the trigger box is off |
| 121 | +} |
| 122 | + |
| 123 | + void loop(){ |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | + if (rf69.waitAvailableTimeout(1000)) { |
| 128 | + // Should be a message for us now |
| 129 | + uint8_t buf[RH_RF69_MAX_MESSAGE_LEN]; |
| 130 | + uint8_t len = sizeof(buf); |
| 131 | + |
| 132 | + if (! rf69.recv(buf, &len)) { |
| 133 | + Serial.println("Receive failed"); |
| 134 | + return; |
| 135 | + } |
| 136 | + |
| 137 | + //digitalWrite(LED, HIGH); |
| 138 | + |
| 139 | + //rf69.printBuffer("Received: ", buf, len); |
| 140 | + //buf[len] = 0; |
| 141 | + |
| 142 | + //Serial.print("Got: "); Serial.println((char*)buf); |
| 143 | + //Serial.print("RSSI: "); Serial.println(rf69.lastRssi(), DEC); |
| 144 | + |
| 145 | + |
| 146 | + char radiopacket[20] = "Button #";//prep reply message to send |
| 147 | + |
| 148 | + |
| 149 | + |
| 150 | + if (buf[0]=='A'){ //the letter sent from the button |
| 151 | + ledMode(0); |
| 152 | + radiopacket[8] = 'A'; |
| 153 | + } |
| 154 | + else if (buf[0]=='B'){ //the letter sent from the button |
| 155 | + ledMode(1); |
| 156 | + radiopacket[8] = 'B'; |
| 157 | + } |
| 158 | + |
| 159 | + else if (buf[0]=='C'){ //the letter sent from the button |
| 160 | + ledMode(2); |
| 161 | + radiopacket[8] = 'C'; |
| 162 | + } |
| 163 | + |
| 164 | + else if (buf[0]=='D'){ //the letter sent from the button |
| 165 | + ledMode(3); |
| 166 | + radiopacket[8] = 'D'; |
| 167 | + } |
| 168 | + else if (buf[0]=='E'){ //the letter sent from the button |
| 169 | + ledMode(4); |
| 170 | + radiopacket[8] = 'E'; |
| 171 | + } |
| 172 | + |
| 173 | + else if (buf[0]=='F'){ //the letter sent from the button |
| 174 | + ledMode(5); |
| 175 | + radiopacket[8] = 'F'; |
| 176 | + } |
| 177 | + |
| 178 | + else if (buf[0]=='G'){ //the letter sent from the button |
| 179 | + ledMode(6); |
| 180 | + radiopacket[8] = 'G'; |
| 181 | + |
| 182 | + } |
| 183 | + else if (buf[0]=='H'){ //the letter sent from the button |
| 184 | + ledMode(7); |
| 185 | + radiopacket[8] = 'H'; |
| 186 | + } |
| 187 | + |
| 188 | + else if (buf[0]=='I'){ //the letter sent from the button |
| 189 | + ledMode(8); |
| 190 | + radiopacket[8] = 'I'; |
| 191 | + } |
| 192 | + |
| 193 | + else if (buf[0]=='J'){ //the letter sent from the button |
| 194 | + ledMode(9); |
| 195 | + radiopacket[8] = 'J'; |
| 196 | + } |
| 197 | + else if (buf[0]=='K'){ //the letter sent from the button |
| 198 | + ledMode(10); |
| 199 | + radiopacket[8] = 'K'; |
| 200 | + } |
| 201 | + |
| 202 | + else if (buf[0]=='L'){ //the letter sent from the button |
| 203 | + ledMode(11); |
| 204 | + radiopacket[8] = 'L'; |
| 205 | + } |
| 206 | + |
| 207 | + else if (buf[0]=='M'){ //the letter sent from the button |
| 208 | + ledMode(12); |
| 209 | + radiopacket[8] = 'M'; |
| 210 | + } |
| 211 | + else if (buf[0]=='N'){ //the letter sent from the button |
| 212 | + ledMode(13); |
| 213 | + radiopacket[8] = 'N'; |
| 214 | + } |
| 215 | + else if (buf[0]=='O'){ //the letter sent from the button |
| 216 | + ledMode(14); |
| 217 | + radiopacket[8] = 'O'; |
| 218 | + } |
| 219 | + else if (buf[0]=='P'){ //the letter sent from the button |
| 220 | + ledMode(15); |
| 221 | + radiopacket[8] = 'P'; |
| 222 | + } |
| 223 | + else if (buf[0]=='Q'){ //the letter sent from the button |
| 224 | + ledMode(16); |
| 225 | + radiopacket[8] = 'Q'; |
| 226 | + } |
| 227 | + else if (buf[0]=='R'){ //the letter sent from the button |
| 228 | + ledMode(17); |
| 229 | + radiopacket[8] = 'R'; |
| 230 | + } |
| 231 | + else if (buf[0]=='S'){ //the letter sent from the button |
| 232 | + ledMode(18); |
| 233 | + radiopacket[8] = 'S'; |
| 234 | + } |
| 235 | + else if (buf[0]=='T'){ //the letter sent from the button |
| 236 | + ledMode(19); |
| 237 | + radiopacket[8] = 'T'; |
| 238 | + } |
| 239 | + else if (buf[0]=='Z'){ //the letter sent from the button |
| 240 | + ledMode(20); |
| 241 | + radiopacket[8] = 'Z'; |
| 242 | + } |
| 243 | + |
| 244 | + |
| 245 | + /* radiopacket[9] = 0; |
| 246 | +
|
| 247 | + Serial.print("Sending "); Serial.println(radiopacket); |
| 248 | + rf69.send((uint8_t *)radiopacket, strlen(radiopacket)); |
| 249 | + rf69.waitPacketSent(); */ |
| 250 | + |
| 251 | + digitalWrite(LED, LOW); |
| 252 | + } |
| 253 | + |
| 254 | +} |
| 255 | + |
| 256 | + |
| 257 | +void ledMode(int i) { |
| 258 | + switch(i){ |
| 259 | + case 0: HUE=0; SATURATION=255; BRIGHTNESS=200; Solid(); // red |
| 260 | + break; |
| 261 | + case 1: HUE=40; SATURATION=255; BRIGHTNESS=200; Solid(); // gold |
| 262 | + break; |
| 263 | + case 2: HUE=100; SATURATION=255; BRIGHTNESS=200; Solid(); // green |
| 264 | + break; |
| 265 | + case 3: HUE=140; SATURATION=255; BRIGHTNESS=200; Solid(); // Blue |
| 266 | + break; |
| 267 | + case 4: HUE=180; SATURATION=255; BRIGHTNESS=200; Solid(); // purple |
| 268 | + break; |
| 269 | + case 5: HUE=220; SATURATION=255; BRIGHTNESS=200; Solid(); // pink |
| 270 | + break; |
| 271 | + case 6: HUE=0; SATURATION=0; BRIGHTNESS=200; Solid(); // white |
| 272 | + break; |
| 273 | + case 7: HUE=0; BRIGHTNESS=0; Solid(); // off |
| 274 | + break; |
| 275 | + case 8: HUE=0; SATURATION=255; BRIGHTNESS=200; Gradient(); // red |
| 276 | + break; |
| 277 | + case 9: HUE=40; SATURATION=255; BRIGHTNESS=200; Gradient(); // gold |
| 278 | + break; |
| 279 | + case 10: HUE=100; SATURATION=255; BRIGHTNESS=200; Gradient(); // green |
| 280 | + break; |
| 281 | + case 11: HUE=140; SATURATION=255; BRIGHTNESS=200; Gradient(); // blue |
| 282 | + break; |
| 283 | + case 12:HUE=180; SATURATION=255; BRIGHTNESS=200; Gradient(); // purple |
| 284 | + break; |
| 285 | + case 13:HUE=220; SATURATION=255; BRIGHTNESS=200; Gradient(); // pink |
| 286 | + break; |
| 287 | + case 14:HUE=160; SATURATION=50; BRIGHTNESS=200; Gradient(); // white |
| 288 | + break; |
| 289 | + case 15:SATURATION=255; BRIGHTNESS=200; Rainbow_Fade(); // rainbow fade |
| 290 | + break; |
| 291 | + case 16:STEPS=4; SATURATION=255; BRIGHTNESS=200; Rainbow(); //rainbow 2 |
| 292 | + break; |
| 293 | + case 17:STEPS=20; BRIGHTNESS=200; SATURATION=255; Rainbow(); // rainbow 3 |
| 294 | + break; |
| 295 | + case 20:BRIGHTNESS=200; |
| 296 | + break; |
| 297 | + |
| 298 | + } |
| 299 | +} |
| 300 | + |
| 301 | +// GRADIENT -------------------------------------------------------------- |
| 302 | +void Gradient() |
| 303 | +{ |
| 304 | + SetupGradientPalette(); |
| 305 | + |
| 306 | + static uint8_t startIndex = 0; |
| 307 | + startIndex = startIndex + 1; // motion speed |
| 308 | + FillLEDsFromPaletteColors( startIndex); |
| 309 | + FastLED.show(); |
| 310 | + FastLED.delay(SPEEDO); |
| 311 | +} |
| 312 | + |
| 313 | +// SOLID ---------------------------------------------------- |
| 314 | +void Solid() |
| 315 | +{ |
| 316 | + fill_solid(leds, NUM_LEDS, CHSV(HUE, SATURATION, BRIGHTNESS)); |
| 317 | + FastLED.show(); |
| 318 | + delay(20); |
| 319 | + |
| 320 | +} |
| 321 | + |
| 322 | +// RAINBOW -------------------------------------------------- |
| 323 | +void Rainbow() |
| 324 | +{ |
| 325 | + FastLED.setBrightness( BRIGHTNESS ); |
| 326 | + currentPalette = RainbowColors_p; |
| 327 | + |
| 328 | + static uint8_t startIndex = 0; |
| 329 | + startIndex = startIndex + 1; |
| 330 | + |
| 331 | + FillLEDsFromPaletteColors( startIndex); |
| 332 | + |
| 333 | + FastLED.show(); |
| 334 | + FastLED.delay(SPEEDO); |
| 335 | +} |
| 336 | +// RAINBOW FADE -------------------------------------------------- |
| 337 | +void Rainbow_Fade() { //-m2-FADE ALL LEDS THROUGH HSV RAINBOW |
| 338 | + HUE++; |
| 339 | + if (HUE > 255) {HUE = 0;} |
| 340 | + for(int idex = 0 ; idex < NUM_LEDS; idex++ ) { |
| 341 | + leds[idex] = CHSV(HUE, SATURATION, BRIGHTNESS); |
| 342 | + } |
| 343 | + LEDS.show(); |
| 344 | + delay(SPEEDO); |
| 345 | +} |
| 346 | + |
| 347 | + |
| 348 | +void SetupGradientPalette() |
| 349 | +{ |
| 350 | + CRGB light = CHSV( HUE + 25, SATURATION - 20, BRIGHTNESS); |
| 351 | + CRGB dark = CHSV( HUE, SATURATION - 15, BRIGHTNESS); |
| 352 | + CRGB medium = CHSV ( HUE - 25, SATURATION, BRIGHTNESS); |
| 353 | + |
| 354 | + currentPalette = CRGBPalette16( |
| 355 | + light, light, light, light, |
| 356 | + medium, medium, medium, medium, |
| 357 | + dark, dark, dark, dark, |
| 358 | + medium, medium, medium, medium ); |
| 359 | +} |
| 360 | + |
| 361 | +void FillLEDsFromPaletteColors( uint8_t colorIndex) |
| 362 | +{ |
| 363 | + uint8_t brightness = BRIGHTNESS; |
| 364 | + |
| 365 | + for( int i = 0; i < NUM_LEDS; i++) { |
| 366 | + leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending); |
| 367 | + colorIndex += STEPS; |
| 368 | + } |
| 369 | +} |
| 370 | + |
| 371 | + |
0 commit comments