Hello, I am new with Arduino, I am trying to activate a relay through a bluetooth controlled app, but every time I press the button on the mobile phone, the relay turns on and off continuously.
This is the code I use:
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
bool doneFlag = false;
SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
/*BLUETOOH*/
SoftwareSerial BT(12,13); // RX, TX
char command;
String string;
/*BLUETOOH*/
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);
# define ACTIVATED LOW
/*RELEVADOR*/
int pinOut = 2;
/*RELEVADOR*/
/*SE ESTABLECE E PIN PARA REALIZAR CADA ACCION, EJEMPLO, BIENVENIDA CON EL PIN 9 DE ARDUINO*/
int btnpuerta = 7;
int btnwelcome = 6;
int btnreload = 8;
void setup()
{
/*SETUP PARA CADA ACCION EN ARDUINO*/
pinMode(btnpuerta, INPUT);
digitalWrite(btnpuerta,HIGH);
pinMode(btnwelcome, INPUT);
digitalWrite(btnwelcome,HIGH);
pinMode(btnreload, INPUT);
digitalWrite(btnreload,HIGH);
/*RELEVADOR*/
pinMode (pinOut, OUTPUT);
/*RELEVADOR*/
Serial.begin(9600);
/*BLUETOOH*/
BT.begin(9600);
/*BLUETOOH*/
mySoftwareSerial.begin(9600);
/*BLUETOOH*/
mySoftwareSerial.listen();
/*BLUETOOH*/
Serial.println();
Serial.println(F("Escanmotor.com"));
Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
if (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3.
Serial.println(F("Unable to begin:"));
Serial.println(F("1.Please recheck the connection!"));
Serial.println(F("2.Please insert the SD card!"));
while(true);
}
Serial.println(F("DFPlayer Mini online."));
myDFPlayer.setTimeOut(500); //Set serial communictaion time out 500ms
//----Set volume----
myDFPlayer.volume(30); //Set volume value (0~30).
myDFPlayer.volumeUp(); //Volume Up
myDFPlayer.volumeDown(); //Volume Down
//----Set different EQ----
myDFPlayer.EQ(DFPLAYER_EQ_NORMAL);
//----Set device we use SD as default----
myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD);
//----Read imformation----
Serial.println(myDFPlayer.readState()); //read mp3 state
Serial.println(myDFPlayer.readVolume()); //read current volume
Serial.println(myDFPlayer.readEQ()); //read EQ setting
Serial.println(myDFPlayer.readFileCounts()); //read all file counts in SD card
Serial.println(myDFPlayer.readCurrentFileNumber()); //read current play file number
Serial.println(myDFPlayer.readFileCountsInFolder(3)); //read file counts in folder SD:/03
}
void loop(){
/*BLUETOOH*/
BT.listen();
delay(50);
if (BT.available() > 0)
{
string = "";
}
// Serial.println(string);
while (BT.available() > 0) {
command = ((byte) BT.read());
Serial.println(command);
if (command == ':')
{
break;
}
else {
string += command;
}
delay(10);
}
mySoftwareSerial.listen();
switch (command) {
case 'P':
reproducir(); // Reproduccion
break;
}
/*BLUETOOH*/
if (digitalRead(btnpuerta) == ACTIVATED) {
myDFPlayer.play(1);
delay(2500);
}
if (digitalRead(btnwelcome) == ACTIVATED) {
if (doneFlag == false) // fragmento para repetir el comando una sola vez
{
myDFPlayer.play (5);
delay (2000);
myDFPlayer.stop ();
doneFlag = true;
}
}
if (digitalRead(btnreload) == ACTIVATED) {
doneFlag == true;
}
}
void reproducir() { // Reproduccion
//Le mandamos la señal de HIGH a la salida del
//Arduino, la salida es el pin 2
digitalWrite (pinOut, HIGH);
//Hacemos un delay de 1 segundo
delay (1000);
digitalWrite (pinOut, LOW);
break;
}
void printDetail(uint8_t type, int value){
switch (type) {
case TimeOut:
Serial.println(F("Time Out!"));
break;
case WrongStack:
Serial.println(F("Stack Wrong!"));
break;
case DFPlayerCardInserted:
Serial.println(F("Card Inserted!"));
break;
case DFPlayerCardRemoved:
Serial.println(F("Card Removed!"));
break;
case DFPlayerCardOnline:
Serial.println(F("Card Online!"));
break;
case DFPlayerUSBInserted:
Serial.println("USB Inserted!");
break;
case DFPlayerUSBRemoved:
Serial.println("USB Removed!");
break;
case DFPlayerPlayFinished:
Serial.print(F("Number:"));
Serial.print(value);
Serial.println(F(" Play Finished!"));
break;
case DFPlayerError:
Serial.print(F("DFPlayerError:"));
switch (value) {
case Busy:
Serial.println(F("Card not found"));
break;
case Sleeping:
Serial.println(F("Sleeping"));
break;
case SerialWrongStack:
Serial.println(F("Get Wrong Stack"));
break;
case CheckSumNotMatch:
Serial.println(F("Check Sum Not Match"));
break;
case FileIndexOut:
Serial.println(F("File Index Out of Bound"));
break;
case FileMismatch:
Serial.println(F("Cannot Find File"));
break;
case Advertise:
Serial.println(F("In Advertise"));
break;
default:
break;
}
break;
default:
break;
}
}
100 / 5,000
Translation results
How can I make it only activate once, every time I press the button on the mobile phone?
very thnks, im a noob