Page 1 of 1

Marshmello Helmet Code

Marshmello Helmet Code

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
Examples: "MacOS X - Your question", "MS Word - Your Tip or Trick".

Please note that switching to another language when reading a post will not work!
Posts will not have a translated counterpart.




RSS Feed

Home Forums Hardware Arduino Marshmello Helmet Code

This topic contains 6 replies, has 2 voices, and was last updated by  hans 5 days, 11 hours ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • 12329

    ronnieblaze
    Participant

    I have built and wired all the leds for a Marshmello helmet. I found the code below but the original author made it for a 12 x 54 matrix. My current setup has more rows. So would it be possible to keep the effect and remove the matrix part of the code?

    Here is a youtube link to see the code in action.

    https://youtu.be/goOs4tyvBH4?t=210

    Attachments:
    12332

    ronnieblaze
    Participant
    If this helps, this is how it looked.  This was my first version. and i lost the code for this one. 
    12335

    hans
    Keymaster

    Hi RonnieBlaze!

    Wow, nice job! 

    I assume this is what you mean; replace the XY function for something easier and more flexible?
    I’m not sure if the XY function (with huge array) is the most efficient way to do this – calculate XY instead of looking it up.

    Just taking a stab at it, untested since I do not have a matrix of LEDs available at the moment.

    I replaced some of the constants with defines as well (should be more memory efficient) – I hope I’m, not overlooking something here, since I do not fully understand why there was an array created. There could be a very good reason for that though, I’m just not seeing it (I may need more coffee since it’s still early in the morning  ).

    // Params for width and height
    #define kMatrixWidth = 54;
    #define kMatrixHeight = 12;
    #define NUM_LEDS (kMatrixWidth * kMatrixHeight)
    #define LAST_VISIBLE_LED NUM_LEDS-1
    CRGB leds[ NUM_LEDS ];
    uint16_t XY (uint16_t x, uint16_t y) {
      if ( (x >= kMatrixWidth) || (y >= kMatrixHeight) ) {
        return (LAST_VISIBLE_LED + 1);
      }
      return (y*kMatrixWidth)+x-1;
    }

    Since you’re using FastLED, you can replace all showStrip() calls with FastLED.show() – this way you will not need the definition of the showStrip() function.
    For this reason you can also get rid of the setPixel() function and replace calls like

    setPixel(i,red,green,blue);

    with

    leds[i] = CRGB( red,green,blue);

    You can also remove the function setAll()  since it’s not being used (you’re using the better fill_solid() function). 

    I also did some cleanup of the code, I personally prefer to see setup() and loop() first, and “helper” functions at the end.
    I did remove some of the global variables (I try to use those as little as possible), and replaced some of the “while” loops with “for” loops.
    You’re referring to the “leds” array by address of the first LED (leds[0]), which should not be needed and can be done by just using “leds”.

    Apologies for taking the liberty to make a few changes – not trying to be a smart ass, it just helps me read the code. 
    Of course, this code should be tested to see if everything still works as before – which I can’t since I don’t have a matrix available. The code does compile properly though.
    Since the your previous code took way too much memory for my Arduino Uno, you may like the outcome of the modified code. It still takes up quite a bit of the dynamic memory (only 16 bytes left), but the previous code actually ran 1280 bytes short on memory (I suppose this is why you were asking?).

    For comparison, the old code:

    Sketch uses 4320 bytes (13%) of program storage space. Maximum is 32256 bytes.
    Global variables use 3328 bytes (162%) of dynamic memory, leaving -1280 bytes for local variables. Maximum is 2048 bytes.
    Not enough memory

    and the new code:

    Sketch uses 2894 bytes (8%) of program storage space. Maximum is 32256 bytes.
    Global variables use 2032 bytes (99%) of dynamic memory, leaving 16 bytes for local variables. Maximum is 2048 bytes.
    Low memory available, stability problems may occur.

    All this results in:

    // Modified V2 Mello Head
    #include "FastLED.h"
    // Params for width and height
    #define kMatrixWidth 54
    #define kMatrixHeight 12
    // LED strip PIN
    #define PIN 2 
    // Number of LEDs
    #define NUM_LEDS (kMatrixWidth * kMatrixHeight)
    // FastLED LEDs array
    CRGB leds[ NUM_LEDS ];
    // Global variable for Hue (for functions that use this)
    byte cycleHue = 0;

    void setup()
    {
      FastLED.addLeds<WS2811, PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
    }
    void loop() {
      FastLED.setBrightness(50);
      //myLights();
      slantBars();
       
      // AllRunningLights();
      // FastLED.setBrightness(50);
      // FlashLights();  
    }
    void slantBars() {
      static byte slantPos = 0;
      for (byte x = 0; x < kMatrixWidth; x++) {
        for (byte y = 0; y < kMatrixHeight; y++) {
          leds[XY(x, y)] = CHSV(cycleHue, 255, quadwave8(x * 32 + y * 32 + slantPos));
        }
      }
      slantPos -= 4;
    }
    void RunningLights(byte red, byte green, byte blue, int WaveDelay) {
      int Position = 0;
      int myCounter = 0;
        
      for(int i=0; i<NUM_LEDS*3; i++)
      {
        if(myCounter<6) {
          Position++; // = 0; //Position + Rate;
          for(int i=0; i<NUM_LEDS; i++) {
            // sine wave, 3 offset waves make a rainbow!
            //float level = sin(i+Position) * 127 + 128;
            leds[i] = CRGB( 127, 127, 127);
            float level = sin(i+Position) * 127 + 128;
            leds[i] = CRGB( ((sin(i+Position) * 127 + 128)/255)*red,
                            ((sin(i+Position) * 127 + 128)/255)*green,
                            ((sin(i+Position) * 127 + 128)/255)*blue );
          }
          
          FastLED.show();
          myCounter++;
        } 
        else { 
          return; 
        }
      }
    }
    void AllRunningLights() {
       for(int myCounter = 0; myCounter<5; myCounter++) {
          RunningLights(0x00,0x80,0xff, 10);
          RunningLights(0xEE,0x00,0xEE, 10);
          RunningLights(0xff,0xff,0xff, 10);
          RunningLights(0xff,0x70,0x00, 10);
          RunningLights(0xff,0x10,0x10, 10);
          RunningLights(0x26,0xff,0x00, 10);
       } 
       return;
    }
    void myLights() {
       fill_solid( leds, NUM_LEDS, CRGB( 0, 128, 255) ); //1
       FastLED.show();
    }

    void FlashLights() {
       for(int myCounter=0; myCounter<10; myCounter++) {
      
         fill_solid( leds, NUM_LEDS, CRGB( 0, 128, 255) ); //1
         FastLED.show();
         delay(100);
         fill_solid( leds, NUM_LEDS, CRGB( 0, 0, 0) );
         FastLED.show();
         delay(40);
         fill_solid( leds, NUM_LEDS, CRGB( 238, 0, 238) ); //2
         FastLED.show();
         delay(100);
         fill_solid( leds, NUM_LEDS, CRGB( 0, 0, 0) );
         FastLED.show();
         delay(40);
         fill_solid( leds, NUM_LEDS, CRGB( 255, 255, 255) ); //3
         FastLED.show();
         delay(100);
         fill_solid( leds, NUM_LEDS, CRGB( 0, 0, 0) );
         FastLED.show();
         delay(40);
         fill_solid( leds, NUM_LEDS, CRGB( 255, 112, 0) ); //4
         FastLED.show();
         delay(100);
         fill_solid( leds, NUM_LEDS, CRGB( 0, 0, 0) );
         FastLED.show();
         delay(40);
         fill_solid( leds, NUM_LEDS, CRGB( 255, 16, 16) ); //5
         FastLED.show();
         delay(100);
         fill_solid( leds, NUM_LEDS, CRGB( 0, 0, 0) );
         FastLED.show();
         delay(40);
         fill_solid( leds, NUM_LEDS, CRGB( 38, 255, 0) ); //6
         FastLED.show();
         delay(100);
         fill_solid( leds, NUM_LEDS, CRGB( 0, 0, 0) );
         FastLED.show();
         delay(40);
       } 
       
       return;   
    }
    // Convert X,Y to actual LED location
    uint16_t XY (uint16_t x, uint16_t y) {
      if ( (x >= kMatrixWidth) || (y >= kMatrixHeight) ) {
        return (NUM_LEDS);
      }
      return (y*kMatrixWidth)+x-1;
    }
    // Increment the global hue value for functions that use it
    void hueCycle(byte incr) {
        cycleHue+=incr;
    }

    I’ve attached the INO file as well, and hope this is what you were looking for.
    And I hope I didn’t ruin the code 

    Let me know if it works as before (or not) and feel free to ask questions if you have any.

    Attachments:
    12340

    ronnieblaze
    Participant

    Hi Hans,

    Thank you for looking at the code and cleaning it up.  I am having a problem getting it to run but I am starting to think that could be because I am currently using a Wemos D1 R2 mini pro and not a Arduino uno/mega/ETC.

    In my continued search for trying to find the original code that i ran on a uno,  I do believe i finally found it (attached). But i do not have my uno to test it..  It is with a friend that is doing all the led strip soldering for me, so he can test the strips as he goes.

    The code is based on the Running Lights effect, So I am really not sure why it does not want to work.

    Attachments:
    12343

    hans
    Keymaster

    Hi RonnieBlaze,

    Can you tell me what is happening when you run the code? (in comparison with your code that you posted initially)
    Do you see error messages? Or very odd behavior?

    I’d recommend testing with an Arduino Uno first – so you get more familiar with the Arduino’s. Then later when you migrate to another board, at least you can see that the code is supposed to work and that code made need some adaptation for that specific board.

    As I do not have a Wemos available, I wouldn’t know what kind of issue you may or may not run into.
    When looking at some bare minimum info I found online; there may be an issue with the PIN layout (compared to the Uno, you may have to choose different pins) and the available voltage on the board (3.3V vs 5V – your LED strip most likely wants 5V, where as your board may only provide 3.3V for the LED strip data pin).

    12345

    ronnieblaze
    Participant

    Hans,

    Visually it looks to be running faster.  The leds that are moving seem to be going 2 or 3 times faster they are almost a blur when moving. And I agree i need to wait until i get my uno back from my friend so i can flash the same code to both controllers and look at the difference.

    The main look i would like to achieve is similar to Theatre Chase (https://www.tweaking4all.com/hardware/arduino/adruino-led-strip-effects/#LEDStripEffectTheatreChase) but with 4 leds on the middle 2 being brighter then the ones on the end, and a 4 led spacing to the next group (example below), having them scroll for a bit then have a random color change.  I will work with your Theatre Chase sample code and see what if anything i can get working.

    xXXx—-xXXx—-xXXx—-xXXx—-xXXx

    12347

    hans
    Keymaster

    Ah OK, well, running too fast shouldn’t be a problem (and I did optimize a few things, so that could be why).
    You could try adding a short delay after each FastLED.show();. something like this:

    FastLED.show();
    delay(20);

    Assuming you run the code that I modified.

Viewing 7 posts - 1 through 7 (of 7 total)



You must be logged in to reply to this topic.