Starting led strips from middle with potentiometer

Dear all,
for a small project i am building a led strip that has to be controlled from the middle with a potmeter.

i have a code that starts from led 1.
now i can make 2 strips and let them start from the middle,but i thought it was easier to program it.
i tried several steps,but i can't get it to work.
maybe some of you guys can give me the smoking gun?

anyway,here is the program i tried to modify but failed to do it.
Thanks in advance:D

#include "FastLED.h"

#define PIN_SLIDE_POT_A A0 // input pin of the slide pot
#define MAX_SLIDE_POT_ANALGOG_READ_VALUE 1023// maximum voltage as analog-to-digital converted value, depends on the voltage level of the VCC pin. Examples: 5V = 1023; 3.3V ~700

#define NUM_LEDS 200 // add number of LEDs of your RGB LED strip
#define PIN_LED 3 // digital output PIN that is connected to DIN of the RGB LED strip
#define LED_COLOR CRGB::Green // see Pixel reference · FastLED/FastLED Wiki · GitHub for a full list, e.g. CRGB::AliceBlue, CRGB::Amethyst, CRGB::AntiqueWhite...

CRGB rgb_led[NUM_LEDS]; // color array of the LED RGB strip

void setup() {
Serial.begin(9600);

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

Serial.println("Setup done.");
}

void loop() {
// 1) Analog value of slide pot is read
int value_slide_pot_a = analogRead(PIN_SLIDE_POT_A);
Serial.print("Slide Pot value: ");
Serial.println(value_slide_pot_a);

// 2) Analog value is mapped from slide pot range (analog input value) to led range (number of LEDs)
int num_leds_switchedon = map(value_slide_pot_a, 0, MAX_SLIDE_POT_ANALGOG_READ_VALUE, 0, NUM_LEDS);

// 3) Light up the LEDs
// Only LEDs are switched on which correspond to the area left of the slide knob
for (int i =0; i < num_leds_switchedon; ++i )

{
rgb_led[i] = LED_COLOR; }

// LEDs are switched off which correspond to the area right of the slide knob
for (int i = num_leds_switchedon; i < NUM_LEDS; ++i) {
rgb_led[i] = CRGB::Black;
}
FastLED.show();
}

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

In my experience the easiest way to tidy up the code and add the code tags is as follows
Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

If I understand correctly, you have a strip of 200 LEDs numbered from 0 to 199 and you want to program a mirrored effect from the centre out

If so, then use a for loop that runs from 99 to 0 to create the effect in one half of the strip by addressing LED[currentLed] where currentLed is from 99 to 0 and inside the same for loop apply the same output to LED[199 - currentLed ]

I'm not going to try and write your code but standard practice is to work on input (the pot in your case) and output (LEDs in your case) separately before "connecting" them with your code.

I assume you've got the LED under control...

You can address one or more LEDs at a time, so it should be easy to turn-on LED #100 & #101 in the middle, then #99, #100, #101, and #102, etc.

If you make a loop that counts down from 100-1, and up from 101-200 at the same time, that should simplify your code.

Then you'll need to figure-out how to use map() and/or if-statements to control how far you count up and down, depending on the pot.

Thanks for the info.
It is my first time i try to program a code ,i had hoped with a little bit of configuring some lines to get it running.
i could manage to get the line running from led 100 to 200 and back to 100.
i copied those lines and tried some things to make it from 99 to 0 and back.
although that attempt failed.

i tried again to adressing the LED[currentLed] in the program but i kept giving me fault codes.

i think i have to watch a whole lot of youtube,to finally really know how to start from scratch.

Replace currentLed with the current name of the for loop variable

You can run an index i from 0 to NUM_LEDS-1, setting LED[i] to black if its distance to the center exceeds the mapped pot value, or set it to LED_COLOR otherwise

Make a basic FastLED setup with a potentiometer...

#include "FastLED.h"

#define POTENTIOMETER A0
#define NUM_LEDS 20 // change for your project
#define DATA_PIN 3 // data pin

// define color names for easy use
#define RED CRGB::Red
#define GRN CRGB::Green
#define BLU CRGB::Blue
#define WHT CRGB::White
#define BLK CRGB::Black

CRGB pixel[NUM_LEDS]; //

void setup() {
  Serial.begin(115200);
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(pixel, NUM_LEDS); // RGB is another choice
  FastLED.clear(); // pixels off
  FastLED.show(); // display pixels

Color key pixels: first (0), last (NUM_LEDS - 1) and middle (NUM_LEDS / 2).

  pixel[0] = WHT; // first pixel
  pixel[NUM_LEDS - 1] = BLU; // last pixel
  pixel[NUM_LEDS / 2] = RED; // middle pixel
  FastLED.show();
}

void loop() {}

This should produce three colored pixels to give you reference points... then...

Map the potentiometer (with a range of 1024 bits) to "HALF" the NUM_LEDS (200/2 = 100 for your project). You will only control pixels on half the strip. The other half will be mirrored

void loop () {
  int last_pixel = map (analogRead(POTENTIOMETER), 0, 1023, 0, NUM_LEDS / 2);

Make a line drawing to represent the pixels. You have 200. (I have 20 for this example.)

0---------M--------N // pixels: 0, middle, last
          1111111111
01234567890123456789

Make a for() loop to color the pixels from the middle of the LED strip to the mapped potentiometer value...

  for (int i = 0; i < last_pixel; i++) {
    pixel[NUM_LEDS / 2 - i ] = RED; // color pixels before the middle red
    pixel[NUM_LEDS / 2 + i] = GRN; // color pixels after the middle green
    FastLED.show();
  }

Then clear the remaining pixels from the mapped potentiometer value to 0 and NUM_LEDS...

  for (int i = last_pixel; i < NUM_LEDS / 2; i++) {
    pixel[NUM_LEDS / 2 - i ] = BLK; // pixels before the middle
    pixel[NUM_LEDS / 2 + i] = BLK; // pixels after the middle
    FastLED.show();

Try it on WOKWI.COM

diagram.json
{
  "version": 1,
  "author": "Anonymous maker",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-nano", "id": "nano", "top": 4.8, "left": -0.5, "attrs": {} },
    {
      "type": "wokwi-led-ring",
      "id": "ring1",
      "top": -196.22,
      "left": 17.96,
      "attrs": { "pixels": "20" }
    },
    { "type": "wokwi-vcc", "id": "vcc1", "top": -104.84, "left": -19.2, "attrs": {} },
    { "type": "wokwi-gnd", "id": "gnd1", "top": 9.6, "left": -19.8, "attrs": {} },
    {
      "type": "wokwi-slide-potentiometer",
      "id": "pot1",
      "top": -81.2,
      "left": 119.2,
      "rotate": 270,
      "attrs": { "travelLength": "30" }
    }
  ],
  "connections": [
    [ "nano:3", "ring1:DIN", "green", [ "v0" ] ],
    [ "vcc1:VCC", "ring1:VCC", "red", [ "v67.2", "h105.6" ] ],
    [ "gnd1:GND", "ring1:GND", "black", [ "v-9.6", "h96" ] ],
    [ "nano:5V", "pot1:VCC", "red", [ "v19.2", "h67.2" ] ],
    [ "nano:A0", "pot1:SIG", "green", [ "v28.8", "h192" ] ]
  ],
  "dependencies": {}
}

I forgot to give an update.:blush:
Since it was my first arduino ide project, i asked my friend chatgpt and it made a sketch.
It worked but it was flickering between leds,so i asked for a new sketch with the flickering deleted.
It added "smoothing" to the sketch,and now it works as i had hoped for.
Hopefully someone can use this advice to ask openAI ,since a computer knows the computer language best.

At best, a computer "knows" what it has been programmed to do

At worst, a computer "knows" nothing

Please post the best AI effort that you have got working. Who knows, maybe it can be improved by a human

Wrong. You like when you ask your friend to help you and they give you useless work, and you have to ask a second time? Look one post above yours. I explained how to start. How to locate critical points. How to clear unused pixels. How to cut your code in half. I even gave you a working simulation.

But you prefer a garbage-writing "friend?"

Strange world.

#include <Adafruit_NeoPixel.h>

#define PIN        6       // De pin waar de LED strip aan is verbonden
#define NUM_LEDS   18      // Het aantal LED's in de strip
#define POT_PIN    A0      // De pin waar de potentiometer aan is verbonden
#define SMOOTHING  0.1     // Smoothing factor, tussen 0 en 1 (hoe hoger, hoe soepeler de overgang)

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);

