0

My first question here. As an example, I'm using https://howtomechatronics.com/tutorials/arduino/how-to-control-ws2812b-individually-addressable-leds-using-arduino/ code to control my lighting, which I'm going to install to my car, just my code was a bit simplified, but I left the line, where brightness is being controlled and here's the fun part. When trying to change the brightness, it changes inconsistently. When increasing, it gets a bit brighter then a bit dimmer and at some spots it just turns off, when it should be bright. I'm also new to Arduino, so don't really know how Arduino reads these values, but maybe there is some other way to control brightness?

#include <SoftwareSerial.h>
#include <FastLED.h>
#define NUM_LEDS 20 
#define PIN 5 
CRGB leds[NUM_LEDS];
int brightness = 155;
//int fadeAmount = 5; 
SoftwareSerial BT(0,1);
String dataIn = "";

void setup() {

  FastLED.addLeds<WS2811, PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  FastLED.setBrightness(brightness);
  Serial.begin(9600);
  BT.begin(9600);
  setAll(50, 50, 50); 
}

void loop() { 


  if (BT.available() > 0) {
    dataIn = BT.readString();
    delay(10);
    FastLED.show();
  }
    if (dataIn.startsWith("0")) {
        setAll(0,0,0);
        delay(10);
    FastLED.show();
    }
    else if (dataIn.startsWith("w")) {
        setAll(255,255,255);
        delay(10);
    FastLED.show();
    }
    else if (dataIn.startsWith("r")) {      
        setAll(0,255,0);
        delay(10);
    FastLED.show();
    }
    else if (dataIn.startsWith("g")) {
        setAll(255,0,0);
        delay(10);
    FastLED.show();
    }
    else if (dataIn.startsWith("b")) {
        setAll(0,0,255);
        delay(10);
    FastLED.show();
    }
    else if (dataIn.startsWith("y")) {
        setAll(200,255,0);
    }
    else if (dataIn.startsWith("1")) { //This is the brightness lines
      String stringBrightness = dataIn.substring(dataIn.indexOf("1") + 1);
      brightness = stringBrightness.toInt();
      FastLED.setBrightness(brightness);
      FastLED.show();
    }
  }


void showStrip() {
   FastLED.show();
}

void setPixel(int Pixel, byte red, byte green, byte blue) {
   leds[Pixel].r = red;
   leds[Pixel].g = green;
   leds[Pixel].b = blue;
}

void setAll(byte red, byte green, byte blue) {
  for(int i = 0; i < NUM_LEDS; i++ ) {
    setPixel(i, red, green, blue); 
  }
  showStrip();
}
  • A couple of comments: 1) why are you using a software serial? If you use pins 0 and 1 use the hardware serial, otherwise if you must use other pins set them correctly. 2) Are you saying that even if you are setting all the leds to the same color some of them have different brightness than the other? It seems to me this is a power supply issue rather than a control one.. Are all the LEDs the same ones, from the same strip? – frarugi87 Jun 7 at 10:00
  • @frarugi87 I guess it would be best to make a short video to show what I mean, because I dont know how to say that for everyone to understand correctly. I'll put a link later today, because now I have to go to work. – M4relis Jun 7 at 10:06
  • Using BT.readString() is asking for trouble, as the result is dependent on the timing of the serial data stream. Try using linefeed delimited commands instead. – Edgar Bonet Jun 7 at 10:09
  • 1
    For sure use the hardware serial rather then software serial, but before you even mess with getting the serial figured out, make sure the LED's work as you expect. In the setup function do something like: setAll(255,0,0); delay(1000);setAll(128,0,0);delay(1000);setAll(10,0,0); delay(1000); Does this make them all start out bright red and then go half bright and then very dim or off? If this is not producing the expected results then you need to work on that. Once you can make it work as you want by directly coding it that way, then introduce the ability to control it via commands. – Chad G Jun 7 at 16:44
  • Another gotcha: I assume \these LEDs use PWM to get different brightness levels for each color channel. If the cycle rate is slow enough, the pulsing of the LEDs can create strange light trail effects when the light source is moving across the field of view of the observer. You can see this effect if you take an LED calculator and sweep it rapidly across your field of view. Very high frequency PWM usually solves the problem, but since these are LEDs that are controlled using a serial interface (I2C?) you likely don't have any control over the PWM frequency used to drive them. – Duncan C Jun 7 at 22:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.