Advertisement
Guest User

Untitled

a guest
May 20th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.71 KB | None | 0 0
  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  18
  14. #define NUM_LEDS    7    
  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 30
  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 = 255;
  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( 500 ); // power-up safety delay
  79.  
  80.   // Start serial to PC for debug.
  81.   Serial.begin(115200);  
  82.   delay(500);
  83.  
  84.   Serial.println("Setting up FastLED...");
  85.   FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  86.   FastLED.setBrightness(  BRIGHTNESS );
  87.   pinMode(LED, OUTPUT);  
  88.      
  89.   pinMode(RFM69_RST, OUTPUT);
  90.   digitalWrite(RFM69_RST, LOW);
  91.  
  92.   Serial.println("Starting radio comms");
  93.  
  94.   // manual reset
  95.   digitalWrite(RFM69_RST, HIGH);
  96.   delay(10);
  97.   digitalWrite(RFM69_RST, LOW);
  98.   delay(10);
  99.   /*
  100.   if (!rf69.init()) {
  101.     Serial.println("RFM69 radio init failed");
  102.     while (1);
  103.   }
  104.   Serial.println("RFM69 radio init OK!");
  105.  
  106.  
  107.   // Defaults after init are 434.0MHz, modulation GFSK_Rb250Fd250, +13dbM (for low power module)
  108.   // No encryption
  109.   if (!rf69.setFrequency(RF69_FREQ)) {
  110.     Serial.println("setFrequency failed");
  111.   }
  112.  
  113.   // If you are using a high power RF69 eg RFM69HW, you *must* set a Tx power with the
  114.   // ishighpowermodule flag set like this:
  115.   rf69.setTxPower(14, true);
  116.  
  117.   // The encryption key has to be the same as the one in the server
  118.   uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  119.                     0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
  120.   rf69.setEncryptionKey(key);
  121.  
  122.   pinMode(LED, OUTPUT);
  123.  
  124.   Serial.print("RFM69 radio @");  Serial.print((int)RF69_FREQ);  Serial.println(" MHz");
  125.   */
  126.  
  127.   Serial.println("Testing neopixels.");
  128.   for(int i=0;i<NUM_LEDS;i++){
  129.     leds(i, i) = CRGB::Red;
  130.     FastLED.show();
  131.     delay(200);
  132.     leds(i, i) = CRGB::Black;
  133.     FastLED.show();
  134.   }  
  135.  
  136.   delay(500);
  137.   //Gradient();  //So the lights come un upon startup, even if the trigger box is off
  138. }
  139.  
  140.   void loop(){
  141.  
  142. /*  
  143.  
  144.   if (rf69.waitAvailableTimeout(1000)) {
  145.     // Should be a message for us now  
  146.     uint8_t buf[RH_RF69_MAX_MESSAGE_LEN];
  147.     uint8_t len = sizeof(buf);
  148.    
  149.     if (! rf69.recv(buf, &len)) {
  150.       Serial.println("Receive failed");
  151.       return;
  152.     }
  153.    
  154.     //digitalWrite(LED, HIGH);
  155.    
  156.     //rf69.printBuffer("Received: ", buf, len);
  157.     //buf[len] = 0;
  158.    
  159.     //Serial.print("Got: "); Serial.println((char*)buf);
  160.     //Serial.print("RSSI: "); Serial.println(rf69.lastRssi(), DEC);
  161.  
  162.    
  163.     char radiopacket[20] = "Button #";//prep reply message to send
  164.  
  165.    
  166.  
  167.     if (buf[0]=='A'){ //the letter sent from the button
  168.       ledMode(0);
  169.         radiopacket[8] = 'A';  
  170.     }
  171.      else if (buf[0]=='B'){ //the letter sent from the button
  172.       ledMode(1);
  173.       radiopacket[8] = 'B';    
  174.     }
  175.    
  176.      else if (buf[0]=='C'){ //the letter sent from the button
  177.       ledMode(2);
  178.        radiopacket[8] = 'C';    
  179.     }
  180.  
  181.      else if (buf[0]=='D'){ //the letter sent from the button
  182.       ledMode(3);
  183.       radiopacket[8] = 'D';    
  184.     }
  185.          else if (buf[0]=='E'){ //the letter sent from the button
  186.       ledMode(4);
  187.       radiopacket[8] = 'E';    
  188.     }
  189.    
  190.      else if (buf[0]=='F'){ //the letter sent from the button
  191.       ledMode(5);
  192.        radiopacket[8] = 'F';    
  193.     }
  194.  
  195.      else if (buf[0]=='G'){ //the letter sent from the button
  196.       ledMode(6);
  197.       radiopacket[8] = 'G';
  198.          
  199.     }
  200.      else if (buf[0]=='H'){ //the letter sent from the button
  201.       ledMode(7);
  202.       radiopacket[8] = 'H';    
  203.     }
  204.    
  205.      else if (buf[0]=='I'){ //the letter sent from the button
  206.       ledMode(8);
  207.        radiopacket[8] = 'I';    
  208.     }
  209.  
  210.      else if (buf[0]=='J'){ //the letter sent from the button
  211.       ledMode(9);
  212.       radiopacket[8] = 'J';    
  213.     }    
  214.      else if (buf[0]=='K'){ //the letter sent from the button
  215.       ledMode(10);
  216.       radiopacket[8] = 'K';    
  217.     }
  218.    
  219.      else if (buf[0]=='L'){ //the letter sent from the button
  220.       ledMode(11);
  221.        radiopacket[8] = 'L';    
  222.     }
  223.  
  224.      else if (buf[0]=='M'){ //the letter sent from the button
  225.       ledMode(12);
  226.       radiopacket[8] = 'M';    
  227.     }
  228.      else if (buf[0]=='N'){ //the letter sent from the button
  229.       ledMode(13);
  230.       radiopacket[8] = 'N';    
  231.     }
  232.      else if (buf[0]=='O'){ //the letter sent from the button
  233.       ledMode(14);
  234.       radiopacket[8] = 'O';    
  235.     }
  236.      else if (buf[0]=='P'){ //the letter sent from the button
  237.       ledMode(15);
  238.       radiopacket[8] = 'P';    
  239.     }
  240.          else if (buf[0]=='Q'){ //the letter sent from the button
  241.       ledMode(16);
  242.       radiopacket[8] = 'Q';    
  243.     }
  244.          else if (buf[0]=='R'){ //the letter sent from the button
  245.       ledMode(17);
  246.       radiopacket[8] = 'R';    
  247.     }
  248.              else if (buf[0]=='S'){ //the letter sent from the button
  249.       ledMode(18);
  250.       radiopacket[8] = 'S';    
  251.     }
  252.          else if (buf[0]=='T'){ //the letter sent from the button
  253.       ledMode(19);
  254.       radiopacket[8] = 'T';    
  255.     }
  256.          else if (buf[0]=='Z'){ //the letter sent from the button
  257.       ledMode(20);
  258.       radiopacket[8] = 'Z';    
  259.     }
  260.  
  261.  
  262.     radiopacket[9] = 0;
  263.  
  264.     //Serial.print("Sending "); Serial.println(radiopacket);
  265.     //rf69.send((uint8_t *)radiopacket, strlen(radiopacket));
  266.     //rf69.waitPacketSent();
  267.  
  268.     //digitalWrite(LED, LOW);
  269.   }
  270. */
  271.   Fire2012();
  272.   delay(1000 / UPDATES_PER_SECOND);
  273.   FastLED.show();
  274.  
  275. }
  276.  
  277.  
  278. void ledMode(int i) {
  279.   switch(i){
  280.     case 0: HUE=0; SATURATION=255; BRIGHTNESS=200; Solid();    // red
  281.             break;
  282.     case 1: HUE=40; SATURATION=255; BRIGHTNESS=200; Solid();    // gold
  283.             break;
  284.     case 2: HUE=100; SATURATION=255; BRIGHTNESS=200; Solid();    // green
  285.             break;
  286.     case 3: HUE=140; SATURATION=255; BRIGHTNESS=200; Solid();    // Blue  
  287.             break;
  288.     case 4: HUE=180; SATURATION=255; BRIGHTNESS=200; Solid();    // purple
  289.             break;
  290.     case 5: HUE=220; SATURATION=255; BRIGHTNESS=200; Solid();    // pink
  291.             break;
  292.     case 6: HUE=0; SATURATION=0; BRIGHTNESS=200; Solid();    // white
  293.             break;
  294.     case 7: HUE=0; BRIGHTNESS=0; Solid();    // off
  295.             break;
  296.     case 8: HUE=0; SATURATION=255; BRIGHTNESS=200; Gradient();    // red
  297.             break;
  298.     case 9: HUE=40; SATURATION=255; BRIGHTNESS=200; Gradient();    // gold
  299.             break;
  300.     case 10: HUE=100; SATURATION=255; BRIGHTNESS=200; Gradient();    // green
  301.             break;
  302.     case 11: HUE=140; SATURATION=255; BRIGHTNESS=200; Gradient();    // blue
  303.             break;
  304.     case 12:HUE=180; SATURATION=255; BRIGHTNESS=200; Gradient();    // purple
  305.             break;
  306.     case 13:HUE=220; SATURATION=255; BRIGHTNESS=200; Gradient();    // pink
  307.             break;
  308.     case 14:HUE=160; SATURATION=50; BRIGHTNESS=200; Gradient();    // white
  309.             break;
  310.     case 15:SATURATION=255; BRIGHTNESS=200; Rainbow_Fade();    // rainbow fade
  311.             break;
  312.     case 16:STEPS=4; SATURATION=255; BRIGHTNESS=200; Rainbow();   //rainbow 2
  313.             break;
  314.     case 17:STEPS=20; BRIGHTNESS=200; SATURATION=255; Rainbow();    // rainbow 3
  315.             break;
  316.     case 20:BRIGHTNESS=200;
  317.             break;
  318.            
  319.   }
  320. }
  321.  
  322. #define COOLING  30  // How much heat is lost each frame. Default 50, suggested range 20-100
  323. #define SPARKING 75 // How likely a new spark is each frame. Default 120, suggested range 50-200.
  324. #define TEMPERATURE 150 // How hot new sparks are. Range 0-155
  325.  
  326. void Fire2012()
  327. {
  328. // Array of temperature readings at each simulation cell
  329.   static byte heat[NUM_LEDS];
  330.  
  331.   // Step 1.  Cool down every cell a little
  332.     for( int i = 0; i < NUM_LEDS; i++) {
  333.       heat[i] = qsub8( heat[i],  random8(0, ((COOLING * 10) / NUM_LEDS) + 2));
  334.     }
  335.   /*
  336.     // Step 2.  Heat from each cell drifts 'up' and diffuses a little
  337.     for( int k= NUM_LEDS - 1; k >= 2; k--) {
  338.       heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
  339.     }
  340.   */  
  341.     // Step 3.  Randomly ignite new 'sparks' of heat near the bottom
  342.     if( random8() < SPARKING ) {
  343.       int y = random8(7);
  344.       heat[y] = qadd8( heat[y], random8(TEMPERATURE, TEMPERATURE + 100) );
  345.     }
  346.  
  347.     // Step 4.  Map from heat cells to LED colors
  348.     for( int j = 0; j < NUM_LEDS; j++) {
  349.       leds(j, j) = HeatColor( heat[j]);
  350.       //pixels.setPixelColor(j, color.r, color.g, color.b);
  351.     }
  352.    
  353. }
  354.  
  355.  
  356. // GRADIENT --------------------------------------------------------------
  357. void Gradient()
  358. {
  359.   SetupGradientPalette();
  360.  
  361.   static uint8_t startIndex = 0;
  362.   startIndex = startIndex + 1;  // motion speed
  363.   FillLEDsFromPaletteColors( startIndex);
  364.   FastLED.show();
  365.   FastLED.delay(SPEEDO);
  366. }
  367.  
  368. // SOLID ----------------------------------------------------
  369. void Solid()
  370. {
  371.    fill_solid(leds, NUM_LEDS, CHSV(HUE, SATURATION, BRIGHTNESS));
  372.    FastLED.show();
  373.    delay(20);
  374.  
  375. }
  376.  
  377. // RAINBOW --------------------------------------------------
  378. void Rainbow()
  379. {
  380.   FastLED.setBrightness(  BRIGHTNESS );
  381.   currentPalette = RainbowColors_p;
  382.  
  383.   static uint8_t startIndex = 0;
  384.   startIndex = startIndex + 1;
  385.  
  386.   FillLEDsFromPaletteColors( startIndex);
  387.    
  388.   FastLED.show();
  389.   FastLED.delay(SPEEDO);  
  390. }
  391. // RAINBOW FADE --------------------------------------------------
  392. void Rainbow_Fade() {                         //-m2-FADE ALL LEDS THROUGH HSV RAINBOW
  393.     HUE++;
  394.     if (HUE > 255) {HUE = 0;}
  395.     for(int idex = 0 ; idex < NUM_LEDS; idex++ ) {
  396.       leds[idex] = CHSV(HUE, SATURATION, BRIGHTNESS);
  397.     }
  398.     LEDS.show();    
  399.     delay(SPEEDO);
  400. }
  401.  
  402.  
  403. void SetupGradientPalette()
  404. {
  405.   CRGB light = CHSV( HUE + 25, SATURATION - 20, BRIGHTNESS);
  406.   CRGB dark  = CHSV( HUE, SATURATION - 15, BRIGHTNESS);
  407.   CRGB medium = CHSV ( HUE - 25, SATURATION, BRIGHTNESS);
  408.  
  409.   currentPalette = CRGBPalette16(
  410.     light,  light,  light,  light,
  411.     medium, medium, medium,  medium,
  412.     dark,  dark,  dark,  dark,
  413.     medium, medium, medium,  medium );
  414. }
  415.  
  416. void FillLEDsFromPaletteColors( uint8_t colorIndex)
  417. {
  418.   uint8_t brightness = BRIGHTNESS;
  419.  
  420.   for( int i = 0; i < NUM_LEDS; i++) {
  421.     leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending);
  422.     colorIndex += STEPS;
  423.   }
  424. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement