// Source: https://github.com/s-marley/FastLED-basics
#include <FastLED.h>
#define NUM_LEDS 180
#define LED_PIN 2
#define BTN_PIN 4
#define DELAY_TIME 5
const int SHORT_PRESS_TIME = 1000; // 500 milliseconds
const int LONG_PRESS_TIME = 5000; // 1000 milliseconds
int lastState = LOW; // the previous state from the input pin
int currentState; // the current reading from the input pin
unsigned long pressedTime = 0;
unsigned long releasedTime = 0;
bool state;
CRGB leds[NUM_LEDS];
void setup() {
Serial.begin(9600);
pinMode(BTN_PIN, INPUT_PULLUP);
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(50);
}
void on_right () {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::White;
FastLED.show();
delay(DELAY_TIME);
}
}
void off_right() {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::Black;
FastLED.show();
delay(DELAY_TIME);
}
}
void on_left () {
for (int i = NUM_LEDS; i >= 0; i--) {
leds[i] = CRGB::White;
FastLED.show();
delay(DELAY_TIME);
}
}
void off_left() {
for (int i = NUM_LEDS; i >= 0; i--) {
leds[i] = CRGB::Black;
FastLED.show();
delay(DELAY_TIME);
}
}
void main_loop() {
if (state == false) {
on_right();
state = true;
} else {
off_right();
state = false;
}
}
void main_loop_back() {
if (state == false) {
on_left();
state = true;
} else {
off_left();
state = false;
}
}
void loop() {
currentState = digitalRead(BTN_PIN);
if (lastState == HIGH && currentState == LOW) // button is pressed
pressedTime = millis();
else if (lastState == LOW && currentState == HIGH) { // button is released
releasedTime = millis();
long pressDuration = releasedTime - pressedTime;
Serial.print("pressed: ");
Serial.print(pressedTime);
Serial.print(", released: ");
Serial.print(releasedTime);
Serial.print(", duration: ");
Serial.println(pressDuration);
if (pressDuration > 10 && pressDuration < SHORT_PRESS_TIME && pressDuration > 0) {
Serial.println("A short press is detected");
main_loop();
}
if ( pressDuration > LONG_PRESS_TIME && pressDuration > 0) {
Serial.println("A long press is detected, go to setup menu.");
setup_menu();
}
}
if (millis() > (pressedTime + LONG_PRESS_TIME) && currentState == LOW)
blink_when_ready_for_setup_menu();
// save the the last state
lastState = currentState;
}
void blink_when_ready_for_setup_menu() {
Serial.println("Blink!");
for (int i = 0; i < 10; i++) {
leds[i] = CRGB::White;
FastLED.show();
}
for (int i = 0; i < 10; i++) {
leds[i] = CRGB::Black;
FastLED.show();
}
// Serial.println("Wychodzimy z trybu serwisowego");
}
void setup_menu() {
Serial.println("Wchodzimy w tryb serwisowy");
for (int j = 0; j < 10; j++) {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::White;
}
FastLED.show();
delay(50);
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::Black;
}
FastLED.show();
delay(50);
}
while (ButtonPre)
Serial.println("Wychodzimy z trybu serwisowego");
}