Hey guys I am very new to coding arduino. I am working on a little project using an ESP32 Bluetooth to trigger LED's and a sound via serial MP3.
My code is a mess of cobbled together code from around the internet. It is working a little and just trying to get some further guidance to make it work how I want it to fully.
The bluetooth is working. I am using an android serial bluetooth app to send signals to the ESP32 and it triggers it.
The issue I have is it's not doing what I expect/want with my code. I have 6 WS2811 LED's that I want to trigger a certain way.
For example "If BTData=1" I just want it to trigger the first 3 LED's.
"If BTData=2" I want the last 3 LED's triggered.
"If BTData=3" I want all 6 LED's triggered.
"If BTData=4" I want them to cascade from 1-6.
But when I send 1,2, or 3 it just triggers all 6 LED's. When I send 4 instead of cascading 1 through 6, it flashes 6 times.
The other issue is I am getting nothing from the MP3. I am trying to trigger a sound with the LED's. I only have one sound that I need triggered and I have just named it 001.mp3 and it's in the root folder of the SD Card. I have tried using different pins but I am not getting anything from that at this point.
Any help is greatly appreciated. Here is the code I am working with:
#include <BluetoothSerial.h>
#include "FastLED.h"
#include <SoftwareSerial.h>
#define ARDUINO_RX RX//should connect to TX of the Serial MP3 Player module
#define ARDUINO_TX TX//connect to RX of the module
SoftwareSerial mySerial(ARDUINO_RX, ARDUINO_TX);//init the serial protocol, tell to myserial wich pins are TX and RX
static int8_t Send_buf[8] = {0} ;//The MP3 player undestands orders in a 8 int string
//0X7E FF 06 command 00 00 00 EF;(if command =01 next song order)
#define NEXT_SONG 0X01
#define PREV_SONG 0X02
#define CMD_PLAY_W_INDEX 0X03 //DATA IS REQUIRED (number of song)
#define VOLUME_UP_ONE 0X04
#define VOLUME_DOWN_ONE 0X05
#define CMD_SET_VOLUME 0X1E//DATA IS REQUIRED (number of volume from 0 up to 30(0x1E))
#define SET_DAC 0X17
#define CMD_PLAY_WITHVOLUME 0X22 //data is needed 0x7E 06 22 00 xx yy EF;(xx volume)(yy number of song)
#define CMD_SEL_DEV 0X09 //SELECT STORAGE DEVICE, DATA IS REQUIRED
#define DEV_TF 0X02 //HELLO,IM THE DATA REQUIRED
#define SLEEP_MODE_START 0X0A
#define SLEEP_MODE_WAKEUP 0X0B
#define CMD_RESET 0X0C//CHIP RESET
#define CMD_PLAY 0X0D //RESUME PLAYBACK
#define CMD_PAUSE 0X0E //PLAYBACK IS PAUSED
#define CMD_PLAY_WITHFOLDER 0X0F//DATA IS NEEDED, 0x7E 06 0F 00 01 02 EF;(play the song with the directory \01\002xxxxxx.mp3
#define STOP_PLAY 0X16
#define PLAY_FOLDER 0X17// data is needed 0x7E 06 17 00 01 XX EF;(play the 01 folder)(value xx we dont care)
#define SET_CYCLEPLAY 0X19//data is needed 00 start; 01 close
#define SET_DAC 0X17//data is needed 00 start DAC OUTPUT;01 DAC no output
#define NUM_LEDS 7
// Which pin are we using on the arduino to send data to the strip?
#define BRIGHTNESS 255
// Create a variable that will hold the current state of each of our LEDs.
CRGB leds[NUM_LEDS];
#define ledPIN 21
#define ledPIN 22
#define ledPIN 23
#define ledPIN 25
#define ledPIN 26
#define ledPIN 27
BluetoothSerial SerialBT;
byte BTData;
/* Check if Bluetooth configurations are enabled in the SDK */
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
void sendCommand(int8_t command, int16_t dat)
{
delay(20);
Send_buf[0] = 0x7e; //starting byte
Send_buf[1] = 0xff; //version
Send_buf[2] = 0x06; //the number of bytes of the command without starting byte and ending byte
Send_buf[3] = command; //
Send_buf[4] = 0x00;//0x00 = no feedback, 0x01 = feedback
Send_buf[5] = (int8_t)(dat >> 8);//datah
Send_buf[6] = (int8_t)(dat); //datal
Send_buf[7] = 0xef; //ending byte
for(uint8_t i=0; i<8; i++)//
{
mySerial.write(Send_buf[i]) ;//send bit to serial mp3
Serial.print(Send_buf[i],HEX);//send bit to serial monitor in pc
}
Serial.println();
}
void setup()
{
pinMode(ledPIN, OUTPUT);
Serial.begin(115200);
SerialBT.begin("BT-1");
Serial.println("Bluetooth Started! Ready to pair...");
delay(20);//Wait chip initialization is complete
sendCommand(CMD_SEL_DEV, DEV_TF);//select the TF card
delay(200);//wait for 200ms
}
void loop()
{
if(SerialBT.available())
{
BTData = SerialBT.read();
Serial.write(BTData);
}
/* If received Character is 1, then turn ON the LED */
/* You can also compare the received data with decimal equivalent */
/* 48 for 0 and 49 for 1 */
/* if(BTData == 48) or if(BTData == 49) */
if(BTData == '1')
{
digitalWrite(21, HIGH);LEDS.addLeds<WS2811, GPIO_NUM_21, GRB>(leds,NUM_LEDS);
fill_solid(leds, NUM_LEDS, CRGB::Red);
// Turn on all of the LEDs
FastLED.show();
// wait for a bit so we can see them.
delay(100);
digitalWrite(22, HIGH);LEDS.addLeds<WS2811, GPIO_NUM_22, GRB>(leds,NUM_LEDS);
fill_solid(leds, NUM_LEDS, CRGB::Red);
// Turn on all of the LEDs
FastLED.show();
// wait for a bit so we can see them.
delay(100);
digitalWrite(23, HIGH);LEDS.addLeds<WS2811, GPIO_NUM_23, GRB>(leds,NUM_LEDS);
fill_solid(leds, NUM_LEDS, CRGB::Red);
// Turn on all of the LEDs
FastLED.show();
// wait for a bit so we can see them.
delay(100);
sendCommand(CMD_PLAY_WITHVOLUME, 0X1E01);//play the first song with volume 30 class
delay(100);//the programm will send the play option each .1 seconds to the catalex chip
// Set the state of all of the leds to black (off)
fill_solid(leds, NUM_LEDS, CRGB::Black);
// Turn on all of the LEDs
FastLED.show();
// wait for a bit so we can see them.
delay(500);
}
if(BTData == '2')
{
digitalWrite(25, HIGH);LEDS.addLeds<WS2811, GPIO_NUM_25, GRB>(leds,NUM_LEDS);
fill_solid(leds, NUM_LEDS, CRGB::Red);
// Turn on all of the LEDs
FastLED.show();
// wait for a bit so we can see them.
delay(100);
digitalWrite(26, HIGH);LEDS.addLeds<WS2811, GPIO_NUM_26, GRB>(leds,NUM_LEDS);
fill_solid(leds, NUM_LEDS, CRGB::Red);
// Turn on all of the LEDs
FastLED.show();
// wait for a bit so we can see them.
delay(100);
digitalWrite(27, HIGH);LEDS.addLeds<WS2811, GPIO_NUM_27, GRB>(leds,NUM_LEDS);
fill_solid(leds, NUM_LEDS, CRGB::Red);
// Turn on all of the LEDs
FastLED.show();
// wait for a bit so we can see them.
delay(100);
sendCommand(CMD_PLAY_WITHVOLUME, 0X1E01);//play the first song with volume 30 class
delay(100);//the programm will send the play option each .1 seconds to the catalex chip
// Set the state of all of the leds to black (off)
fill_solid(leds, NUM_LEDS, CRGB::Black);
// Turn on all of the LEDs
FastLED.show();
// wait for a bit so we can see them.
delay(500);
}
if(BTData == '3')
{
digitalWrite(21, HIGH);LEDS.addLeds<WS2811, GPIO_NUM_21, GRB>(leds,NUM_LEDS);
fill_solid(leds, NUM_LEDS, CRGB::Red);
// Turn on all of the LEDs
FastLED.show();
// wait for a bit so we can see them.
delay(100);
digitalWrite(22, HIGH);LEDS.addLeds<WS2811, GPIO_NUM_22, GRB>(leds,NUM_LEDS);
fill_solid(leds, NUM_LEDS, CRGB::Red);
// Turn on all of the LEDs
FastLED.show();
// wait for a bit so we can see them.
delay(100);
digitalWrite(23, HIGH);LEDS.addLeds<WS2811, GPIO_NUM_23, GRB>(leds,NUM_LEDS);
fill_solid(leds, NUM_LEDS, CRGB::Red);
// Turn on all of the LEDs
FastLED.show();
// wait for a bit so we can see them.
delay(100);
digitalWrite(25, HIGH);LEDS.addLeds<WS2811, GPIO_NUM_25, GRB>(leds,NUM_LEDS);
fill_solid(leds, NUM_LEDS, CRGB::Red);
// Turn on all of the LEDs
FastLED.show();
// wait for a bit so we can see them.
delay(100);
digitalWrite(26, HIGH);LEDS.addLeds<WS2811, GPIO_NUM_26, GRB>(leds,NUM_LEDS);
fill_solid(leds, NUM_LEDS, CRGB::Red);
// Turn on all of the LEDs
FastLED.show();
// wait for a bit so we can see them.
delay(100);
digitalWrite(27, HIGH);LEDS.addLeds<WS2811, GPIO_NUM_27, GRB>(leds,NUM_LEDS);
fill_solid(leds, NUM_LEDS, CRGB::Red);
// Turn on all of the LEDs
FastLED.show();
// wait for a bit so we can see them.
delay(100);
sendCommand(CMD_PLAY_WITHVOLUME, 0X1E01);//play the first song with volume 30 class
delay(100);//the programm will send the play option each .1 seconds to the catalex chip
// Set the state of all of the leds to black (off)
fill_solid(leds, NUM_LEDS, CRGB::Black);
// Turn on all of the LEDs
FastLED.show();
// wait for a bit so we can see them.
delay(500);
}
if(BTData == '4')
{
digitalWrite(21, HIGH);LEDS.addLeds<WS2811, GPIO_NUM_21, GRB>(leds,NUM_LEDS);
fill_solid(leds, NUM_LEDS, CRGB::Red);
// Turn on all of the LEDs
FastLED.show();
// wait for a bit so we can see them.
delay(100);
sendCommand(CMD_PLAY_WITHVOLUME, 0X1E01);//play the first song with volume 30 class
delay(100);//the programm will send the play option each .1 seconds to the catalex chip
// Set the state of all of the leds to black (off)
fill_solid(leds, NUM_LEDS, CRGB::Black);
// Turn on all of the LEDs
FastLED.show();
// wait for a bit so we can see them.
delay(500);
digitalWrite(22, HIGH);LEDS.addLeds<WS2811, GPIO_NUM_22, GRB>(leds,NUM_LEDS);
fill_solid(leds, NUM_LEDS, CRGB::Red);
// Turn on all of the LEDs
FastLED.show();
// wait for a bit so we can see them.
delay(100);
sendCommand(CMD_PLAY_WITHVOLUME, 0X1E01);//play the first song with volume 30 class
delay(100);//the programm will send the play option each .1 seconds to the catalex chip
// Set the state of all of the leds to black (off)
fill_solid(leds, NUM_LEDS, CRGB::Black);
// Turn on all of the LEDs
FastLED.show();
// wait for a bit so we can see them.
delay(500);
digitalWrite(23, HIGH);LEDS.addLeds<WS2811, GPIO_NUM_23, GRB>(leds,NUM_LEDS);
fill_solid(leds, NUM_LEDS, CRGB::Red);
// Turn on all of the LEDs
FastLED.show();
// wait for a bit so we can see them.
delay(100);
sendCommand(CMD_PLAY_WITHVOLUME, 0X1E01);//play the first song with volume 30 class
delay(100);//the programm will send the play option each .1 seconds to the catalex chip
// Set the state of all of the leds to black (off)
fill_solid(leds, NUM_LEDS, CRGB::Black);
// Turn on all of the LEDs
FastLED.show();
// wait for a bit so we can see them.
delay(500);
digitalWrite(25, HIGH);LEDS.addLeds<WS2811, GPIO_NUM_25, GRB>(leds,NUM_LEDS);
fill_solid(leds, NUM_LEDS, CRGB::Red);
// Turn on all of the LEDs
FastLED.show();
// wait for a bit so we can see them.
delay(100);
sendCommand(CMD_PLAY_WITHVOLUME, 0X1E01);//play the first song with volume 30 class
delay(100);//the programm will send the play option each .1 seconds to the catalex chip
// Set the state of all of the leds to black (off)
fill_solid(leds, NUM_LEDS, CRGB::Black);
// Turn on all of the LEDs
FastLED.show();
// wait for a bit so we can see them.
delay(500);
digitalWrite(26, HIGH);LEDS.addLeds<WS2811, GPIO_NUM_26, GRB>(leds,NUM_LEDS);
fill_solid(leds, NUM_LEDS, CRGB::Red);
// Turn on all of the LEDs
FastLED.show();
// wait for a bit so we can see them.
delay(100);
sendCommand(CMD_PLAY_WITHVOLUME, 0X1E01);//play the first song with volume 30 class
delay(100);//the programm will send the play option each .1 seconds to the catalex chip
// Set the state of all of the leds to black (off)
fill_solid(leds, NUM_LEDS, CRGB::Black);
// Turn on all of the LEDs
FastLED.show();
// wait for a bit so we can see them.
delay(500);
digitalWrite(27, HIGH);LEDS.addLeds<WS2811, GPIO_NUM_27, GRB>(leds,NUM_LEDS);
fill_solid(leds, NUM_LEDS, CRGB::Red);
// Turn on all of the LEDs
FastLED.show();
// wait for a bit so we can see them.
delay(100);
sendCommand(CMD_PLAY_WITHVOLUME, 0X1E01);//play the first song with volume 30 class
delay(100);//the programm will send the play option each .1 seconds to the catalex chip
// Set the state of all of the leds to black (off)
fill_solid(leds, NUM_LEDS, CRGB::Black);
// Turn on all of the LEDs
FastLED.show();
// wait for a bit so we can see them.
delay(500);
}
/* If received Character is 0, then turn OFF the LED */
if(BTData == '0')
{
digitalWrite(ledPIN, LOW);
}
}