If statement been ignored inside a function

Hi, I hope someone can help. I am writing a sketch as part of a larger project using the RN-42XV with the UNO. As part of the setup I want to test the serial connection to the RS-42 serial connection to the UNO.

I have it working but needed to make changes to reduce the memory usage so I started to use functions to reduce the Flash RAM usage and the Flash memory print function "F(print)" to reduce the SRAM usage.

This worked well up to the point where I have a function ignoring an if statement. The if statement looks for the CR and NL characters from a mySerial write to a variable rnBuffer, if they are there then the function should just loop until not further mySerial input is seen.

I've copied the function below and the full code after

void getRN() {
  byte buffIndex = 0;
  byte rnBuffer;
  while (!mySerial.available()) {
  }
  while (mySerial.available()) {
    rnBuffer = mySerial.read();
    if (rnBuffer != 13 || rnBuffer != 10) { 
      dataBuffer[buffIndex] = rnBuffer;
      buffIndex ++;
    }
  }
}

I have tried using else statement, and removing the if statement completely and still get the same results.
There are come other strange things going on too. Originally I was assigning the Char (string) variable dataBuffer = "abcdefghijk" to test menset (never used it before). When I assign these characters to the variable I the mySerial stops printing the "---" (Triple hyphen) at the end of the setup to exit the CMD mode and I get random "--" (Double Hyphen) prints on my Serial monitor.
Maybe something to do with memory ?? I don't know I've spent all of tonight trying to figure this out. Please help :confused:

Ow one lat thing..... please ignore some of the excess variables and the myANStoBIN function call results it has not worked since I added the functions, which I needed to change some o the variable names for, just not got round to it fixing yet, its a work in progress! but welcome any comments or info on anything else.

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); //Rx, Tx

char rnCMD[3], rnBaudRate[5], rnName[12], rnPinCode[5], rnHID[5];
uint16_t rnHID_bin;
byte rnHID_bin1, rnHID_bin2, rnHID_bin3, rnHID_bin4, rnHID_mode, rnHID_cnt;
byte buffIndex = 0;
byte exitSetup = 0;
char dataBuffer[12];



void setup() {
  Serial.begin(57600);
  mySerial.begin(57600);



  Serial.println(F("======================================================================"));
  Serial.println(F("=                    Serial Communication Test                       ="));
  Serial.println(F("======================================================================"));
  Serial.print(F("Connecting to RN-42XV"));
  delay (250);
  Serial.print(F("."));
  delay (250);
  Serial.print(F("."));
  delay (250);
  Serial.print(F("."));
  delay (250);
  Serial.print(F("."));
  delay (250);
  Serial.print(F("."));
  delay (250);
  Serial.print(F("."));
  delay (250);
  Serial.println(F("."));
  delay (250);



  //Enter CMD mode and test connection
  mySerial.print(F("$$"));

  getRN();

  if (dataBuffer[0] == 'C' & dataBuffer[1] == 'M' & dataBuffer[2] == 'D') {
    Serial.println (F("Connection: Pass"));
  }
  else {
    Serial.println(F("Connection: Failed, please check"));
    Serial.println(F("                           - Wiring"));
    Serial.println(F("                           - Baud rate"));
    Serial.println(F("                           - Power supply"));
    Serial.println(F("                           - Reset Power to RN-42"));
    mySerial.println(F("---"));
    return;
  }  

  Serial.println(F("RN-42XV Settings:"));

  //Retrieve name of the device
  mySerial.println("GN");
  getRN();
  Serial.print (F("\tDevice name  :  "));
  Serial.println (dataBuffer);
  memset(dataBuffer, 0, sizeof(dataBuffer));

  //Retrieve Baudrate of the device
  mySerial.println(F("GU"));
  getRN();
  Serial.print (F("\tBaud Rate    :  "));
  Serial.println (dataBuffer);
  memset(dataBuffer, 0, sizeof(dataBuffer));

  //Retrieve Pincode of the device
  mySerial.println(F("GP"));
  getRN();
  Serial.print (F("\tPin Code     :  "));
  Serial.println (dataBuffer);
  memset(dataBuffer, 0, sizeof(dataBuffer));

  //Retrieve BIN HID code of the device
  mySerial.println(F("GH"));
  getRN();
  rnHID_bin = myANStoBIN(dataBuffer[0], dataBuffer[1], dataBuffer[2], dataBuffer[3]);
  Serial.print (F("\tHID Binary   :  "));
  for (int x = 15 ; x > -1 ; x--) {
    Serial.print(bitRead(rnHID_bin, x));
  }
  Serial.print (F("\n"));

  //Bit masking for HID Binary

  Serial.print (F("\tHID Settings : "));
  if (bitRead(rnHID_bin, 9) == 1) {
    Serial.println (F("HID mode active if GPIO11 is high "));
  }
  if (bitRead(rnHID_bin, 8) == 1) {
    Serial.println (F("\t\t\tIOS virtual keyboard active on power up "));
  }
  if (bitRead(rnHID_bin, 3) == 1) {
    Serial.println (F("\t\t\tOutput reports over UART active "));
  }
  Serial.print (F("\t\t\tHID Mode (if active) : "));
  for (int j = 7; j > 3; j --) {
    byte mode = bitRead(rnHID_bin, j);
    rnHID_mode = rnHID_mode << (4 - j) | mode;
  }
  if (rnHID_mode == 0) {
    Serial.println (F("Keyboard"));
  }
  else if (rnHID_mode == 1) {
    Serial.println (F("Game pad"));
  }
  else if (rnHID_mode == 2) {
    Serial.println (F("Mouse"));
  }
  else if (rnHID_mode == 3) {
    Serial.println (F("Combo"));
  }
  else if (rnHID_mode == 3) {
    Serial.println (F("Joystick"));
  }
  else {
    Serial.println (F("Unrecognised value"));
  }

  Serial.print (F("\t\t\tNumber of connected devices allowed : "));
  for (int j = 2; j > -1 ; j --) {
    byte mode = bitRead(rnHID_bin, j);
    rnHID_cnt = rnHID_cnt << (4 - j) | mode;
  }
  Serial.println ((int)rnHID_cnt);

      mySerial.println("---");
  
  //Exit Setup
  Serial.println(F("\nPress enter to continue!"));
  while (exitSetup != 13) {
    exitSetup = Serial.read();
  }


}

