Looking for help with code for WS2812B led strip

Hello, I'm trying to define the 55 leds into 3 different colors from one potentiometer. would like to get white, green, red at different ranges from the pot (white at 0-70, green 70-800, red 800-1023)

//trying to define the 55 leds into 3 different colors from one potentiometer. 
// trying to get white, green, red at different ranges from the pot (white at 0-70, green 70-800, red //800-1023)

#include "FastLED.h"

#define PIN_SLIDE_POT_A A0 //  pot
#define MAX_SLIDE_POT_ANALGOG_READ_VALUE 1023 // (5volt)

#define NUM_LEDS 55 
//#define PIN_LED 3 

//#define DATA_PIN 3
#define LED_TYPE WS2812B


#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];


#define MAX_SLIDE_VALUE 1024
#define MAX_MAP_OUTPUT 110
#define LED_PIN     3

// doesnt work , max_map_output used instead ---> #define BRIGHTNESS 50



CRGB rgb_led[NUM_LEDS]; // array

void setup() {
  delay(3000); 
  Serial.begin(9600);
  
//maybe add
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);


  pinMode(PIN_SLIDE_POT_A, INPUT);
  FastLED.addLeds<WS2812B, LED_PIN>(rgb_led, NUM_LEDS);  
  
  
}

void loop() {
  // Read analog pot
  int value_slide_pot_a = analogRead(PIN_SLIDE_POT_A);
  Serial.print("Slide Pot value: ");
  Serial.println(value_slide_pot_a);

  //maybe try
  int value_slide_a = analogRead(PIN_SLIDE_POT_A);
  int SLIDER_VALUE = map(value_slide_a, 0, MAX_SLIDE_VALUE, 0, MAX_MAP_OUTPUT);



  //  pot to range leds
  int num_leds_switchedon = map(value_slide_pot_a, 0, MAX_SLIDE_POT_ANALGOG_READ_VALUE, 0, NUM_LEDS);  


 
  // leds lower range pot
  for (int i = 0; i < num_leds_switchedon; ++i) {
    rgb_led[i] = CHSV(320,255,SLIDER_VALUE);
   //leds[i] = CHSV(180,255,SLIDER_VALUE);
  }  
///try three colors

  


  // LEDs color from high range pot
  for (int i = num_leds_switchedon; i < NUM_LEDS; ++i) {
   rgb_led[i] = CHSV(350,255,SLIDER_VALUE);
   //leds[i] = CHSV(50,255,SLIDER_VALUE);
  }
  FastLED.show();
}

Why do you read the pot twice at the beginning of loop without doing anything with the value and why do you change the variable name.

Why do you describe what you want but then not use those ranges in your code?

Hey thanks for responding. yeah, i forgot to exclude that , but it still works but not how I want, or described.

I could not find how to code for the ranges I need/described. looking for tips to help me with that.

Well what does your serial print show you have stored in the variable?

slide pot value" anywhere from 0 - 1023 for the potentiometer

So add some if else statements for the ranges using the >< operators. Start with 1. Get it to print in range 1 or something similar

If (value<70){
Print whatever;
}

Look up comparison operators and Boolean operators

You will need && for the next statement

ok thanks, new to this again after a while . am I on the right path with the following?

// if the analog value is 0-80, turn on the white led:
  if (analogRead > 80) {
rgb_led[24] = CHSV(1,255,SLIDER_VALUE);
  } else {
    rgb_led[80] = CHSV(220,255,SLIDER_VALUE);
  }

Maybe I am not understanding what you want to do.
CHSV is hue, saturation, brightness. Hue controls the color (0-255).

image

I have a 55 led strip. I'm trying to have the 0-6 leds white color, leds 7-37 green, leds 37 - 55 red. all controlled by one pot's 0 - 1023 value, white would turn on gradually increasing 0-110 , yellow 110-720, and red 720- 1023.

Kind of like a cars RPM guage I suppose.

The colors dont matter for now

Thanks

  • Jane

You would write that like:

// if the analog value is 0-80, turn on the white led:
int potValue  =  analogRead (PIN_SLIDE_POT_A);
 if ( potValue > 0 && potValue < 80)
{
      // turn on the white led
}

I did not map the analog read value. The above is to show method and syntax.

Code to illustrate the method from my last post.

const byte potPin = A0;

void setup()
{
   Serial.begin(115200);
   Serial.println("decode pot value to light 1 of 3 LEDs");
}

void loop()
{
   static unsigned long timer = 0;
   unsigned long interval = 500;
   if (millis() - timer >= interval)  // see blink without delay in IDE examples
   {
      timer = millis();
      int potValue = analogRead(potPin);
      if (potValue >= 0 && potValue <= 340)
      {
         Serial.println("light RED");
      }
      else if (potValue >= 341 && potValue <= 682)
      {
         Serial.println("light BLUE");
      }
      else if (potValue >= 683 && potValue <= 1023)
      {
         Serial.println("light GREEN");
      }
      else
      {
          Serial.println("invalid input");
      }
   }
}

ok thanks, I will try this out

Use the language reference for syntax
analogRead() - Arduino Reference
if - Arduino Reference

You need to put the work in when translating from English to code. The uC is very fussy about syntax. Just look everything up in the reference and then make sure your syntax matches.

You can copy and paste their example syntax and then comment it out in the code so that you have it right there to copy from.

A lot of help in the IDE examples, too.

The analogRead() function

Blink without delay

The if structure.

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