// start of macros dbg and dbgi
#define dbg(myFixedText, variableName) \
Serial.print( F(#myFixedText " " #variableName"=") ); \
Serial.println(variableName);
// usage: dbg("1:my fixed text",myVariable);
// myVariable can be any variable or expression that is defined in scope
#define dbgi(myFixedText, variableName,timeInterval) \
do { \
static unsigned long intervalStartTime; \
if ( millis() - intervalStartTime >= timeInterval ){ \
intervalStartTime = millis(); \
Serial.print( F(#myFixedText " " #variableName"=") ); \
Serial.println(variableName); \
} \
} while (false);
// usage: dbgi("2:my fixed text",myVariable,1000);
// myVariable can be any variable or expression that is defined in scope
// third parameter is the time in milliseconds that must pass by until the next time a
// Serial.print is executed
// end of macros dbg and dbgi
#include <FastLED.h>
unsigned long interval_ms = 5000;
unsigned long lastExecutedMillis = 0; // vairable to save the last executed time
#define LED_PIN1 2
#define LED_PIN2 3
#define LED_PIN3 4
#define LED_PIN4 5
#define NUM_LEDS 25
CRGB leds[NUM_LEDS];
void setup() {
FastLED.addLeds<WS2812B, LED_PIN1, GRB>(leds, NUM_LEDS);
FastLED.addLeds<WS2812B, LED_PIN2, GRB>(leds, NUM_LEDS);
FastLED.addLeds<WS2812B, LED_PIN3, GRB>(leds, NUM_LEDS);
FastLED.addLeds<WS2812B, LED_PIN4, GRB>(leds, NUM_LEDS);
Serial.begin(115200);
for (int i = 0; i < NUM_LEDS ; i++) {
leds[i] = CRGB(0, 0, 0);
FastLED.show();
}
}
void loop() {
static int state = 1;
static int last_val = 0;
int voltage = analogRead(A0);
dbgi("A0",voltage,1000);
int value = map(voltage, 140, 420, 1, NUM_LEDS);
dbgi("mapped",value,1000);
switch (state) {
case 0:
measure_mode(value);
if (voltage > 160) {
lastExecutedMillis = millis(); // save the last executed time
dbgi("measure_mode voltage > 160", voltage,500);
}
dbgi("measure_mode",millis() - lastExecutedMillis,500);
if (millis() - lastExecutedMillis >= 5000) {
dbgi("measure_mode millis() - lastExecutedMillis >= 5000",state,500)
state = 1;
//Serial.println("there");
}
break;
case 1:
dbgi("entering standby-mode",0,500);
standby_mode();
dbgi("leaving standby-mode",1,500);
dbgi("",0,1000);
dbgi("",0,1000);
if (voltage > 160) {
dbgi("standby_mode voltage > 160", voltage,500);
state = 0;
}
break;
}
}
void measure_mode(int value) {
if (value >= 0 && value < NUM_LEDS) {
//lastExecutedMillis = millis(); // save the last executed time
for (int i = 0; i < value ; i++) {
leds[i] = CRGB(204, 230, 255);
FastLED.show();
}
for (int i = NUM_LEDS; i > value + 1 ; i--) {
leds[i] = CRGB(242, 107, 107);
FastLED.show();
}
}
else {
if (value < 0) {
for (int i = NUM_LEDS; i > 0 ; i--) {
leds[i] = CRGB(242, 107, 107);
FastLED.show();
}
}
else if (value > NUM_LEDS) {
for (int i = 0; i < NUM_LEDS ; i++) {
leds[i] = CRGB(204, 230, 255);
FastLED.show();
}
}
}
}
void standby_mode() {
static int i = 0;
static int dir = 0;
static int color = 0;
if (i < 244 && dir == 0) {
for (int j = 0; j < NUM_LEDS; j++) {
if (color == 0) {
leds[j] = CHSV(201, 94, i);
}
else if (color == 1) {
leds[j] = CHSV(0, 100, i);
}
}
i++;
if (i == 244) {
dir = 1;
i = 0;
}
FastLED.show();
}
else if (i < 244 && dir == 1) {
for (int j = 0; j < NUM_LEDS; j++) {
if (color == 0) {
leds[j] = CHSV(201, 94, (244 - i));
}
else if (color == 1) {
leds[j] = CHSV(0, 100, (244 - i));
}
}
i++;
if (i == 244) {
dir = 0;
i = 0;
if (color == 0) {
color = 1;
}
else if (color == 1) {
color = 0;
}
}
FastLED.show();
}
}