Skip to content

Instantly share code, notes, and snippets.

@atuline
Last active April 24, 2018 08:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atuline/b600638a418d46470de0984c4909a0bd to your computer and use it in GitHub Desktop.
Save atuline/b600638a418d46470de0984c4909a0bd to your computer and use it in GitHub Desktop.
Time_Sawtooth
/* Time_Sawtooth
*
* By: Andrew Tuline
*
* Date: February, 2018
*
* Creating a sawtooth pattern using millis and bpm.
*
*
*/
#include "FastLED.h" // FastLED library.
#if FASTLED_VERSION < 3001000
#error "Requires FastLED 3.1 or later; check github for latest code."
#endif
// Fixed definitions cannot change on the fly.
#define LED_DT 12 // Data pin to connect to the strip.
#define LED_CK 11 // Clock pin for WS2801 or APA102.
#define COLOR_ORDER BGR // It's GRB for WS2812 and BGR for APA102.
#define LED_TYPE APA102 // Using APA102, WS2812, WS2801. Don't forget to modify LEDS.addLeds to suit.
#define NUM_LEDS 60 // Number of LED's.
// Global variables can be changed on the fly.
uint8_t max_bright = 128; // Overall brightness.
struct CRGB leds[NUM_LEDS]; // Initialize our LED array.
// Palette definitions
CRGBPalette16 currentPalette = PartyColors_p;
CRGBPalette16 targetPalette = PartyColors_p;
TBlendType currentBlending = LINEARBLEND; // NOBLEND or LINEARBLEND
void setup() {
Serial.begin(57600); // Initialize serial port for debugging.
delay(1000); // Soft startup to ease the flow of electrons.
LEDS.addLeds<LED_TYPE, LED_DT, LED_CK, COLOR_ORDER>(leds, NUM_LEDS); // Use this for WS2801 or APA102
// LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS); // Use this for WS2812
FastLED.setBrightness(max_bright);
set_max_power_in_volts_and_milliamps(5, 500); // FastLED Power management set at 5V, 500mA.
} // setup()
void loop () {
sawtooth();
FastLED.show();
} // loop()
void sawtooth() {
int bpm = 60;
int ms_per_beat = 60000/bpm; // 500ms per beat
int ms_per_led = 60000/bpm/NUM_LEDS;
int cur_led = ((millis() % ms_per_beat) / ms_per_led) % (NUM_LEDS);
if (cur_led == 0)
fill_solid(leds, NUM_LEDS, CRGB::Black);
else
leds[cur_led] = ColorFromPalette(currentPalette, 0, 255, currentBlending);
} // sawtooth()
@marcopolo303
Copy link

I have a crude version but needed 4 waves to make a whirlwind effect getting faster and faster on 300 leds. Learned much off the 100 reel demo you and Kriegsman put together ! Thought you might like the variable fade as it gets faster. Second is a non blocking heart beat idea it has a couple of wobbles I need to fix but interesting using phase and the two different waves to create the double beat.
The project is a box that operates in time sequence with the teensy audio, next step getting a time trigger to operate via Xbee to control this and the teensy audio board to have a fully synchronised float.
Hope of interest seeing what a pupil can do!!
All good fun
Mark

void whirlwind_beat1 () { // Uses Beat this is not the Beat1 of Beat 2 read from pot
uint16_t posshow1;
uint16_t posshow2 ;
uint16_t posshow3 ;
uint16_t posshow4 ;

uint16_t pos = scale16( beat16(Beat),NUMLEDS1); //dot at beat BPM scaled to led

posshow1 = (pos);
posshow2 = (posshow1 + (NUMLEDS1/4)) % NUMLEDS1;
posshow3 = (posshow1 + (NUMLEDS1/2)) % NUMLEDS1;
posshow4 = (posshow1 + (3*NUMLEDS1/4)) % NUMLEDS1;

Leds1[pos] += CHSV(Hue , Sat, Bri);
Leds1[posshow2] += CHSV(Hue , Sat, Bri);
Leds1[posshow3] += CHSV(Hue , Sat, Bri);
Leds1[posshow4] += CHSV(Hue , Sat, Bri);

fadeToBlackBy( Leds1, NUMLEDS1, 22*log(Beat));
if (sinceWhirlwindspeedup >= 3000){ // each pass change colour and speed up
Beat = constrain((Beat + 4) % (60),10,60); // rolls round end
Hue = (Hue + random16(huediff)/4);
sinceWhirlwindspeedup = 0;

} 

} // whirlwind_beat()

The heartbeat attempt
void heart_beat() {
uint16_t pos1 = beatsin16(22,1,NUMLEDS1,0,3000);
uint16_t pos2 = (NUMLEDS1 - pos1 +1) % (0);
uint16_t pos3 = scale16(beat16(22),NUMLEDS1);
uint16_t pos4 = (NUMLEDS1 - pos3 - 1 ) % (0);
fill_gradient_RGB (Leds1, pos1,CHSV(0 , 220, 180) ,pos2 ,CHSV(0 , 220, 180));
fill_gradient_RGB (Leds1, pos3,CRGB::Red,pos4 ,CRGB::Red);
fadeToBlackBy(Leds1, NUMLEDS1, 50);
} // heart_beat()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment