Help needed for a led bar project

Can someone enlight me?

I'm working on a project of doing my own led bar for my setup.

The setup have been thought as follow:

56 leds RGB 2812B

Controlled by 4 potentiometer linear 1K Ohm one for the intensity and the 3 other respectively for Red Green and Blue.

The whole thing is connected to an arduino R3

An the leds are alimented by a 5v 3amps power supply.

On an electric plan all the thing works as intented, i've planned the whole thing on tinkercad : Login - Tinkercad

But i'm having a problem, the arduino seems to ignore the potentiometer and choose random color and brightness for the leds and the whole bar flicker as hell...

The code i use is this one:

``#include <Adafruit_NeoPixel.h>`

`#define PIN 2 // input pin Neopixel is attached to`

`#define NUMPIXELS` `56 // number of neopixels in Ring`

`Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);`

`int delayval = 1; // timing delay`

`int redColor = 0;`

`int greenColor = 0;`

`int blueColor = 0;`

`int potAngle = A0;`

`void setup() {`

`pixels.begin(); // Initializes the NeoPixel library.`

`Serial.begin(9600);`

`}`

`void loop() {`

`//get colors from potentiometers`

`getColor();`

`//initialize all colors to 'off'`

`//this line enables real-time adjustments.`

`pixels.show();`

`int val = analogRead(potAngle);`

`//this line converts the angle of potentiometer to the brightness of LEDs.`

`int brightness = map(val, 0, 1023, 0, 255);`

`Serial.print("brightness: ");`

`Serial.println(brightness);`

`//this for loop controls all the 24 LEDs every single time.`

`for (int i = 0; i < NUMPIXELS; i++) {`

`// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255`

`pixels.setPixelColor(i, pixels.Color(redColor * brightness / 255, greenColor * brightness / 255, blueColor * brightness / 255));`

`//pixels.show(); // This sends the updated pixel color to the hardware.`

`}`

`pixels.show();`

`}`

`// setColor()`

`// picks random values to set for RGB`

`void setColor() {`

`redColor = random(0, 255);`

`greenColor = random(0, 255);`

`blueColor = random(0, 255);`

`Serial.print("red: ");`

`Serial.println(redColor);`

`Serial.print("green: ");`

`Serial.println(greenColor);`

`Serial.print("blue: ");`

`Serial.println(blueColor);`

`}`

`void getColor() {`

`int redAngle = analogRead(A1);`

`int greenAngle = analogRead(A2);`

`int blueAngle = analogRead(A3);`

`int rVal = map(redAngle, 0, 1023, 0, 255);`

`int gVal = map(greenAngle, 0, 1023, 0, 255);`

`int bVal = map(blueAngle, 0, 1023, 0, 255);`

`redColor = rVal;`

`greenColor = gVal;`

`blueColor = bVal;`

`Serial.print("red: ");`

`Serial.println(redColor);`

`Serial.print("green: ");`

`Serial.println(greenColor);`

`Serial.print("blue: ");`

`Serial.println(blueColor);`

`}`

