Skip to content

Instantly share code, notes, and snippets.

@marmilicious
Last active February 20, 2019 08:29
Show Gist options
  • Save marmilicious/1370dfca4788643cb019e5a07ad43267 to your computer and use it in GitHub Desktop.
Save marmilicious/1370dfca4788643cb019e5a07ad43267 to your computer and use it in GitHub Desktop.
Originally from this post:
https://plus.google.com/100542328320334100166/posts/Y5BdWFEvfo1
Copied here since G+ is going away.
-----------------------------------------------------------------------
When using fill_rainbow...
To make the rainbow flow go the other way you can multiply the millis bit by -1:
change this:
fill_rainbow( leds[x], stripLength, millis()/2, 256/stripLength );
to this:
fill_rainbow( leds[x], stripLength, -1*millis()/2, 256/stripLength );
-----------------------------------------------------------------------
And to explain /why/ the rainbow is moving...
The rainbow function can be fed an initial hue value.
fill_rainbow( leds, NUM_LEDS, initialhue);
If this initial hue value is different each time the strip is updated then the rainbow will
"flow" down the strip. In your code it is being driven by the ever increasing millis time value.
fill_rainbow( leds, NUM_LEDS, millis() ); //initialhue changes over time
This can be slowed down by dividing millis, thus making the initial hue change smaller each loop.
fill_rainbow( leds, NUM_LEDS, millis()/10 ); //slower initialhue change over time
The initial hue could also be based on your own variable that is incremented or varied in some fashion:
fill_rainbow( leds, NUM_LEDS, rainbowStartColor );
rainbowStartColor = rainbowStartColor + 7;
Making this initial hue value negative will change the direction the rainbow flows.
In addition to initialhue, the fill_rainbow function can also be fed a forth variable, deltahue.
This is how much the color changes from pixel to pixel. It defaults to 5, but can be set to
whatever you want.
fill_rainbow( leds, NUM_LEDS, initialhue, deltahue);
So, if you want the complete rainbow, from red (value 0) all the way through the color spectrum
and back to red (value 255), over a specific number of pixels it would need to be calculated. Thus:
fill_rainbow( leds, NUM_LEDS, initialhue, 256/NUM_LEDS ); //full rainbow spread over entire strip.
If you just wanted a quarter of the rainbow to show up (and have it flow over time) you could use
something like this:
fill_rainbow( leds, NUM_LEDS, millis()/10, 256/NUM_LEDS/4); //displays 1/4 of rainbow
Or you could have it display the rainbow twice within a certain length by doing:
fill_rainbow( leds, NUM_LEDS, millis()/10, 256/NUM_LEDS*2); //displays two rainbows
Have fun!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment