Setting FastLed Lights to work off of timed button

Hi I am trying to get the colors of ws28128b led strip to change color based on how long I hold a button. I am using an Elegoo Uno R3 as the board. I can get the first color to work and turn off but the second color does nothing. When I verify the code I have no errors.

This is the code I have.

float	pressLength_milliSeconds	=	0;

int	optionOne_milliSeconds	=	100;
int	optionTwo_milliSeconds	=	500;	
int	optionThree_milliSeconds	=	1000;							

int	buttonPin	=	3;


#include <FastLED.h>

#define NUM_LEDS_PER_STRIP 180

CRGB leds[NUM_LEDS_PER_STRIP];

void	setup(){

		pinMode(buttonPin,	INPUT_PULLUP);					

    FastLED.addLeds<NEOPIXEL, 2>(leds, NUM_LEDS_PER_STRIP);


		Serial.begin(9600);																																					
}	//	close	setup
void	loop()	{

		while	(digitalRead(buttonPin)	==	LOW	){	
				delay(100);	
				pressLength_milliSeconds	=	pressLength_milliSeconds	+	100;

				Serial.print("ms	=	");
				Serial.println(pressLength_milliSeconds);
		}
		
    if	(pressLength_milliSeconds	>=	optionTwo_milliSeconds){
				for(int i = 0; i < NUM_LEDS_PER_STRIP; i++) {
    leds[i] = CRGB::Black;
    FastLED.show();
    leds[i] = CRGB::Black;
    delay(1);
        			
		}
    }


		else	if(pressLength_milliSeconds	>=	optionOne_milliSeconds){
      for(int i = NUM_LEDS_PER_STRIP-1; i >= 0; i--) {

    leds[i] = CRGB(0, 0, 225);;
    FastLED.show();

    leds[i] = CRGB(0, 0, 225);;
    delay(1);
          }

    }


		else	if(pressLength_milliSeconds >=	optionThree_milliSeconds){
      for(int i = NUM_LEDS_PER_STRIP-1; i >= 0; i--) {

    leds[i] = CRGB(0, 225, 0);;
    FastLED.show();
    leds[i] = CRGB(0, 225, 0);;
    delay(1);
          }
				
        

		}
    

		pressLength_milliSeconds	=	0;
}

How could you tell anything about what that code was supposed to do with the indentation all over the place like that?

Try checking for the longest press length first.

Then the next longest.

Then the shortest.

Rather than checking for the second longest first, then the shortest, and only then checking for the longest. If pressLength is > 1000 but you check for > 500 first, you'll never get to the else if > 1000 part.

1 Like

Thank You! It works now. I fixed the indentations it helped a lot.

Please post your prett(ier) code that works now.

a7

Code tags are three back ticks (```) on their own line (before and after the code). You did not do that for the opening code tag which was interpreted as a code formatting instruction. As a result float pressLength_milliSeconds = 0; did not show.

I've fixed it for you.

Two common uses of opening code tag with optional formatting
```cpp for cpp files; it's the default behaviour.
```text for e.g. output of the serial monitor or error messages.

Here is the fixed code:

float	pressLength_milliSeconds	=	0;

int optionFour_milliSeconds  = 1000;
int	optionThree_milliSeconds	=	1500;	
int	optionOne_milliSeconds	=	100;
int	optionTwo_milliSeconds	=	500;	
int T = 20;						

int	buttonPin	=	3;


#include <FastLED.h>
#include "DHT.h"

#define NUM_LEDS_PER_STRIP 150
#define DHT11_PIN 4

CRGB leds[NUM_LEDS_PER_STRIP];
DHT dht11(DHT11_PIN, DHT11);

void	setup(){

delay(3000);

pinMode(buttonPin,	INPUT_PULLUP);					

FastLED.addLeds<NEOPIXEL, 2>(leds, NUM_LEDS_PER_STRIP);

dht11.begin(); // initialize the sensor

Serial.begin(9600);		

}	//	close	setup


void	loop()	{

		while	(digitalRead(buttonPin)	==	LOW	){	
				delay(100);	
				pressLength_milliSeconds	=	pressLength_milliSeconds	+	100;

				Serial.print("ms	=	");
				Serial.println(pressLength_milliSeconds);
		}
		
   if(pressLength_milliSeconds >=	optionThree_milliSeconds){
      for(int i = NUM_LEDS_PER_STRIP-1; i >= 0; i--) {
       leds[i] = CRGB(0, 0, 0);;
       FastLED.show();
       leds[i] = CRGB(0, 0, 0);;
       delay(T);
          }
		}

    else if(pressLength_milliSeconds >=	optionFour_milliSeconds){
      for(int i = NUM_LEDS_PER_STRIP-1; i >= 0; i--) {
       leds[i] = CRGB(255, 0, 0);;
       FastLED.show();
       leds[i] = CRGB(255, 0, 0);;
       delay(T);
          }
		}

		else	if(pressLength_milliSeconds	>=	optionTwo_milliSeconds){
      for(int i = NUM_LEDS_PER_STRIP-1; i >= 0; i--) {
        leds[i] = CRGB(0, 0, 225);;
        FastLED.show();
        leds[i] = CRGB(0, 0, 225);;
        delay(T);
          }

    }


		else	if(pressLength_milliSeconds >=	optionOne_milliSeconds){
      for(int i = NUM_LEDS_PER_STRIP-1; i >= 0; i--) {
       leds[i] = CRGB(0, 225, 0);;
       FastLED.show();
       leds[i] = CRGB(0, 225, 0);;
       delay(T);
          }
				
        

		}
   

  // read humidity
  float humi  = dht11.readHumidity();
  // read temperature as Celsius
  float tempC = dht11.readTemperature();
  // read temperature as Fahrenheit
  float tempF = dht11.readTemperature(true);

  // check if any reads failed
  if (isnan(humi) || isnan(tempC) || isnan(tempF)) {
    Serial.println("Failed to read from DHT11 sensor!");
  } else {
    Serial.print("DHT11# Humidity: ");
    Serial.print(humi);
    Serial.print("%");

    Serial.print("  |  "); 

    Serial.print("Temperature: ");
    Serial.print(tempC);
    Serial.print("°C ~ ");
    Serial.print(tempF);
    Serial.println("°F");
  }
    

	pressLength_milliSeconds	=	0;
}


You don't need to set the same led to the same value twice - once is enough.
You also don't need double semi-colons at the ends of many of your lines.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.