ESP32 repetitive data

Hello everyone
I send X command to ESP32 over BT with Android application, but it keeps repeating as if sending this command

post your code using code tags </> plus output of serial monitor and the Android

#include <BluetoothSerial.h>
#include <EEPROM.h>

BluetoothSerial SerialBT;
byte BTData;

int kalanzaman;
const int tetik = 22; 
const int role = 27; 
int buttonState = 0;
int verilenhak = 500;


#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

void setup()
{

  Serial.begin(115200);
  SerialBT.begin("AirSoft Silah Kontrol");
  Serial.println("AirSoft Silah Kontrol v1");
  Serial.println("Bluetooth baslatildi, Baglanmaya hazir...");
  pinMode(tetik, INPUT);
  pinMode(role, OUTPUT);
  EEPROM.read(5);
}

void loop(){


if(SerialBT.available())
{
BTData = SerialBT.read();
}


  if(BTData == '1')
{
digitalWrite(role, HIGH);
Serial.println("High");

}
 
  if(BTData == '0')
{
digitalWrite(role, LOW);
Serial.println("Low");
}


 if (digitalRead(tetik) == LOW){
verilenhak --;
EEPROM.write(5,verilenhak);
Serial.println(verilenhak);
}

if (verilenhak < 10){
   SerialBT.write('K');
}

 if (verilenhak < 1){
  EEPROM.write(5,0);
  digitalWrite(role, LOW);
  Serial.println("Bitti");
  SerialBT.write('X');
}



   if(BTData == '2')
{
 verilenhak = 20;
Serial.println("20 Atis");
EEPROM.write(5,verilenhak);
}


   if (BTData == '3')
{
verilenhak = 30;
Serial.println("30 Atis");
EEPROM.write(5,verilenhak);
}


   if(BTData == '4')
{
verilenhak = 40;
Serial.println("40 Atis");
EEPROM.write(5,verilenhak);
}

   if(BTData == '5')
{
verilenhak = 50;
Serial.println("50 Atis");
EEPROM.write(5,verilenhak);
}

   if(BTData == '6')
{
verilenhak = 60;
Serial.println("60 Atis");
EEPROM.write(5,verilenhak);
}

   if(BTData == '7')
{
verilenhak = 70;
Serial.println("70 Atis");
EEPROM.write(5,verilenhak);
}

   if(BTData == '8')
{
verilenhak = 80;
Serial.println("80 Atis");
EEPROM.write(5,verilenhak);
}

   if(BTData == '9')
{
verilenhak = 90;
Serial.println("90 Atis");
EEPROM.write(5,verilenhak);
}

}

think at the start of loop() you need to clear the value of BTData, e.g.

void loop(){
BTData=' ';

otherwise it retains the previous value received and executes the corresponding if() again

1 Like

Use C++ Switch instead if's. And clear the BTData with each iteration of the loop and before reading the new value.

1 Like

thank you very much

and the button is constantly spinning, it works as if it was pressed all the time.

is the button press code

 if (digitalRead(tetik) == LOW){

what should happen if the value read is HIGH?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.