float smoothedValue = 0;  // Variabele om de gesmoothe waarde van de potentiometer op te slaan

void setup() {
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  pinMode(POT_PIN, INPUT);
}

void loop() {
  int potValue = analogRead(POT_PIN); // Lees de waarde van de potentiometer (0-1023)
  smoothedValue = (1.0 - SMOOTHING) * smoothedValue + SMOOTHING * potValue; // Pas smoothing toe

  int ledCount = map(smoothedValue, 0, 1023, 0, NUM_LEDS / 2); // Map de gesmoothe waarde naar het aantal LED's om aan te zetten

  // Zet alle LED's uit
  for (int i = 0; i < NUM_LEDS; i++) {
    strip.setPixelColor(i, strip.Color(0, 0, 0));
  }

  // Zet LED's aan van het midden naar buiten
  for (int i = 0; i < ledCount; i++) {
    strip.setPixelColor((NUM_LEDS / 2) - i - 1, strip.Color(255, 0, 0)); // Linkerzijde
    strip.setPixelColor((NUM_LEDS / 2) + i, strip.Color(255, 0, 0)); // Rechterzijde
  }

  strip.show(); // Update de strip met de nieuwe waarden

  delay(10); // Een kleine vertraging om flikkeringen te voorkomen
}

Don't take the post so seriously.
I don't wrote it to offend you or anybody.
My wife name isn't Google,because she knows everything better😉.

I really appreciate your comment ,where you explained how to set up this sketch.
And tried to make a sketch of it,but as i say ,i am a rooky at it .
So then it came in my mind,to ask openAI.

It just saved me a lot of time,that i can use for other things.

This is not a balanced comment -- for my case, ChatGPT has always provided sensible information which I can manipulte and manage to prepare my own version.

In recent times, I asked the OpenAI to illustrate the difference between "Microcontroller Foundation" and "Microcontroller Foundations", it nicely and in well formatted manner has described the difference.

I am in Arduino Forum for my own better leanring. I have many many small questions which when placed in Forum are termed by some veteran posters that -- "I am trolling". ChatGPT never feels tired of answering any question.

I could place the above write-up to ChatGPT for revision; in response, I would get a much better write-up which I have denied to respect my own style of writing.

Not always!

The sketch that your friend ChatGPT has provided works well for my 30 LED Strip except that the sketch hadles only 26 LEDs.

The ChatGPT's sketch of your post #12 has declared the smoothedValue variable as float. Should not it be an int type variable as it has been used in map() function or in the map() function, should not it be used as (int)smoothedValue (doing the cast) to take only the integer part?

It is a comment borne from experience. I have had "ChatGPT" workers who I would rather send home than "repair" an issue because I have to chase them around to get them to start, send them back to the site to fix the other stuff they broke, and fix the stuff they broke when they were fixing the stuff they broke. I would save time and energy by doing it myself, on top of my own work. A tool in the hands of an expert is a benefit. A tool the hands of a novice is a weapon.

1 Like

For the case of the OP, the tool (ChatGPT) has worked as a benefit -- the sketch (post #12) provided by ChatGPT is working the way he has wanted?

Yes, I see. I agree with myself. Maybe a sub-topic of non-specific-subject, rather "ChatGPT your ChatGPT" would rend this weapon into ploughshares... and let ChatGPT be the moderator... or you.

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