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
#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);
}
}
}