Hello,
I am making a program where LED strips need to operate in 2 modes.
First, measures some values from the analog pin and depending on how high the value is, that's how much LEDs change color.
The other one is just fading lights in 2 colors.
The two functions work separately but from the first function to switch to the nect function, the value of the analog pin shouldn't exceed 160 for 5s. I am trying to achieve this with millis() bur for some reason it doesn't work.
It eighter changes to the other function immediately or it doesn't change at all.
Any help on that can be the problem is useful.
#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);
Serial.println(voltage);
int value = map(voltage, 140, 420, 1, NUM_LEDS);
switch(state){
case 0:
measure_mode(value);
if(voltage > 160){
lastExecutedMillis = millis(); // save the last executed time
Serial.println("here");
}
if (millis() - lastExecutedMillis >= 5000) {
state = 1;
//Serial.println("there");
}
break;
case 1:
standby_mode();
if(voltage > 160){
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();
}
}