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