I've been struggling with writing a code for Arduino UNO so that I could use my phone and change the colour of the LED strip via terminal.
#include <FastLED.h>
#include <SoftwareSerial.h>
SoftwareSerial btSerial(2, 4); // RX, TX
#define NUM_LEDS 22
#define DATA_PIN 6
CRGB leds[NUM_LEDS];
void setup() {
btSerial.begin(9600);
btSerial.println("Connected to Arduino");
FastLED.addLeds<WS2812, DATA_PIN>(leds, NUM_LEDS);
}
char a;
void loop()
{
if (btSerial.available())
{
a= (char)btSerial.read();
if(a=="b")
{
btSerial.println("Turning LED blue");
leds[0] = CRGB::Blue; // Turns 1 LED to blue
FastLED.show();
}
}
}
So basically I'd like to be able to change LED colours with my phone. If you enter "r", the LEDS turn red, if you enter "b", leds turn blue etc. Maybe even add some patterns. Brightness controller would be nice too to save some battery.
EDITED. Serial works.
#include <FastLED.h>
#include <SoftwareSerial.h>
SoftwareSerial btSerial(2, 4); // RX, TX
#define NUM_LEDS 22
#define DATA_PIN 6
CRGB leds[NUM_LEDS];
void setup() {
Serial.begin(9600);
btSerial.begin(9600);
btSerial.println("Connected to Arduino");
FastLED.addLeds<WS2812, DATA_PIN>(leds, NUM_LEDS);}
char a;
void loop(){
if (btSerial.available())
Serial.write(btSerial.read());
if (Serial.available())
btSerial.write(Serial.read());}
3.
#include <FastLED.h>
#include <SoftwareSerial.h>
SoftwareSerial btSerial(2, 4); // RX, TX
#define NUM_LEDS 22
#define DATA_PIN 6
CRGB leds[NUM_LEDS];
void setup() {
Serial.begin(9600);
btSerial.begin(9600);
btSerial.println("Connected to Arduino");
FastLED.addLeds<WS2812, DATA_PIN>(leds, NUM_LEDS);}
char a;
void loop(){
if (btSerial.available())
Serial.write(btSerial.read());
if (Serial.available())
btSerial.write(Serial.read());}
if (a=='b'){
btSerial.println("Turning LED blue");
leds[0] = CRGB::Blue;
FastLED.show();}
RECENT:
#include <FastLED.h>
#include <SoftwareSerial.h>
SoftwareSerial btSerial(2, 4); // RX, TX
#define NUM_LEDS 28
#define DATA_PIN 6
CRGB leds[NUM_LEDS];
void setup()
{
Serial.begin(9600);
btSerial.begin(9600);
btSerial.println("Connected to Arduino");
FastLED.addLeds<WS2812, DATA_PIN>(leds, NUM_LEDS);
}
char a;
void loop()
{
if (btSerial.available())
{
a= (char)btSerial.read();
if( a == 'b' )
{
btSerial.println("Sinine"); //All LEDs are blue
fill_solid( leds, NUM_LEDS, CRGB::Blue);
FastLED.show();
}
if( a == 'r' )
{
btSerial.println("Punane"); //All LEDs are red
fill_solid( leds, NUM_LEDS, CRGB::Green);
FastLED.show();
}
if( a == 'g' )
{
btSerial.println("Roheline"); //All LEDs are green
fill_solid( leds, NUM_LEDS, CRGB::Red);
FastLED.show();
}
if( a == 'y' )
{
btSerial.println("Sinine"); //All LEDs are yellow
fill_solid( leds, NUM_LEDS, CRGB::Yellow);
FastLED.show();
}
if( a == 'w' )
{
btSerial.println("Sinine"); //All LEDs are white
fill_solid( leds, NUM_LEDS, CRGB::White);
FastLED.show();
}
if( a == 'o' )
{
btSerial.println("Väljas"); //All LEDs are turned off
fill_solid( leds, NUM_LEDS, CRGB::Black);
FastLED.show();}
if ( a == 'z' ) //1 blue LED from start to finish
{
int i= 2;
while (i > 0)
{
for (int dot = 0; dot < NUM_LEDS; dot++)
{
leds[dot] = CRGB::Blue;
FastLED.show();
leds[dot] = CRGB::Black;
delay(100);
}
}
}
if ( a == 'x' ) //1 red LED from start to finish
{
int i= 2;
while (i > 0)
{
for (int dot = 0; dot < NUM_LEDS; dot++)
{
leds[dot] = CRGB::Green;
FastLED.show();
leds[dot] = CRGB::Black;
delay(100);
}
}
}
}
}