`Use code tags to format code for the forum
If anyone got clues to solve the trouble i'm opne to suggestion.

Schematics, code without empty lines, in code tags would be good.

I suggest you print the values that you are passing off to functions. You may see some surprises

// right here, print like

  Serial.println(" red : ") ; Serial.println(redColor * brightness / 255);

// and print the green and blue calculated colours too

  pixels.setPixelColor(i, pixels.Color(redColor * brightness / 255, greenColor * brightness / 255, blueColor * brightness / 255));

Also please try again to post your code. One way is to use the <CODE/> button in the message composition window tool bar, and paste your code where it says so it looks like code

void setup() {
  pixels.begin(); // Initializes the NeoPixel library.
  Serial.begin(9600);
}

Before you copy your code in the IDE, apply the Auto Format tool so your code is in one of the more common styles, it makes it easier for everyone to read.

a7

I would not exclude wiring/powering mistake...

@EcardBruno - Your sketch works. Check your wiring.

  • What devices are attached to A1, A2 and A3? (I put potentiometers on them)
  • Using the four pots, I can adjust colors
  • I updated the text print to be more readable.
  • setColor() is never used.

Placing these WOKWI.COM files here until I can get to my computer...

sketch.ino
#include <Adafruit_NeoPixel.h>
#define PIN 2 // input pin Neopixel is attached to
#define NUMPIXELS 56 // number of neopixels in Ring
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

int delayval = 1; // timing delay
int redColor = 0;
int greenColor = 0;
int blueColor = 0;
int pot0Angle = A0;
int pot1Angle = A1;
int pot2Angle = A2;
int pot3Angle = A3;

void setup() {
  pixels.begin(); // Initializes the NeoPixel library.
  Serial.begin(9600);
}

void loop() {
  //get colors from potentiometers
  getColor();
  //initialize all colors to 'off'
  //this line enables real-time adjustments.
  pixels.show();
  int val = analogRead(pot0Angle);
  //this line converts the angle of potentiometer to the brightness of LEDs.
  int brightness = map(val, 0, 1023, 0, 255);
  Serial.print(" brightness: ");
  Serial.print(brightness);
  //this for loop controls all the 24 LEDs every single time.
  for (int i = 0; i < NUMPIXELS; i++) {
    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    pixels.setPixelColor(i, pixels.Color(redColor * brightness / 255, greenColor * brightness / 255, blueColor * brightness / 255));
    //pixels.show(); // This sends the updated pixel color to the hardware.
  }
  pixels.show();
}

// setColor() picks random values to set for RGB
void setColor() {
  redColor = random(0, 255);
  greenColor = random(0, 255);
  blueColor = random(0, 255);
  Serial.print("red: ");
  Serial.print(redColor);
  Serial.print(" green: ");
  Serial.print(greenColor);
  Serial.print(" blue: ");
  Serial.print(blueColor);
}

void getColor() {
  int redAngle = analogRead(pot1Angle);
  int greenAngle = analogRead(pot2Angle);
  int blueAngle = analogRead(pot3Angle);
  int rVal = map(redAngle, 0, 1023, 0, 255);
  int gVal = map(greenAngle, 0, 1023, 0, 255);
  int bVal = map(blueAngle, 0, 1023, 0, 255);
  redColor = rVal;
  greenColor = gVal;
  blueColor = bVal;
  Serial.print(" _red: ");
  Serial.print(redColor);
  Serial.print(" _green: ");
  Serial.print(greenColor);
  Serial.print(" _blue: ");
  Serial.println(blueColor);
}

diagram.json
{
  "version": 1,
  "author": "Anonymous maker",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-nano", "id": "nano", "top": 4.8, "left": -29.3, "attrs": {} },
    {
      "type": "wokwi-led-ring",
      "id": "ring1",
      "top": -403.2,
      "left": -109.53,
      "attrs": { "pixels": "56" }
    },
    { "type": "wokwi-potentiometer", "id": "pot1", "top": -10.9, "left": 182.2, "attrs": {} },
    { "type": "wokwi-potentiometer", "id": "pot2", "top": -10.9, "left": 259, "attrs": {} },
    { "type": "wokwi-potentiometer", "id": "pot3", "top": -10.9, "left": 335.8, "attrs": {} },
    { "type": "wokwi-potentiometer", "id": "pot4", "top": -10.9, "left": 412.6, "attrs": {} }
  ],
  "connections": [
    [ "nano:2", "ring1:DIN", "green", [ "v0" ] ],
    [ "ring1:VCC", "nano:5V", "red", [ "v28.8", "h19.2", "v9.6" ] ],
    [ "ring1:GND", "nano:GND.1", "black", [ "v38.4", "h38.4" ] ],
    [ "pot1:VCC", "nano:5V", "red", [ "v28.8", "h-125.6" ] ],
    [ "pot1:SIG", "nano:A0", "green", [ "v38.4", "h-154" ] ],
    [ "pot1:GND", "nano:GND.1", "black", [ "v19.2", "h-86.4" ] ],
    [ "pot2:VCC", "pot1:VCC", "red", [ "v28.8", "h-77.6" ] ],
    [ "pot2:GND", "pot1:GND", "black", [ "v19.2", "h-76.8" ] ],
    [ "pot3:VCC", "pot2:VCC", "red", [ "v28.8", "h-77.6" ] ],
    [ "pot3:GND", "pot2:GND", "black", [ "v19.2", "h-67.2" ] ],
    [ "pot4:VCC", "pot3:VCC", "red", [ "v28.8", "h-77.6" ] ],
    [ "pot4:GND", "pot3:GND", "black", [ "v19.2", "h-76.8" ] ],
    [ "pot4:SIG", "nano:A3", "green", [ "v38.4", "h-413.2" ] ],
    [ "pot3:SIG", "nano:A2", "green", [ "v38.4", "h-336.4" ] ],
    [ "pot2:SIG", "nano:A1", "green", [ "v38.4", "h-269.2" ] ]
  ],
  "dependencies": {}
}

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