Hello Arduino Forum!
I am working on a project using an Arduino Uno, a WS2812b LED strip and a BLE UART friend.
The goal of my little project is to collect data which I input from my phone (in the Bluefruit connect app) and use it to choose different pre-programmed options on my LED strip. I simply want to use the "controller -> control pad" to send my data. To put it simply, I want to press one button to make the LED strip do one thing, and another button to do something else.
My problem is that I can't seem to figure out how to use this data to do something with the LED strip and simultaneously still check for new incoming data.
I am having problems such as the LED strip doing mode 1 while I haven't even pressed anything yet.
I am currently using a method that looks like this:
if (strcmp(ble.buffer, "!B11:")) {
mode = 1; // Rainbow
}
The "!B11:" is the data which is collected upton pressing button number 1, from what I learned by screwing around with Serial Printing random button options.
I will post my full code below.
Main Tab:
#include <Arduino.h>
#include <SPI.h>
#include "modes.h"
#include "Adafruit_BLE.h"
#include "Adafruit_BluefruitLE_SPI.h"
#include "Adafruit_BluefruitLE_UART.h"
#include "BluefruitConfig.h"
#if SOFTWARE_SERIAL_AVAILABLE
#include <SoftwareSerial.h>
#endif
#define FACTORYRESET_ENABLE 0
#define MINIMUM_FIRMWARE_VERSION "0.6.6"
#define MODE_LED_BEHAVIOUR "MODE"
SoftwareSerial bluefruitSS = SoftwareSerial(BLUEFRUIT_SWUART_TXD_PIN, BLUEFRUIT_SWUART_RXD_PIN);
Adafruit_BluefruitLE_UART ble(bluefruitSS, BLUEFRUIT_UART_MODE_PIN,
BLUEFRUIT_UART_CTS_PIN, BLUEFRUIT_UART_RTS_PIN);
void error(const __FlashStringHelper*err) {
Serial.println(err);
while (1);
}
void setup(void)
{
Serial.begin(115200);
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(BRIGHTNESS);
/* Select room, comment other lines */
//void Matthijs();
//void Dexter();
//void Bar();
void Test();
Serial.println(F("Adafruit Bluefruit Command Mode Example"));
Serial.println(F("---------------------------------------"));
Serial.print(F("Initialising the Bluefruit LE module: "));
if ( !ble.begin(VERBOSE_MODE) )
{
error(F("Couldn't find Bluefruit, make sure it's in CoMmanD mode & check wiring?"));
}
Serial.println( F("OK!") );
if ( FACTORYRESET_ENABLE )
{
Serial.println(F("Performing a factory reset: "));
if ( ! ble.factoryReset() ) {
error(F("Couldn't factory reset"));
}
}
ble.echo(false);
Serial.println("Requesting Bluefruit info:");
ble.info();
Serial.println(F("Please use Adafruit Bluefruit LE app to connect in UART mode"));
Serial.println(F("Then Enter characters to send to Bluefruit"));
Serial.println();
ble.verbose(false); // debug info is a little annoying after this point!
while (! ble.isConnected()) {
delay(500);
}
// LED Activity command is only supported from 0.6.6
if ( ble.isVersionAtLeast(MINIMUM_FIRMWARE_VERSION) )
{
// Change Mode LED Activity
Serial.println(F("******************************"));
Serial.println(F("Change LED activity to " MODE_LED_BEHAVIOUR));
ble.sendCommandCheckOK("AT+HWModeLED=" MODE_LED_BEHAVIOUR);
Serial.println(F("******************************"));
}
}
void loop(void)
{
checkInput();
}
bool getUserInput(char buffer[], uint8_t maxSize)
{
// timeout in 100 milliseconds
TimeoutTimer timeout(100);
memset(buffer, 0, maxSize);
while ( (!Serial.available()) && !timeout.expired() ) {
delay(1);
}
if ( timeout.expired() ) return false;
delay(2);
uint8_t count = 0;
do
{
count += Serial.readBytes(buffer + count, maxSize);
delay(2);
} while ( (count < maxSize) && (Serial.available()) );
return true;
}
void checkInput() {
int mode = 0;
bool loopMode = true;
// Check for incoming characters from Bluefruit
ble.println("AT+BLEUARTRX");
ble.readline();
if (strcmp(ble.buffer, "OK")) {
mode = 0;
}
// Some data was found, its in the buffer
Serial.print(F("[Recv] ")); Serial.println(ble.buffer);
if (strcmp(ble.buffer, "!B11:")) {
mode = 1; // Rainbow
}
else if (strcmp(ble.buffer, "!B219")) {
mode = 2; // Bounce
}
else if (strcmp(ble.buffer, "!B309")) {
mode = 3; // Double Rainbow
}
else if (strcmp(ble.buffer, "!B318")) {
mode = 4;
}
else if (strcmp(ble.buffer, "!B417")) {
mode = 5;
}
else if (strcmp(ble.buffer, "!B516")) {
mode = 6;
}
else if (strcmp(ble.buffer, "!B714")) {
mode = 7;
}
else if (strcmp(ble.buffer, "!B813")) {
mode = 8;
}
else {
mode = 0;
}
Serial.print("Mode: "); Serial.println(mode);
switch (mode) {
case 0:
loopMode = true;
break;
case 1:
rainbow();
break;
case 2:
bounce();
break;
case 3:
doubleBounce();
break;
case 4:
singleRainbowBounce();
break;
case 5:
doubleRainbowBounce();
break;
case 6:
rainbowThroughStrip();
break;
case 7:
Serial.println("Speed Increased");
break;
case 8:
Serial.println("Speed Decreased");
break;
}
}
Configuration Tab:
// COMMON SETTINGS
// ----------------------------------------------------------------------------------------------
// These settings are used in both SW UART, HW UART and SPI mode
// ----------------------------------------------------------------------------------------------
#define BUFSIZE 128 // Size of the read buffer for incoming data
#define VERBOSE_MODE true // If set to 'true' enables debug output
// SOFTWARE UART SETTINGS
// ----------------------------------------------------------------------------------------------
// The following macros declare the pins that will be used for 'SW' serial.
// You should use this option if you are connecting the UART Friend to an UNO
// ----------------------------------------------------------------------------------------------
#define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial!
#define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial!
#define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial!
#define BLUEFRUIT_UART_RTS_PIN 8 // Optional, set to -1 if unused
// HARDWARE UART SETTINGS
// ----------------------------------------------------------------------------------------------
// The following macros declare the HW serial port you are using. Uncomment
// this line if you are connecting the BLE to Leonardo/Micro or Flora
// ----------------------------------------------------------------------------------------------
#ifdef Serial1 // this makes it not complain on compilation if there's no Serial1
#define BLUEFRUIT_HWSERIAL_NAME Serial1
#endif
// SHARED UART SETTINGS
// ----------------------------------------------------------------------------------------------
// The following sets the optional Mode pin, its recommended but not required
// ----------------------------------------------------------------------------------------------
#define BLUEFRUIT_UART_MODE_PIN 12 // Set to -1 if unused
// SHARED SPI SETTINGS
// ----------------------------------------------------------------------------------------------
// The following macros declare the pins to use for HW and SW SPI communication.
// SCK, MISO and MOSI should be connected to the HW SPI pins on the Uno when
// using HW SPI. This should be used with nRF51822 based Bluefruit LE modules
// that use SPI (Bluefruit LE SPI Friend).
// ----------------------------------------------------------------------------------------------
#define BLUEFRUIT_SPI_CS -1
#define BLUEFRUIT_SPI_IRQ -1
#define BLUEFRUIT_SPI_RST -1 // Optional but recommended, set to -1 if unused
// SOFTWARE SPI SETTINGS
// ----------------------------------------------------------------------------------------------
// The following macros declare the pins to use for SW SPI communication.
// This should be used with nRF51822 based Bluefruit LE modules that use SPI
// (Bluefruit LE SPI Friend).
// ----------------------------------------------------------------------------------------------
#define BLUEFRUIT_SPI_SCK -1
#define BLUEFRUIT_SPI_MISO -1
#define BLUEFRUIT_SPI_MOSI -1
"LED Modes" Tab:
#include <FastLED.h>
#define NUM_LEDS 73 // Change to correct amount of LEDs | One strip = 150 LEDs | Test strip = 73 LEDs
#define LED_PIN 3
#define COLOR_ORDER GRB
#define LED_TYPE WS2812B
int SATURATION = 255;
int BRIGHTNESS = 200;
int fadespeed = 4;
int modechoice = 7;
bool on = true;
bool bouncetoggle = true;
CRGB leds[NUM_LEDS];
void clearLeds() {
Serial.println("Clearing LEDs");
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB(0, 0, 0);
FastLED.show();
delay(25);
}
}
void rainbow() {
Serial.println("Rainbow");
for (int j = 0; j < 255; j++) {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CHSV(i - (j * fadespeed), SATURATION, BRIGHTNESS);
}
FastLED.show();
delay(25);
}
}
void bounce() {
Serial.println("Bounce");
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB(50, 200, 235);
FastLED.show();
delay(25);
}
for (int i = NUM_LEDS; i > -1; i--) {
leds[i] = CRGB(255, 0, 0);
FastLED.show();
delay(25);
}
}
void doubleBounce() {
Serial.println("Double Bounce");
for (int i = 0; i < ((NUM_LEDS / 2) + 1); i++) {
int a = i;
int b = NUM_LEDS - i;
leds[a] = CRGB(0, 255, 0);
leds[b] = CRGB(0, 255, 0);
FastLED.show();
delay(25);
}
for (int i = ((NUM_LEDS / 2) + 1); i > 0; i--) {
int a = i;
int b = NUM_LEDS - i;
leds[a] = CRGB(255, 0, 0);
leds[b] = CRGB(255, 0, 0);
FastLED.show();
delay(25);
}
}
void singleRainbowBounce() {
Serial.println("Rainbow Bounce");
for (int i = 0; i < (NUM_LEDS - 8); i++) {
int a = i + 0; leds[a] = CRGB(255, 0, 0);
int b = i + 1; leds[b] = CRGB(255, 111, 3);
int c = i + 2; leds[c] = CRGB(255, 240, 3);
int d = i + 3; leds[d] = CRGB(53, 252, 3);
int e = i + 4; leds[e] = CRGB(0, 255, 255);
int f = i + 5; leds[f] = CRGB(0, 0, 255);
int g = i + 6; leds[g] = CRGB(255, 0, 255);
int h = i + 7; leds[h] = CRGB(255, 0, 200);
// Turn off before and after:
int j = i - 1; leds[j] = CRGB(0, 0, 0);
int k = i + 8; leds[k] = CRGB(0, 0, 0);
FastLED.show();
delay(25);
}
for (int i = (NUM_LEDS - 8); i > -1; i--) {
int a = i + 0; leds[a] = CRGB(255, 0, 0);
int b = i + 1; leds[b] = CRGB(255, 111, 3);
int c = i + 2; leds[c] = CRGB(255, 240, 3);
int d = i + 3; leds[d] = CRGB(53, 252, 3);
int e = i + 4; leds[e] = CRGB(0, 255, 255);
int f = i + 5; leds[f] = CRGB(0, 0, 255);
int g = i + 6; leds[g] = CRGB(255, 0, 255);
int h = i + 7; leds[h] = CRGB(255, 0, 200);
// Turn off before and after:
int j = i - 1; leds[j] = CRGB(0, 0, 0);
int k = i + 8; leds[k] = CRGB(0, 0, 0);
FastLED.show();
delay(25);
}
}
void doubleRainbowBounce() {
Serial.println("Rainbow Double Bounce");
for (int i = 0; i < ((NUM_LEDS / 2) - 8); i++) {
int a1 = i + 0; leds[a1] = CRGB(255, 0, 0); int a2 = ((NUM_LEDS - 8) - i) + 0; leds[a2] = CRGB(255, 0, 200);
int b1 = i + 1; leds[b1] = CRGB(255, 111, 3); int b2 = ((NUM_LEDS - 8) - i) + 1; leds[b2] = CRGB(255, 0, 255);
int c1 = i + 2; leds[c1] = CRGB(255, 240, 3); int c2 = ((NUM_LEDS - 8) - i) + 2; leds[c2] = CRGB(0, 0, 255);
int d1 = i + 3; leds[d1] = CRGB(53, 252, 3); int d2 = ((NUM_LEDS - 8) - i) + 3; leds[d2] = CRGB(0, 255, 255);
int e1 = i + 4; leds[e1] = CRGB(0, 255, 255); int e2 = ((NUM_LEDS - 8) - i) + 4; leds[e2] = CRGB(53, 252, 3);
int f1 = i + 5; leds[f1] = CRGB(0, 0, 255); int f2 = ((NUM_LEDS - 8) - i) + 5; leds[f2] = CRGB(255, 240, 3);
int g1 = i + 6; leds[g1] = CRGB(255, 0, 255); int g2 = ((NUM_LEDS - 8) - i) + 6; leds[g2] = CRGB(255, 111, 3);
int h1 = i + 7; leds[h1] = CRGB(255, 0, 200); int h2 = ((NUM_LEDS - 8) - i) + 7; leds[h2] = CRGB(255, 0, 0);
// Turn off before and after:
int i1 = i - 1; leds[i1] = CRGB(0, 0, 0); int i2 = ((NUM_LEDS - 8) - i) - 1; leds[i2] = CRGB(0, 0, 0);
int j1 = i + 8; leds[j1] = CRGB(0, 0, 0); int j2 = ((NUM_LEDS - 8) - i) + 8; leds[j2] = CRGB(0, 0, 0);
FastLED.show();
delay(25);
}
for (int i = ((NUM_LEDS / 2) - 8); i > -1; i--) {
int a1 = i + 0; leds[a1] = CRGB(255, 0, 0); int a2 = ((NUM_LEDS - 8) - i) + 0; leds[a2] = CRGB(255, 0, 200);
int b1 = i + 1; leds[b1] = CRGB(255, 111, 3); int b2 = ((NUM_LEDS - 8) - i) + 1; leds[b2] = CRGB(255, 0, 255);
int c1 = i + 2; leds[c1] = CRGB(255, 240, 3); int c2 = ((NUM_LEDS - 8) - i) + 2; leds[c2] = CRGB(0, 0, 255);
int d1 = i + 3; leds[d1] = CRGB(53, 252, 3); int d2 = ((NUM_LEDS - 8) - i) + 3; leds[d2] = CRGB(0, 255, 255);
int e1 = i + 4; leds[e1] = CRGB(0, 255, 255); int e2 = ((NUM_LEDS - 8) - i) + 4; leds[e2] = CRGB(53, 252, 3);
int f1 = i + 5; leds[f1] = CRGB(0, 0, 255); int f2 = ((NUM_LEDS - 8) - i) + 5; leds[f2] = CRGB(255, 240, 3);
int g1 = i + 6; leds[g1] = CRGB(255, 0, 255); int g2 = ((NUM_LEDS - 8) - i) + 6; leds[g2] = CRGB(255, 111, 3);
int h1 = i + 7; leds[h1] = CRGB(255, 0, 200); int h2 = ((NUM_LEDS - 8) - i) + 7; leds[h2] = CRGB(255, 0, 0);
// Turn off before and after:
int i1 = i - 1; leds[i1] = CRGB(0, 0, 0); int i2 = ((NUM_LEDS - 8) - i) - 1; leds[i2] = CRGB(0, 0, 0);
int j1 = i + 8; leds[j1] = CRGB(0, 0, 0); int j2 = ((NUM_LEDS - 8) - i) + 8; leds[j2] = CRGB(0, 0, 0);
FastLED.show();
delay(25);
}
}
void rainbowThroughStrip() {
Serial.println("Rainbow Through Strip");
for (int i = 8; i < (NUM_LEDS - 1); i++) {
int a = i + 0;
int b = i - 9;
if (a > -1 and a < 9) {
leds[a] = CRGB(255, 0, 0);
}
else if (a > 8 and a < 18) {
leds[a] = CRGB(255, 111, 3);
leds[b] = CRGB(0, 0, 0);
}
else if (a > 17 and a < 27) {
leds[a] = CRGB(255, 240, 3);
leds[b] = CRGB(0, 0, 0);
}
else if (a > 26 and a < 36) {
leds[a] = CRGB(53, 252, 3);
leds[b] = CRGB(0, 0, 0);
}
else if (a > 35 and a < 45) {
leds[a] = CRGB(0, 255, 255);
leds[b] = CRGB(0, 0, 0);
}
else if (a > 44 and a < 54) {
leds[a] = CRGB(0, 0, 255);
leds[b] = CRGB(0, 0, 0);
}
else if (a > 53 and a < 63) {
leds[a] = CRGB(200, 0, 255);
leds[b] = CRGB(0, 0, 0);
}
else if (a > 62 and a < 72) {
leds[a] = CRGB(255, 0, 255);
leds[b] = CRGB(0, 0, 0);
}
FastLED.show();
delay(25);
}
for (int i = (NUM_LEDS - 9); i > -1; i--) {
int a = i + 0;
int b = i + 9;
if (a > 62 and a < 72) {
leds[a] = CRGB(255, 0, 255);
}
if (a > 53 and a < 63) {
leds[a] = CRGB(200, 0, 255);
leds[b] = CRGB(0, 0, 0);
}
if (a > 44 and a < 54) {
leds[a] = CRGB(0, 0, 255);
leds[b] = CRGB(0, 0, 0);
}
if (a > 35 and a < 45) {
leds[a] = CRGB(0, 255, 255);
leds[b] = CRGB(0, 0, 0);
}
if (a > 26 and a < 36) {
leds[a] = CRGB(53, 252, 3);
leds[b] = CRGB(0, 0, 0);
}
if (a > 17 and a < 27) {
leds[a] = CRGB(255, 240, 3);
leds[b] = CRGB(0, 0, 0);
}
if (a > 8 and a < 18) {
leds[a] = CRGB(255, 111, 3);
leds[b] = CRGB(0, 0, 0);
}
if (a > -1 and a < 9) {
leds[a] = CRGB(255, 0, 0);
leds[b] = CRGB(0, 0, 0);
}
FastLED.show();
delay(25);
}
}
void purpleFade() {
Serial.println("Purple Fade");
for (int i = 0; i < NUM_LEDS; i++) {
int red = (100 * i / NUM_LEDS) * (255 / 100);
int blue = 255 - red;
Serial.print("Red: ");
Serial.println(red);
Serial.print("Blue: ");
Serial.println(blue);
leds[i] = CRGB(red, 0, blue);
FastLED.show();
delay(25);
}
for (int i = NUM_LEDS; i > 0; i--) {
int red = (100 * i / (255 - NUM_LEDS)) * (255 / 100);
int blue = 255 - red;
leds[i] = CRGB(red, 0, blue);
FastLED.show();
delay(25);
}
}