// Pin assign
// arduino PIN 0,button = Attiny PIN 5
// arduino PIN 4,RGB LED = ATTiny PIN 3
#include <EEPROM.h>
#include <FastLED.h>
//LEDの数
#define NUM_LEDS 80
//LED信号ピン
#define DATA_PIN 3
//ボタンのピン
#define myButton 0
CRGB leds[NUM_LEDS];
CRGB color = CRGB::Black;
int currentColor = 0;
int currentMode = 0;
int prevColor = -1;
unsigned long prevTime = 0;
int flgTimer = 1;
unsigned long btnTimer;
const unsigned long shortPress = 30;
const unsigned long longPress = 800;
void parade() {
unsigned long time = millis() / 100;
if (time != prevTime) {
for (int i = 0; i < NUM_LEDS; i++) {
if((time % 2) == 0){
if((i % 2) == 0){
leds[i] = color;
}else{
leds[i] = CRGB::Black;
}
}else{
if((i % 2) == 0){
leds[i] = CRGB::Black;
}else{
leds[i] = color;
}
}
}
FastLED.show();
prevTime = time;
}
}
void LarsonScanner() {
int ledsCluster = 8;
unsigned long time = millis() / 100;
if (time != prevTime) {
for (int i = 0; i < NUM_LEDS; i++) {
int mod = time % (ledsCluster * 2);
int modLed = (mod < ledsCluster) ? mod : ledsCluster * 2 - mod - 1;
if (i % ledsCluster == modLed) {
leds[i] = color;
} else {
switch (currentColor) {
case 0: leds[i] = CRGB::DarkRed; break;
case 1: leds[i] = CRGB::DarkGreen; break;
case 2: leds[i] = CRGB::DarkBlue; break;
case 3: leds[i] = CRGB::Gray; break;
}
}
}
FastLED.show();
prevTime = time;
}
}
void normal() {
if (currentColor != prevColor) {
for (int i = 0; i < NUM_LEDS; i++) {
// leds[i].setRGB(rgbR, rgbG, rgbB);
leds[i] = color;;
}
FastLED.show();
prevColor = currentColor;
}
}
void setup() {
currentColor = EEPROM.read(0);
if (currentColor < 0 || 3 < currentColor) currentColor = 0;
currentMode = EEPROM.read(1);
if (currentMode < 0 || 2 < currentMode) currentMode = 0;
pinMode(myButton, INPUT_PULLUP); // button
///////LED
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS); // GRB ordering
//FastLED.setBrightness(255);
}
void loop() {
if (digitalRead(myButton) == LOW) {
if (flgTimer) {
btnTimer = millis();
flgTimer = 0;
}
} else {
if (!flgTimer) {
unsigned long diffTime = millis() - btnTimer;
if (diffTime > longPress) { // Long pressed
currentMode++;
if (currentMode > 2) currentMode = 0;
EEPROM.write(1, currentMode);
} else if (diffTime > shortPress) {
currentColor++;
if (currentColor > 3) currentColor = 0;
EEPROM.write(0, currentColor);
}
flgTimer = 1;
}
}
switch (currentColor) {
case 0: color = CRGB::Red; break;
case 1: color = CRGB::Green; break;
case 2: color = CRGB::Blue; break;
case 3: color = CRGB::White; break;
}
switch (currentMode) {
case 0: normal(); break;
case 1: parade(); break;
case 2: LarsonScanner(); break;
}
}