Go Down

Topic: FastLed:ws2812b how do i start with all the Leds off and only light particular L (Read 181 times) previous topic - next topic

TheAwakened99

Hello,

I'm working on a project which requires me to switch on only particular LEDs via Bluetooth using an app, for this to work as I want it to the LEDs need to be turned off at the start and only switch on when a particular input is received from the app. But when I connect the Arduino to the power supply all the LEDs are turned on at the start.

from another source, I learned that I would have to use a for loop and turn all the leads to black but which would be the best place to place this loop as I wouldn't want this loop to execute again until the Bluetooth connection is terminated.

Here is my code

Code: [Select]
#include <FastLED.h>
#define NUM_LEDS 5 //no of leds
#define DATA_PIN 5
#define BRIGHTNESS 96             

//#define ledPin 7               //pin to which led is connected on arduino
int state = 0;                 //variable to store data coming from phone

CRGB leds[NUM_LEDS];

void setup()
{
  FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS);                          //setup led strip
  //pinMode(ledPin, OUTPUT);   //set led pin as op for bluetooth
  //digitalWrite(ledPin, LOW); //setting op as low
  Serial.begin(38400); // Default communication rate of the Bluetooth module
  FastLED.setBrightness(BRIGHTNESS );
 }
void loop()
{
    if(Serial.available() > 0) // Checks whether data is comming from the serial port
    {
state = Serial.read(); // Reads the data from the serial port
if(state=='1')
{
leds[0] = CRGB::Red;
        FastLED.show();
        delay(30);
}
if(state=='2')
{
leds[1] = CRGB::Red;
        FastLED.show();
        delay(30);
}
if(state=='3')
{
leds[2] = CRGB::Red;
        FastLED.show();
        delay(30);
}
if(state=='4')
{
leds[3] = CRGB::Red;
        FastLED.show();
        delay(30);
}
if(state=='5')
{
leds[4] = CRGB::Red;
        FastLED.show();
        delay(30);
}
     }
}

larryd

No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

Grumpy_Mike

Quote
. But when I connect the Arduino to the power supply all the LEDs are turned on at the
Yes that sometime happens.

Use the setup function, it gets called only once at the start.

TheAwakened99

Yes that sometime happens.

Use the setup function, it gets called only once at the start.
oh ok, so i would place the for loop in the setup function then?

Grumpy_Mike

Yes or the call to clear all the LEDs if the library you are using has one.

larryd


void setup()
{
   . . .

   FastLED.clear();
   FastLED.show();

}
No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.


Paul__B

But when I connect the Arduino to the power supply all the LEDs are turned on at the start.
Put a 10k resistor between the Arduino output which feeds the LEDs, and ground.

Go Up