Serial.read() problem

Hi, i'm trying to find a solution to my problem with serial communicationvia arduino.
First of all i want to communicate and automate this process with bt module.

while(btModule.available()>0){
  if (Serial.available()) {
    static uint8_t choice = Serial.read();
    char x = "AT+";
        if (wybor == 1)  {
      z = btModule.read(); 
      btModule.print((char)Serial.read());
       choice=0;
      }
    }
  }

Code i wrote. And i want to do is press button on keyboard and bt module will answer to AT commands.

Welcome to the forum

We will need more information in order to help

Start by posting the full sketch

Which Arduino board are you using ?

char x = "AT+";

There is an obvious problem here because a char variable can only hold a single character

So I'm using arduino uno.

#include "Arduino.h"
#include "SoftwareSerial.h" 
#define DPIN_BLUESRL_RX 2  
#define DPIN_BLUESRL_TX 3  

SoftwareSerial btModule(DPIN_BLUESRL_RX, DPIN_BLUESRL_TX);  

void setup() {

  Serial.begin(115200);  
  Serial.println("Bluetooth works);
  btModule.begin(115200);  

void loop() {
  static uint8_t menuPrinted = 0;

  if (0 == menuPrinted) {
    menuPrinted = 1;
    Serial.println("1 - Scan bt devices");
  }
  int x= Serial.read();

  /**/
  while(btModule.available()>0){
  if (Serial.available()) {
    static uint8_t choice = Serial.read();
    char x = "AT+";
        if (choice == 1)  {
      z = btModule.read(); 
      btModule.print((char)Serial.read());
       choice=0;
      }
    }
  }
} 

That's the whole code. Im trying to understand serial.read() but its not going well.

The code looks the mess. Could you please explain it line by line?

SoftwareSerial btModule(DPIN_BLUESRL_RX, DPIN_BLUESRL_TX);

The pin numbers for SoftwareSerial are Rx pin then Tx pin. The BT module Rx pin should be connected to the serial Tx pin and vice versa.

Have you got the BT module connected correctly ?

suggest you try using readBytesUntil()

// -----------------------------------------------------------------------------
void
loop ()
{
    if (Serial.available ())  {
        char buf[10];
        int  n  = Serial.readBytesUntil ('\n', buf, sizeof(buf)-1);
        buf [n] = '\0';         // terminate with nul

        Serial.println (buf);
    }
}

// -----------------------------------------------------------------------------
void
setup (void)
{
    Serial.begin (9600);
}

Yes i am. I've tested if the serial communication works and bt module answer to my AT commands.

The code you posted in the first message not looks as the part of your "full code" posted later.

For example - the tested variable now the choice rather than wybor in the first code.

What version do you actually load into the board ?

 int x= Serial.read();

and

char x = "AT+";

Even if a char could hold multiple characters things could get very messy if you use the same variable name for two different variables

Just noticed something else.
Where is the closing brace for the code in setup() ?

Why use this specific?
I'm a begginer (yes you can easily tell that) and don't know why this if bt module return char.

Oh, sorry

int x = Serial.read();

its just a typo that i was to delete earlier

Do you understand the difference between 1, '1' and "1" ? What value do you sending to the Serial when making a "choice" ?

I was copying in a parts, the code is in mess and a lot of comments. The closing brace for the code in setup() is, i just didnt catch that in

"1" is string and '1' is character

It is not a good idea.

Copy the code to the empty arduino sketch, fix the typos and try to compile.
ONLY post the code to the forum if it compiled fine.

If you don't post the code that you are actually using it is difficult to help

Please post your actual code in a new reply. DO NOT edit your previous reply and post it there

1 Like

and what is 1 in your line below?

to be honest i dont really know what i was thinking in this part of code

static uint8_t choice = Serial.read();

if (choice == 1)  {

Is it means that now you changed your mind about these lines? - if so, edit them.

#include "Arduino.h"
#include "SoftwareSerial.h"

#define DPIN_BLUESRL_RX 2
#define DPIN_BLUESRL_TX 3

SoftwareSerial btModule(DPIN_BLUESRL_RX, DPIN_BLUESRL_TX);

void setup() {

  Serial.begin(115200);
  Serial.println("Bluetooth works");
  btModule.begin(115200);
}

void loop() {
  static uint8_t menuPrinted = 0;

  if (0 == menuPrinted) {
    menuPrinted = 1;
    Serial.println("1 - Scan bt devices");
  }

  while (btModule.available() > 0) {
    if (Serial.available()) {
      static uint8_t choice = Serial.read();
      char x = "AT+";
      if (choice == 1) { //in my mind i wanted to press 1 and arduino will read my choice and send it to bt module
        int z = btModule.read();
        btModule.print((char)Serial.read());
        choice = 0;
      }
    }
  }
}