s-marley/FastLED-basics

Button and timer mixed

landlord101 opened this issue · 0 comments

#include <FastLED.h>
#include <OneButton.h>

#define NUM_LEDS 10
#define LED_PIN 7
#define BTN_PIN 6

CRGB leds[NUM_LEDS];

uint8_t patternCounter = 0;

// Push button connected between pin 7 and GND (no resistor required)
OneButton btn = OneButton(BTN_PIN, true, true);

void setup() {
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(255);
Serial.begin(57600);

btn.attachClick(nextPattern);
}

void loop() {

switch (patternCounter) {
case 0:
movingDots();
break;
case 1:
rainbowBeat();
break;
case 2:
redWhiteBlue();
break;
}

EVERY_N_SECONDS(10)
{
nextPattern();
}

FastLED.show();
btn.tick();
}

void nextPattern() {
patternCounter = (patternCounter + 1) % 3; // Change the number after the % to the number of patterns you have
}

//------- Put your patterns below -------//

void movingDots() {

uint16_t posBeat = beatsin16(30, 0, NUM_LEDS - 1, 0, 0);
uint16_t posBeat2 = beatsin16(60, 0, NUM_LEDS - 1, 0, 0);

uint16_t posBeat3 = beatsin16(30, 0, NUM_LEDS - 1, 0, 32767);
uint16_t posBeat4 = beatsin16(60, 0, NUM_LEDS - 1, 0, 32767);

// Wave for LED color
uint8_t colBeat = beatsin8(45, 0, 255, 0, 0);

leds[(posBeat + posBeat2) / 2] = CHSV(colBeat, 255, 255);
leds[(posBeat3 + posBeat4) / 2] = CHSV(colBeat, 255, 255);

fadeToBlackBy(leds, NUM_LEDS, 10);
}

void rainbowBeat() {

uint16_t beatA = beatsin16(30, 0, 255);
uint16_t beatB = beatsin16(20, 0, 255);
fill_rainbow(leds, NUM_LEDS, (beatA + beatB) / 2, 8);
}

void redWhiteBlue() {

uint16_t sinBeat = beatsin16(30, 0, NUM_LEDS - 1, 0, 0);
uint16_t sinBeat2 = beatsin16(30, 0, NUM_LEDS - 1, 0, 21845);
uint16_t sinBeat3 = beatsin16(30, 0, NUM_LEDS - 1, 0, 43690);

leds[sinBeat] = CRGB::Blue;
leds[sinBeat2] = CRGB::Red;
leds[sinBeat3] = CRGB::White;

fadeToBlackBy(leds, NUM_LEDS, 10);
}

This is code for mixing timing change and button press. Change the time or add a physical 2 state switch for checking on-off.

Sorry, I don't know how to use git.