I just purchased a Bluefruit LE Shield for my LED procject that is using the Adafruit 24-Channel 12-bit PWM LED Driver - SPI Interface - TLC5947
When using an older Blue tooth on serial 1 of my arduino mega everything worked as expected. I changed out to the BLE to support Ios devices. When the sketch calls up anything BLE I lose all control of my TLC5947 and IR receiver
Any Thoughts
Code: Select all
#include <string.h>
#include <Arduino.h>
#include <SPI.h>
#include "Adafruit_BLE.h"
#include "Adafruit_BluefruitLE_SPI.h"
#include "Adafruit_BluefruitLE_UART.h"
#include "BluefruitConfig.h"
#include <SoftwareSerial.h>
#include <IRremote.hpp>
String myCom;
#define FACTORYRESET_ENABLE 0
#define MINIMUM_FIRMWARE_VERSION "0.6.6"
#define MODE_LED_BEHAVIOUR "MODE"
#include "Adafruit_TLC5947.h"
#define IR_RECEIVE_PIN 44
#define Modules 1 // How many boards do you have chained?
#define data 38
#define clock 40
#define latch 42
#define oe -1 // set to -1 to not use the enable pin (its optional)
Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);
Adafruit_TLC5947 tlc = Adafruit_TLC5947(Modules, clock, data, latch);
// ***** A small Helper for Bluetooth LE ********************************************************************
void error(const __FlashStringHelper*err) {
Serial.println(err);
while (1);
}
// ******* function prototypes over in packetparser.cpp
uint8_t readPacket(Adafruit_BLE *ble, uint16_t timeout);
float parsefloat(uint8_t *buffer);
void printHex(const uint8_t * datahex, const uint32_t numBytes);
//********* the packet buffer
extern uint8_t packetbuffer[];
//******Define Varibles Section ******************************************************************************
int scene = 1; // Define the scene
int i = 999; // Define Channel
int state = 0;
String BlueRead = "";
String cmd;
int chase;
// ****** Build Channles ************************************************************************************
int ch0r = 0;
int ch0g = 0;
int ch0b = 0;
int ch1r = 0;
int ch1g = 0;
int ch1b = 0;
int ch2r = 0;
int ch2g = 0;
int ch2b = 0;
int ch3r = 0;
int ch3g = 0;
int ch3b = 0;
int ch4r = 0;
int ch4g = 0;
int ch4b = 0;
int ch5r = 0;
int ch5g = 0;
int ch5b = 0;
int ch6r = 0;
int ch6g = 0;
int ch6b = 0;
int ch7r = 0;
int ch7g = 0;
int ch7b = 0;
int ch8r = 0;
int ch8g = 0;
int ch8b = 0;
int ch9r = 0;
int ch9g = 0;
int ch9b = 0;
int ch10r = 0;
int ch10g = 0;
int ch10b = 0;
int ch11r = 0;
int ch11g = 0;
int ch11b = 0;
int ch12r = 0;
int ch12g = 0;
int ch12b = 0;
int ch13r = 0;
int ch13g = 0;
int ch13b = 0;
int ch14r = 0;
int ch14g = 0;
int ch14b = 0;
int ch15r = 0;
int ch15g = 0;
int ch15b = 0;
int ch16r = 0;
int ch16g = 0;
int ch16b = 0;
int ch17r = 0;
int ch17g = 0;
int ch17b = 0;
int ch18r = 0;
int ch18g = 0;
int ch18b = 0;
int ch19r = 0;
int ch19g = 0;
int ch19b = 0;
int ch20r = 0;
int ch20g = 0;
int ch20b = 0;
int ch21r = 0;
int ch21g = 0;
int ch21b = 0;
int ch22r = 0;
int ch22g = 0;
int ch22b = 0;
int ch23r = 0;
int ch23g = 0;
int ch23b = 0;
int ch24r = 0;
int ch24g = 0;
int ch24b = 0;
int ch25r = 0;
int ch25g = 0;
int ch25b = 0;
int ch26r = 0;
int ch26g = 0;
int ch26b = 0;
int ch27r = 0;
int ch27g = 0;
int ch27b = 0;
int ch28r = 0;
int ch28g = 0;
int ch28b = 0;
int ch29r = 0;
int ch29g = 0;
int ch29b = 0;
int ch30r = 0;
int ch30g = 0;
int ch30b = 0;
int ch31r = 0;
int ch31g = 0;
int ch31b = 0;
//****** run Once Section**************************************************************************************
void setup() {
Serial.begin(9600);
// Serial1.begin(9600); // Default communication rate of the Bluetooth module
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK); // Start the receiver
Serial.println("TLC5974 test");
// +++++++++ Start the LEDS +++++++++++++++++
tlc.begin();
if (oe >= 0) {
pinMode(oe, OUTPUT);
digitalWrite(oe, LOW);
}
// ++++++++ Start the Bluetooth LED ++++++++++
while (!Serial); // required for Flora & Micro
delay(500);
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 )
{
if ( ! ble.factoryReset() ){
error(F("Couldn't factory reset"));
}
}
ble.echo(false);
ble.sendCommandCheckOK("AT+GAPDEVNAME=JEEPLIGHTS");
ble.verbose(false); // debug info is a little annoying after this point!
while (! ble.isConnected()) {
delay(500);
}
if ( ble.isVersionAtLeast(MINIMUM_FIRMWARE_VERSION) )
{
// Change Mode LED Activity
Serial.println(F("Change LED activity to " MODE_LED_BEHAVIOUR));
ble.sendCommandCheckOK("AT+HWModeLED=" MODE_LED_BEHAVIOUR);
}
Serial.println( F("Switching to DATA mode!") );
ble.setMode(BLUEFRUIT_MODE_DATA);
}
//****** loop always Section**************************************************************************************
void loop() {
//***** IR Testing **************************************************************************************
if (IrReceiver.decode()) {
IrReceiver.printIRResultShort(&Serial); // optional use new print version;
if (IrReceiver.decodedIRData.command == 0x58) {
ch3r = 4095;
ch7r = 4095;
ch7b = 4095;
ch7g = 4095;
scene = 0;
}
if (IrReceiver.decodedIRData.command == 0x40) {
Turn_Off();
scene = 0;
}
if (IrReceiver.decodedIRData.command == 0x41) {
scene = 1;
}
IrReceiver.resume(); // Enable receiving of the next value
}
//****** Bluetooth LE ****************************************************************************************
/* Wait for new data to arrive */
uint8_t len = readPacket(&ble, BLE_READPACKET_TIMEOUT);
if (len == 0) return;
/* Got a packet! */
printHex(packetbuffer, len);
// Color
if (packetbuffer[1] == 'C') {
uint8_t red = packetbuffer[2];
uint8_t green = packetbuffer[3];
uint8_t blue = packetbuffer[4];
Serial.print ("RGB # in HEX ");
if (red < 0x10) Serial.print("0");
Serial.print(red, HEX);
if (green < 0x10) Serial.print("0");
Serial.print(green, HEX);
if (blue < 0x10) Serial.print("0");
Serial.println(blue, HEX);
Serial.print ("RGB # in PWM ");
Serial.print (" red = ");
Serial.print (red);
Serial.print (" Green = ");
Serial.print (green);
Serial.print (" Blue = ");
Serial.println (blue);
}
//****** Decode Blue Tooth ***********************************************************************************
// if(Serial1.available()> 0){ // Checks whether data is comming from the serial port
// BlueRead = Serial1.readString(); // Reads the data from the serial port
// }
if (BlueRead.startsWith("99999")) {
Turn_Off();
scene = 0;
Serial1.println("Turn Off"); // Send back, to the phone, the String "LED: ON"
BlueRead = "NUL";
}
if (BlueRead.startsWith("CH000")) {
delay(10);
String stringR = BlueRead.substring(BlueRead.indexOf("R") + 1, BlueRead.indexOf("G"));
ch0r = stringR.toInt();
String stringG = BlueRead.substring(BlueRead.indexOf("G") + 1, BlueRead.indexOf("B"));
ch0g = stringG.toInt();
String stringB = BlueRead.substring(BlueRead.indexOf("B") + 1, BlueRead.indexOf("E"));
ch0b = stringB.toInt();
scene = 0;
Serial1.println("Channel 0 Configured");
BlueRead = "NUL";
}
if (BlueRead.startsWith("CH001")) {
delay(10);
String stringR = BlueRead.substring(BlueRead.indexOf("R") + 1, BlueRead.indexOf("G"));
ch1r = stringR.toInt();
String stringG = BlueRead.substring(BlueRead.indexOf("G") + 1, BlueRead.indexOf("B"));
ch1g = stringG.toInt();
String stringB = BlueRead.substring(BlueRead.indexOf("B") + 1, BlueRead.indexOf("E"));
ch1b = stringB.toInt();
scene = 0;
Serial1.println("Channel 1 Configured");
BlueRead = "NUL";
}
if (BlueRead.startsWith("CH002")) {
delay(10);
String stringR = BlueRead.substring(BlueRead.indexOf("R") + 1, BlueRead.indexOf("G"));
ch2r = stringR.toInt();
String stringG = BlueRead.substring(BlueRead.indexOf("G") + 1, BlueRead.indexOf("B"));
ch2g = stringG.toInt();
String stringB = BlueRead.substring(BlueRead.indexOf("B") + 1, BlueRead.indexOf("E"));
ch2b = stringB.toInt();
scene = 0;
Serial1.println("Channel 2 Configured");
BlueRead = "NUL";
}
if (BlueRead.startsWith("CH003")) {
delay(10);
String stringR = BlueRead.substring(BlueRead.indexOf("R") + 1, BlueRead.indexOf("G"));
ch3r = stringR.toInt();
String stringG = BlueRead.substring(BlueRead.indexOf("G") + 1, BlueRead.indexOf("B"));
ch3g = stringG.toInt();
String stringB = BlueRead.substring(BlueRead.indexOf("B") + 1, BlueRead.indexOf("E"));
ch3b = stringB.toInt();
scene = 0;
Serial1.println("Channel 3 Configured");
BlueRead = "NUL";
}
if (BlueRead.startsWith("CH004")) {
delay(10);
String stringR = BlueRead.substring(BlueRead.indexOf("R") + 1, BlueRead.indexOf("G"));
ch4r = stringR.toInt();
String stringG = BlueRead.substring(BlueRead.indexOf("G") + 1, BlueRead.indexOf("B"));
ch4g = stringG.toInt();
String stringB = BlueRead.substring(BlueRead.indexOf("B") + 1, BlueRead.indexOf("E"));
ch4b = stringB.toInt();
scene = 0;
Serial1.println("Channel 4 Configured");
BlueRead = "NUL";
}
if (BlueRead.startsWith("CH005")) {
delay(10);
String stringR = BlueRead.substring(BlueRead.indexOf("R") + 1, BlueRead.indexOf("G"));
ch5r = stringR.toInt();
String stringG = BlueRead.substring(BlueRead.indexOf("G") + 1, BlueRead.indexOf("B"));
ch5g = stringG.toInt();
String stringB = BlueRead.substring(BlueRead.indexOf("B") + 1, BlueRead.indexOf("E"));
ch5b = stringB.toInt();
scene = 1;
Serial1.println("Channel 5 Configured");
BlueRead = "NUL";
}
if (BlueRead.startsWith("CH006")) {
delay(10);
String stringR = BlueRead.substring(BlueRead.indexOf("R") + 1, BlueRead.indexOf("G"));
ch6r = stringR.toInt();
String stringG = BlueRead.substring(BlueRead.indexOf("G") + 1, BlueRead.indexOf("B"));
ch6g = stringG.toInt();
String stringB = BlueRead.substring(BlueRead.indexOf("B") + 1, BlueRead.indexOf("E"));
ch6b = stringB.toInt();
scene = 0;
Serial1.println("Channel 6 Configured");
BlueRead = "NUL";
}
if (BlueRead.startsWith("CH007")) {
delay(10);
String stringR = BlueRead.substring(BlueRead.indexOf("R") + 1, BlueRead.indexOf("G"));
ch7r = stringR.toInt();
String stringG = BlueRead.substring(BlueRead.indexOf("G") + 1, BlueRead.indexOf("B"));
ch7g = stringG.toInt();
String stringB = BlueRead.substring(BlueRead.indexOf("B") + 1, BlueRead.indexOf("E"));
ch7b = stringB.toInt();
scene = 0;
Serial1.println("Channel 7 Configured");
BlueRead = "NUL";
}
if (BlueRead.startsWith("CH008")) {
delay(10);
String stringR = BlueRead.substring(BlueRead.indexOf("R") + 1, BlueRead.indexOf("G"));
ch8r = stringR.toInt();
String stringG = BlueRead.substring(BlueRead.indexOf("G") + 1, BlueRead.indexOf("B"));
ch8g = stringG.toInt();
String stringB = BlueRead.substring(BlueRead.indexOf("B") + 1, BlueRead.indexOf("E"));
ch8b = stringB.toInt();
scene = 0;
Serial1.println("Channel 8 Configured");
BlueRead = "NUL";
}
if (BlueRead.startsWith("CH009")) {
delay(10);
String stringR = BlueRead.substring(BlueRead.indexOf("R") + 1, BlueRead.indexOf("G"));
ch9r = stringR.toInt();
String stringG = BlueRead.substring(BlueRead.indexOf("G") + 1, BlueRead.indexOf("B"));
ch9g = stringG.toInt();
String stringB = BlueRead.substring(BlueRead.indexOf("B") + 1, BlueRead.indexOf("E"));
ch9b = stringB.toInt();
scene = 0;
Serial1.println("Channel 9 Configured");
BlueRead = "NUL";
}
if (BlueRead.startsWith("CH010")) {
delay(10);
String stringR = BlueRead.substring(BlueRead.indexOf("R") + 1, BlueRead.indexOf("G"));
ch10r = stringR.toInt();
String stringG = BlueRead.substring(BlueRead.indexOf("G") + 1, BlueRead.indexOf("B"));
ch10g = stringG.toInt();
String stringB = BlueRead.substring(BlueRead.indexOf("B") + 1, BlueRead.indexOf("E"));
ch10b = stringB.toInt();
scene = 0;
Serial1.println("Channel 10 Configured");
BlueRead = "NUL";
}
if (BlueRead.startsWith("CH011")) {
delay(10);
String stringR = BlueRead.substring(BlueRead.indexOf("R") + 1, BlueRead.indexOf("G"));
ch11r = stringR.toInt();
String stringG = BlueRead.substring(BlueRead.indexOf("G") + 1, BlueRead.indexOf("B"));
ch11g = stringG.toInt();
String stringB = BlueRead.substring(BlueRead.indexOf("B") + 1, BlueRead.indexOf("E"));
ch11b = stringB.toInt();
scene = 0;
Serial1.println("Channel 11 Configured");
BlueRead = "NUL";
}
if (BlueRead.startsWith("CH012")) {
delay(10);
String stringR = BlueRead.substring(BlueRead.indexOf("R") + 1, BlueRead.indexOf("G"));
ch12r = stringR.toInt();
String stringG = BlueRead.substring(BlueRead.indexOf("G") + 1, BlueRead.indexOf("B"));
ch12g = stringG.toInt();
String stringB = BlueRead.substring(BlueRead.indexOf("B") + 1, BlueRead.indexOf("E"));
ch12b = stringB.toInt();
scene = 0;
Serial1.println("Channel 12 Configured");
BlueRead = "NUL";
}
if (BlueRead.startsWith("CH013")) {
delay(10);
String stringR = BlueRead.substring(BlueRead.indexOf("R") + 1, BlueRead.indexOf("G"));
ch13r = stringR.toInt();
String stringG = BlueRead.substring(BlueRead.indexOf("G") + 1, BlueRead.indexOf("B"));
ch13g = stringG.toInt();
String stringB = BlueRead.substring(BlueRead.indexOf("B") + 1, BlueRead.indexOf("E"));
ch13b = stringB.toInt();
scene = 0;
Serial1.println("Channel 13 Configured");
BlueRead = "NUL";
}
if (BlueRead.startsWith("CH014")) {
delay(10);
String stringR = BlueRead.substring(BlueRead.indexOf("R") + 1, BlueRead.indexOf("G"));
ch14r = stringR.toInt();
String stringG = BlueRead.substring(BlueRead.indexOf("G") + 1, BlueRead.indexOf("B"));
ch14g = stringG.toInt();
String stringB = BlueRead.substring(BlueRead.indexOf("B") + 1, BlueRead.indexOf("E"));
ch14b = stringB.toInt();
scene = 0;
Serial1.println("Channel 14 Configured");
BlueRead = "NUL";
}
if (BlueRead.startsWith("CH015")) {
delay(10);
String stringR = BlueRead.substring(BlueRead.indexOf("R") + 1, BlueRead.indexOf("G"));
ch15r = stringR.toInt();
String stringG = BlueRead.substring(BlueRead.indexOf("G") + 1, BlueRead.indexOf("B"));
ch15g = stringG.toInt();
String stringB = BlueRead.substring(BlueRead.indexOf("B") + 1, BlueRead.indexOf("E"));
ch15b = stringB.toInt();
scene = 0;
Serial1.println("Channel 15 Configured");
BlueRead = "NUL";
}
if (BlueRead.startsWith("9998")) {
scene = 1;
Serial1.println("Chase Started");
BlueRead = "NUL";
}
if (BlueRead.startsWith("9997")) {
scene = 2;
Serial1.println("Cycle Started");
BlueRead = "NUL";
}
// ****** Set Channel Colors ****************************************************************************************
if (scene == 0) {
SET_TLC5974_Mod (ch0r, ch0g, ch0b, 000, 100);
SET_TLC5974_Mod (ch1r, ch1g, ch1b, 001, 100);
SET_TLC5974_Mod (ch2r, ch2g, ch2b, 002, 100);
SET_TLC5974_Mod (ch3r, ch3g, ch3b, 003, 100);
SET_TLC5974_Mod (ch4r, ch4g, ch4b, 004, 100);
SET_TLC5974_Mod (ch5r, ch5g, ch5b, 005, 100);
SET_TLC5974_Mod (ch6r, ch6g, ch6b, 006, 100);
SET_TLC5974_Mod (ch7r, ch7g, ch7b, 007, 100);
SET_TLC5974_Mod (ch8r, ch8g, ch8b, 8, 100);
SET_TLC5974_Mod (ch9r, ch9g, ch9b, 9, 100);
SET_TLC5974_Mod (ch10r, ch10g, ch10b, 010, 100);
SET_TLC5974_Mod (ch11r, ch11g, ch11b, 011, 100);
SET_TLC5974_Mod (ch12r, ch12g, ch12b, 012, 100);
SET_TLC5974_Mod (ch13r, ch13g, ch13b, 013, 100);
SET_TLC5974_Mod (ch14r, ch14g, ch14b, 014, 100);
SET_TLC5974_Mod (ch15r, ch15g, ch15b, 015, 100);
scene = 999;
}
// chase Scene
if (scene == 1) {
for (chase=0; chase<10; chase++){
SET_TLC5974_Mod (0, 4095, 0, chase, 200); // Call Channel 2 LED Control
}
delay(1000);
for (chase=0; chase<10; chase++){
SET_TLC5974_Mod (0, 0, 4095, chase, 100); // Call Channel 2 LED Control
}
chase = 0;
}
if (scene == 2) {
for (chase=0; chase<10; chase++){
SET_TLC5974_Mod (0, 4095, 0, chase, 200); // Call Channel 2 LED Control
SET_TLC5974_Mod (0, 0, 0, chase, 200); // Call Channel 2 LED Control
}
}
} // ****** End of void Loop ************************************************************************************
// ******* LED Control Section **********************************************************************************
void SET_TLC5974_Mod (uint16_t r, uint16_t g, uint16_t b, uint8_t ch, uint32_t wait) {
tlc.setLED(ch, r, g, b);
tlc.write();
delay(wait); // Stops the Flickering
}
// ****** Blue Tooth Read ****************************************************************************************
//****** Turn Off all Devices ******************************************************************************************************
void Turn_Off(){
ch0r = 0;
ch0g = 0;
ch0b = 0;
ch1r = 0;
ch1g = 0;
ch1b = 0;
ch2r = 0;
ch2g = 0;
ch2b = 0;
ch3r = 0;
ch3g = 0;
ch3b = 0;
ch4r = 0;
ch4g = 0;
ch4b = 0;
ch5r = 0;
ch5g = 0;
ch5b = 0;
ch6r = 0;
ch6g = 0;
ch6b = 0;
ch7r = 0;
ch7g = 0;
ch7b = 0;
ch8r = 0;
ch8g = 0;
ch8b = 0;
ch9r = 0;
ch9g = 0;
ch9b = 0;
ch10r = 0;
ch10g = 0;
ch10b = 0;
ch11r = 0;
ch11g = 0;
ch11b = 0;
ch12r = 0;
ch12g = 0;
ch12b = 0;
ch13r = 0;
ch13g = 0;
ch13b = 0;
ch14r = 0;
ch14g = 0;
ch14b = 0;
ch15r = 0;
ch15g = 0;
ch15b = 0;
ch16r = 0;
ch16g = 0;
ch16b = 0;
ch17r = 0;
ch17g = 0;
ch17b = 0;
ch18r = 0;
ch18g = 0;
ch18b = 0;
ch19r = 0;
ch19g = 0;
ch19b = 0;
ch20r = 0;
ch20g = 0;
ch20b = 0;
ch21r = 0;
ch21g = 0;
ch21b = 0;
ch22r = 0;
ch22g = 0;
ch22b = 0;
ch23r = 0;
ch23g = 0;
ch23b = 0;
ch24r = 0;
ch24g = 0;
ch24b = 0;
ch25r = 0;
ch25g = 0;
ch25b = 0;
ch26r = 0;
ch26g = 0;
ch26b = 0;
ch27r = 0;
ch27g = 0;
ch27b = 0;
ch28r = 0;
ch28g = 0;
ch28b = 0;
ch29r = 0;
ch29g = 0;
ch29b = 0;
ch30r = 0;
ch30g = 0;
ch30b = 0;
ch31r = 0;
ch31g = 0;
ch31b = 0;
}
Code: Select all
// 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
#define BLE_READPACKET_TIMEOUT 500 // Timeout in ms waiting to read a response
// 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 8
#define BLUEFRUIT_SPI_IRQ 7
#define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused