Hi Arduino Friends,
After several years of Arduino Nano development and hobby project creation, decided to move to the 33BLE in order to take advantage of the compact Bluetooth solution (previously used HC-06 etc).
My project utilises a simple ‘Unity 3D’ created app to communicate with the Nano33BLE in order to drive a SparkFun TB6612FNG motor driver – so far so good, working well.
I now want to introduce a DFPlayer Mini MP3 module to play sounds at different motor speeds; I have attempted connecting the MP3 module using Serial1 and (as you can see commented out in my sketch) a custom UART serial port.
My issue is, in both attempts, the BLE connects fine but when I select a motor speed the motor spins up, the sound starts to play but then the Nano33BLE immediately crashes and its game over – reset everything and start again.
Question: have I connected the DFPlayer correctly (physically it should be OK, TX to RX/Rx to TX etc and all running on 3.3v)? Feels like there is a conflict between the BLE and the DFPlayer connection?
I’ve had all kit running correctly using the older Nano and HC-06 etc so no issues there.
Thanks in advance!
James.
// BLE
#include <ArduinoBLE.h>
BLEService trainService("180A");
BLEByteCharacteristic motionCharacteristic("2A57", BLERead | BLEWrite);
// MOTOR - PINS
int motorAIN1 = 2;
int motorAIN2 = 3;
int motorSTBY = 4;
int motorPWM = 5;
// MOTOR - PWM SPEEDS
int motorSTOPpwm = 0;
int motorSLOWpwm = 145;
int motorHALFpwm = 200;
int motorFASTpwm = 255;
// DFPLAYER
#include <DFRobotDFPlayerMini.h>
DFRobotDFPlayerMini myDFPlayer;
//UART myDFSerial (digitalPinToPinName(12), digitalPinToPinName(11), NC, NC);
// MP3
// 1 - BRAKE
// 2 - CHUG SLOW
// 3 - CHUG HALF
// 4 - CHUG FAST
// ADVERT
// 1 - WHISTLE
// 2 - ALL ABOARD
// 3 - MERRY XMAS
void setup()
{
// MOTOR SETUP PINMODES
pinMode(motorSTBY, OUTPUT);
pinMode(motorAIN1, OUTPUT);
pinMode(motorSTBY, OUTPUT);
pinMode(motorPWM, OUTPUT);
// MOTOR SETUP DIGITALWRITES
digitalWrite(motorSTBY, HIGH);
// BRAKE MOTOR
Brake();
// DFPLAYER SERIAL
//myDFSerial.begin(9600);
Serial1.begin(9600);
// DFPLAYER SETTINGS
//myDFPlayer.begin(myDFSerial);
myDFPlayer.begin(Serial1);
myDFPlayer.volume(30);
// SET PIN MODES & WRITE FOR CONNECTION LED (ONBOARD)
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
// INITIALISE
if (!BLE.begin())
{
while (1);
}
// SET NAME
BLE.setLocalName("Nano33BLE");
// ADVERSTISE SERVICE
BLE.setAdvertisedService(trainService);
// ADD CHARACTERISTICS TO SERVICE
trainService.addCharacteristic(motionCharacteristic);
// ADD SERVICE
BLE.addService(trainService);
// WRITE DEFAULT VALUES
motionCharacteristic.writeValue(0);
// ADVERTISE
BLE.advertise();
}
void loop()
{
// LISTEN FOR BLE PERIPHERALS TO CONNECT
BLEDevice central = BLE.central();
// IF A CENTRAL IS CONNECTED TO PERIPHERAL
if (central)
{
// WRITE CONNECTION LED ON
digitalWrite(LED_BUILTIN, HIGH);
// WHILE THE CENTRAL IS STILL CONNECTED
while (central.connected())
{
// IF motionCharacteristic WRITTEN TO
if (motionCharacteristic.written())
{
switch (motionCharacteristic.value())
{
case 01:
// FORWARDS / SLOW
digitalWrite(motorAIN1, HIGH);
digitalWrite(motorAIN2, LOW);
analogWrite(motorPWM, motorSLOWpwm);
soundChug();
break;
case 02:
// FORWARDS / HALF
digitalWrite(motorAIN1, HIGH);
digitalWrite(motorAIN2, LOW);
analogWrite(motorPWM, motorHALFpwm);
break;
case 03:
// FORWARDS / FAST
digitalWrite(motorAIN1, HIGH);
digitalWrite(motorAIN2, LOW);
analogWrite(motorPWM, motorFASTpwm);
break;
case 04:
// BACKWARDS / SLOW
digitalWrite(motorAIN1, LOW);
digitalWrite(motorAIN2, HIGH);
analogWrite(motorPWM, motorSLOWpwm);
break;
case 05:
// BACKWARDS / HALF
digitalWrite(motorAIN1, LOW);
digitalWrite(motorAIN2, HIGH);
analogWrite(motorPWM, motorHALFpwm);
break;
case 06:
// BACKWARDS / FAST
digitalWrite(motorAIN1, LOW);
digitalWrite(motorAIN2, HIGH);
analogWrite(motorPWM, motorFASTpwm);
break;
default:
Brake();
break;
}
}
}
}
else
{
// DISCONNECTED
// WRITE CONNECTION LED OFF
digitalWrite(LED_BUILTIN, LOW);
// BRAKE MOTOR
Brake();
}
}
void Brake()
{
// BRAKE MOTOR
digitalWrite(motorAIN1, LOW);
digitalWrite(motorAIN2, LOW);
analogWrite(motorPWM, motorSTOPpwm);
}
void soundChug()
{
myDFPlayer.play(5);
}