Digital read and bluetooth read

Hello,
i have created a project, with 4 physical buttons and 4 logical buttons (app android with bluetooth).
The code works only with the logical buttons, it seems do not read the status of pins with digital read.
If i disable the "while BTSerial.available() > 0.." instructions, the "digital read" works.

Can anyone help me to understand why it happens?

#include <LiquidCrystal.h>
#include <SoftwareSerial.h>

LiquidCrystal lcd(8, 9, 10, 11, 12, 13);
SoftwareSerial BTSerial (7, 6);

String BT = "";

void setup() {

  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);

  Serial.begin(9600);
  BTSerial.begin(9600);
  BTSerial.setTimeout(50);
  delay(1000);

  lcd.begin (16, 2);
  lcd.print ("start!");
  delay (1000);
  
}

void loop() {

  while (BTSerial.available() > 0 ) {

    String BT = BTSerial.readString();

    // button A+
    if (digitalRead(2) == LOW || BT == "A+") {
      while (digitalRead(2) == LOW) {
      }
      delay(250);
      Serial.println("button A+ PRESSED");
      lcd.setCursor(0, 0);
      lcd.print("button A+ PRESSED");
    } 

    // button A-
    if (digitalRead(3) == LOW || BT == "A-") {
      while (digitalRead(3) == LOW) {
      }
      delay(250);
      Serial.println("button A- PRESSED");
      lcd.setCursor(0, 0);
      lcd.print("button A- PRESSED");
    }

    // button B+
    if (digitalRead(4) == LOW || BT == "B+") {
      while (digitalRead(4) == LOW) {
      }
      delay(250);
      Serial.println("button B+ PRESSED");
      lcd.setCursor(0, 0);
      lcd.print("button B+ PRESSED");
     }

    // button B-
    if (digitalRead(5) == LOW || BT == "B-") {
      while (digitalRead(5) == LOW) {
      }
      delay(250);
      Serial.println("button B- PRESSED");
      lcd.setCursor(0, 0);
      lcd.print("button B- PRESSED");
    }

  } //end while BT
} //end loop

This is my code.

type or paste code here


type or paste code here

I have no idea what you did when you posted your code please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Images of code nearly but worthless.

We need to see all of your code. Read the forum guidelines to see how to properly post code. We like to copy the code into our favorite text editor or IDE for examination.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Please include a schematic so we can know how the switches (and the rest of the project) is wired.

I am sorry for my inexperience.
Can i post my code in this topic or should i open a new topic?

You could even edit your first entry.

BTW: Are you aware that you only read the digital states if there's an event on (Bluetooth) Serial?

Please do not open another thread. Post the complete and verifiable code, in code tags, in this thread in a new post.

edit, I did not see that you fixed the OP (Original Post). Good job.

Seems your plan is to act on events.
An event is an arriving message or a state change of one of your physical buttons.
So you have get the states of your buttons independently of arriving messages,
and you have to notice changes, both of button states and and new messages.

I understand thanks.

Here is a demo that reads the buttons and incoming Bluetooth messages independently to control what is printed. There are flags set to indicate a new button or Bluetooth message. It uses the state change detection method to read the button switches so only one message for each button press. See also the state change for active low switches. The code was tested on an Uno so the Bluetooth module was connected to a SoftwareSerial port to keep the hardware serial port free for upload and program debug and output. BT TX to Uno pin 6 and BT RX to Uno pin 7. I use the Receive with End Markers code from the serial input basics tutorial for serial input into a string, avoiding the potential problems that can occur with use of the String class. The data sent to the Bluetooth module must be terminated with a line feed ('\n'). Carriage return optional.

#include <SoftwareSerial.h>

struct Buttons
{
   byte pin;
   bool currentState;
   bool lastState;
};

const byte NUM_BUTTONS = 4;

Buttons button[NUM_BUTTONS];  // array of button switches
bool messageStates[NUM_BUTTONS];  // array of message numbers

const byte numChars = 11;
char receivedChars[numChars];   // an array to store the received data

// flags for states
bool newButton = false;
bool newData = false;
bool newBluetooth = false;

SoftwareSerial ss(6, 7);

void setup()
{
   Serial.begin(115200);
   Serial.println("Arduino up");
   ss.begin(9600);
   ss.println("Bluetooth up");
   button[0] = {2, 1, 1};
   button[1] = {3, 1, 1};
   button[2] = {4, 1, 1};
   button[3] = {5, 1, 1};
   for (int n = 0; n < NUM_BUTTONS; n++)
   {
      pinMode(button[n].pin, INPUT_PULLUP);
      messageStates[n] = HIGH;
   }
}

void loop()
{
   // reset message states for this iteration
   for (int n = 0; n < NUM_BUTTONS; n++)
   {
      messageStates[n] = HIGH;
   }
   
   // recveive from Bluetooth
   recvWithEndMarker();
   if (newData)
   {
      parseData();
   }

   // check for button presses
   checkButtons();

   //if either a button was pressed or a new Bluetooth message.
   if (newButton || newBluetooth)
   {
      if (messageStates[0] == 0)
      {
         Serial.println("A+");
      }
      if (messageStates[1] == 0)
      {
         Serial.println("A-");
      }
      if (messageStates[2] == 0)
      {
         Serial.println("B+");
      }
      if (messageStates[3] == 0)
      {
         Serial.println("B-");
      }
      newButton = false;
      newBluetooth = false;
   }
}

void checkButtons()
{
   static unsigned long timer = 0;
   unsigned long interval = 50;
   if (millis() - timer >= interval)
   {
      timer = millis();

      for (int n = 0; n < NUM_BUTTONS; n++)
      {
         button[n].currentState = digitalRead(button[n].pin);
         //Serial.println(button[n].currentState);
         if ( button[n].currentState != button[n].lastState)
         {
            if (button[n].currentState == LOW)
            {
               messageStates[n] = 0;
               newButton = true;
            }            
            button[n].lastState = button[n].currentState;
         }
      }
   }
}

void recvWithEndMarker()
{
   static byte ndx = 0;
   char endMarker = '\n';
   char rc;

   while (ss.available() > 0 && newData == false)
   {
      rc = ss.read();
      if (rc == '\r') // ignore carriage return
      {
         return;
      }
      //Serial.println(rc);
      if (rc != endMarker)
      {
         receivedChars[ndx] = rc;
         ndx++;
         if (ndx >= numChars)
         {
            ndx = numChars - 1;
         }
      }
      else
      {
         receivedChars[ndx] = '\0'; // terminate the string
         ndx = 0;
         newData = true;
      }
   }
}

void parseData()
{
   //Serial.println("parsing");
   if (strcmp(receivedChars, "A+") == 0)
   {
      messageStates[0] = 0;
   }
   
   if (strcmp(receivedChars, "A-") == 0)
   {
      messageStates[1] = 0;
   }
   
   if (strcmp(receivedChars, "B+") == 0)
   {
      messageStates[2] = 0;
   }
   
   if (strcmp(receivedChars, "B-") == 0)
   {
      messageStates[3] = 0;
   }   
   newData = false;
   newBluetooth = true;
}

thanks a lot, you code is very usefull for me to study structure more complex of programming also.

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