(SOLVED) Does anybody see anything wrong with this code?

Like the title says it: Does anybody see anything wrong with this code?

the problem I have is when I send AT to the BT module it responds OK 11 times and it won't respond to other AT commands

I need help maybe I'm coding it wrong or my TX pin is bad I'm using Arduino Leonardo by the way o and also the TX led gets stuck on when responding Ok 11 times to the AT command

I have tried diffent brand new BT Modules and they do the same so I'm guessing it's either the code or the Leonardo itself ):

char blueToothVal[2]; //value sent over via bluetooth
char ArduValue[2]; //stores last state of device (on/off)

void setup()
{
Serial.begin(9600);
while(!Serial);
Serial.println("Serial Ready");

Serial1.begin(38400);
while(!Serial1);
Serial.println("Serial1 Ready");
}

void loop()
{

if (Serial.available())
{ //if there is data being recieved
ArduValue[0] = Serial.read(); //reads from Arduino and sends it to BLuetooth
// Serial.println(ArduValue);
// Serial.write( ArduValue);
Serial1.write(ArduValue[0]);

}

if (Serial1.available())
{ //if there is data being recieved
blueToothVal[0] = Serial1.read(); //reads from Arduino and sends it to BLuetooth
//Serial.println(ArduValue);
//Serial.print(blueToothVal);
Serial.println(blueToothVal[0]);
//Serial.println(blueToothVal);
}

delay(100);

}

You probably want to use print not write

Are talking about this line??>>> Serial1.write(ArduValue[0]);

I've just changed from Serial1.write(ArduValue[0]); to Serial1.print(ArduValue[0]); and it's doing the samething multiple OK replies ):

Does it make any difference if you remove the 100mS delay?
Are you sending commands using the serial monitor?

what BT module do you use?

I Just changed from Serial1.print(ArduValue[0]); to Serial1.println(ArduValue[0]); and it gave me that error down below

Serial Ready
Serial1 Ready
E
R
R
O
R
:
(
0
)

E
R
R
O
R
:
(
0
)

The Bluetooth modules that I use (HC05) use 9600 as the default baud rate.

Using the BT HC-05

38400 is the baud rate for the AT mode of the HC05. Unless you have changed it the default communication baud rate is usually 9600.

Yes I'm send it tru Serial monitor

When you make changes to code, please post the new version so that we can keep up.

That is because you are sending A followed by a carriage return and a newline then T followed by another carriage return and newline when you use println.

So the documentation says you need to send command as

AT+WHATEVER\r\n

Hence you send it as a sting not as byte, using print or println

The OP is sending the command over Serial (presumably from the serial monitor) and transferring each character individually to Serial1 (Tx and Rx pins on the Leonardo) as they are received. I don't really see any problems with the code, other than the odd use of a two-element array when only a char is needed.

I commented the //delay(100); line and now I get this when I send AT

O
K

Serial.println(blueToothVal[0]);

Change this to Serial.print instead of Serial.println if you want the complete response on a single line.

Now thanks your advice I get one OK when I send AT
now I need to check other AT Commands

Ok Guys I got it solved thanks to everyone for the help many thanks

and this is the last code that worked:

type or paste char blueToothVal[2];           //value sent over via bluetooth
char ArduValue[2];              //stores last state of device (on/off)
 
void setup() 
{
 Serial.begin(9600); 
 while(!Serial);
 Serial.println("Serial Ready");
 
 Serial1.begin(38400); 
 while(!Serial1);
 Serial.println("Serial1 Ready");
 //Serial.println("Serial Ready");
 //Serial1.begin(9600);
}
 
 
void loop()
{

  if (Serial.available())
    { //if there is data being recieved
      ArduValue[0] = Serial.read(); //reads from Arduino and sends it to BLuetooth
    // Serial.println(ArduValue);
   // Serial.write( ArduValue);
     Serial1.write(ArduValue[0]);
     
    }
    
  if (Serial1.available())
    { //if there is data being recieved
      blueToothVal[0] = Serial1.read(); //reads from Arduino and sends it to BLuetooth
     //Serial.println(ArduValue);
    //Serial.print(blueToothVal);
     Serial.print(blueToothVal[0]);
     //Serial.println(blueToothVal);
    }

 
  //delay(100);
  

} here

This one dows work too with the Serial1.print(ArduValue[0]); line

type or paste char blueToothVal[2];           //value sent over via bluetooth
char ArduValue[2];              //stores last state of device (on/off)
 
void setup() 
{
 Serial.begin(9600); 
 while(!Serial);
 Serial.println("Serial Ready");
 
 Serial1.begin(38400); 
 while(!Serial1);
 Serial.println("Serial1 Ready");
 //Serial.println("Serial Ready");
 //Serial1.begin(9600);
}
 
 
void loop()
{

  if (Serial.available())
    { //if there is data being recieved
      ArduValue[0] = Serial.read(); //reads from Arduino and sends it to BLuetooth
    // Serial.println(ArduValue);
   // Serial.write( ArduValue);
     Serial1.print(ArduValue[0]);
     
    }
    
  if (Serial1.available())
    { //if there is data being recieved
      blueToothVal[0] = Serial1.read(); //reads from Arduino and sends it to BLuetooth
     //Serial.println(ArduValue);
    //Serial.print(blueToothVal);
     Serial.print(blueToothVal[0]);
     //Serial.println(blueToothVal);
    }

 
  //delay(100);
  

} here