void loop() {
  if (buffIndex < 1) {
    Serial.println(F("End setup"));
    mySerial.println("---");
    buffIndex = ++ buffIndex;  
  }
}


//FUNCTIONS
void getRN() {
  byte buffIndex = 0;
  byte rnBuffer;
  while (!mySerial.available()) {
  }
  while (mySerial.available()) {
    rnBuffer = mySerial.read();
    if (rnBuffer != 13 || rnBuffer != 10) { 
      dataBuffer[buffIndex] = rnBuffer;
      buffIndex ++;
    }
  }
}

uint16_t myANStoBIN(char w, char x, char y, char z) {
  uint16_t result;
  int word1[4] = {w, x, y, z};
  for (int i = 0; i > 3; i ++) {
    switch (word1[i]) {
      case 48: word1[i] = 0; break;
      case 49: word1[i] = 1; break;
      case 50: word1[i] = 2; break;
      case 51: word1[i] = 3; break;
      case 52: word1[i] = 4; break;
      case 53: word1[i] = 5; break;
      case 54: word1[i] = 6; break;
      case 55: word1[i] = 7; break;
      case 56: word1[i] = 8; break;
      case 57: word1[i] = 9; break;
      case 65: word1[i] = 10; break;
      case 66: word1[i] = 11; break;
      case 67: word1[i] = 12; break;
      case 68: word1[i] = 13; break;
      case 69: word1[i] = 14; break;
      case 70: word1[i] = 15; break;
      default: word1[i] = 16; break;
    }
    result = result << (i * 4) | word1[i];
  }
  return result;
}

The problem is that your logic is incorrect:

if (rnBuffer != 13 || rnBuffer != 10) {

The if will be true if either of the separate conditions are true (that's what the || operator does). If you think about it a bit, you'll realize that at least one of those conditions is true no matter what rnBuffer is.

Some examples:

| rnBuffer | | rnBuffer != 13 | | tnBuffer != 10 | | result |
| - | - | - | - |
| 0 | true | true | true |
| 10 | true | false | true |
| 13 | false | true | true |
| 65 | true | true | true |

I think you want to use &&.

--Doug

dmwheeler is 100% correct.

You probably thought in a sentence like "Not equal to CR or NL" and tried to translate that into a boolean expression. But that's not correct. The proper way to word the expression so you can translate it is "not equal to CR and not equal to NL".

Also, there are special character sequences you can use to represent CR and LF.

x!='\r' && x!='\n'

Google "de Morgan's laws"

This

while (mySerial.available()) {
    rnBuffer = mySerial.read();

is not a reliable way to receive data because the Arduino works very much faster than the data arrives.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

Sorry for the late reply, Thanks all your feedback really helped. Yes it was an daft logic error in the function.... Thanks Robin 2 for the link as well. Very helpful.