Having a bit of trouble with a part of a sketch. breaking out of a while loop

[code]
// using a Zebra LS2208 w/ TTL RS-232 cable into a Pro Mini NOTE the parameters for SoftwareSerial scanner, inverts logic signal.
#include <SoftwareSerial.h>
SoftwareSerial scanner(8, 9, 1);   // Scanner
#define RESETLINE 4
String scanInfo;

void setup()
{
  Serial.begin(9600);
  scanner.begin(9600);
}

void loop ()
{
  char StrScan[20];
  int x = 0;
  char b;
  String scantxt, stng1;
  {
    while (x < 18)
      if (scanner.available())
      {
        b = scanner.read();
        StrScan[x] = b;
      [color=blue]  if ((b, DEC) != 13)[/color]
        {
          x++;
        }
        else
        {
          break;
        }
      }
    Serial.println(x);
    scantxt = String(StrScan[0]);
    stng1 = String(StrScan[1]);
    scantxt += stng1;
    stng1 = String(StrScan[2]);
    scantxt += stng1;
    stng1 = String(StrScan[3]);
    scantxt += stng1;
    stng1 = String(StrScan[4]);
    scantxt += stng1;
    stng1 = String(StrScan[5]);
    scantxt += stng1;
    stng1 = String(StrScan[6]);
    scantxt += stng1;
    stng1 = String(StrScan[7]);
    scantxt += stng1;
    stng1 = String(StrScan[8]);
    scantxt += stng1;
    Serial.println(scantxt);
  }
}

[/code]

I highlighted it in dark blue. I want to break out of the while loop once the CR is detected (DEC 13). I have inserted line just below the one in blue with this: Serial.print(b, DEC); I decoded this back to characters and it contained both my expected data and “1310”. Both the CR and LF that was programmed into the barcode scanner. I need to leave the while set for 18 cause some bar codes will be 9 +CRLF and some will be 16 characters long + CRLF. Was trying to break out of the while loop. Code below is in work. Plan to look how long ‘x’ is and loop through the StrScan once I have the breakout problem fixed.

Thanks
Paul

Lose the , DEC. It doesn’t do what you think it does. There is no need to convert anything 13 is 13 is 13 whether it came from a calculation or an ascii code for CR. Just use

if(b != 13)

or

if(b != '\r')

I don't think color works inside a source listing...not sure. I'm not sure you know how the String class works. I would suggest you put a Serial.print() after each of these statements:

    Serial.println(x);
    scantxt = String(StrScan[0]);
    stng1 = String(StrScan[1]);
    scantxt += stng1;
    stng1 = String(StrScan[2]);
    scantxt += stng1;
    stng1 = String(StrScan[3]);
    scantxt += stng1;
    stng1 = String(StrScan[4]);
    scantxt += stng1;
    stng1 = String(StrScan[5]);
    scantxt += stng1;
    stng1 = String(StrScan[6]);
    scantxt += stng1;
    stng1 = String(StrScan[7]);
    scantxt += stng1;
    stng1 = String(StrScan[8]);
    scantxt += stng1;
    Serial.println(scantxt);

to see if it's what you think it should be. I can't do it since I don't have a scanner.

Delta_G: Lose the , DEC. It doesn't do what you think it does. There is no need to convert anything 13 is 13 is 13 whether it came from a calculation or an ascii code for CR. Just use

if(b != 13)

or

if(b != '\r')

Thanks Delt_G, Will try your examples again. I fairly sure that I had tried the first example you gave. But will try it and the second example. Sorry that the text didnt turn blue but see you did find it.

Hi econjack.

As you noted, I am so new here that I was not aware that I cant change color of the text within the code insert as I did when I pasted my sketch.

Actually at this time the code you quoted works quit nicely thanks. Once I get the break out code working I plan to take the number of iterations and loop the lower code that you quoted, into just a few lines. Dont need a scanner. Just make an array of char say 6 elements. Assign 1, 2, 3, A, B, & C to those 6 elements one character each. Then use my code and tell me what happens. Please in the furture focus on the question. About as bad as a cop at a traffic stop. Fishing for something.

Some back story. I need to scan 3 different bar codes. The shortest is 9 +2 characters long. And the longest is 16 + 2 characters long. I want to be able to reuse the code for each scan. I am going to name this function "scanner" and call it from a different function. From within that function I will assign several variables from the scatxt.

if ((b, DEC) != 13)

what is this supposed to be ?

michinyon: what is this supposed to be ?

Read reply #1

Dont need a scanner. Just make an array of char say 6 elements. Assign 1, 2, 3, A, B, & C to those 6 elements one character each. Then use my code and tell me what happens. Please in the furture focus on the question. About as bad as a cop at a traffic stop. Fishing for something.

Since it's your post, how about you write the code not using the scanner and then I'll try what you write. That way I won't waste my time fishing for something that is not clearly specified.

Delta_G: Lose the , DEC. It doesn't do what you think it does. There is no need to convert anything 13 is 13 is 13 whether it came from a calculation or an ascii code for CR. Just use

if(b != 13)

or

if(b != '\r')

Well I be danged. I am sure that I did the same as your first example before trying the "DEC" in the line. Just did it again both examples and the darn thing is working as expected. Thanks for giving me the mojo that I needed to make it to work. I am at work now, so cant show the finial sketch for this portion of my project. When I get home will post the sketch for anyone else wanting to do the same.

Thanks Delta_G for the help. BTW I also was able to use another while loop on the second half and got that down to a few lines. Darn if this project aint going to work after all.

Paul1958

econjack "I'm not sure you know how the String class works". No sir it is you who does not know how the string class works. My code is giving me just what I wanted. And with the help I was looking for from Delta_G. I now have it perfectly working the way I wanted it do. So, all you did was pick on some code that I was not asking for help with. And as it is, the code is working perfectly. So, what did you contribute to this discussion. ZERO Shame I cant take Karma points away. Good day...

Don’t get so bent at @econjack. It is good that you get your expected output for now. There may well be a time when the ground opens up underneath you and your abuse of the String object will swallow you up in a flurry of undefined operation and errors that can’t be tracked down. The Arduino has very limited memory space and if you keep fragmenting the heap using += to concatenate strings then it is just a matter of waiting for your code to hit some magic size and suddenly stops working altogether.

It is well known issue to those of us who have been at this a while. Don’t hate on someone for trying to give you a heads up.

Since StrScan is already a char buffer, it is doubly wasteful. Why not make scantxt a char buffer and use strcat for the concatenation. You can either learn to do that now or you can wait until the String class bites you. It would be far easier and less frustrating to learn now while there's not so much code to fix.

No sir it is you who does not know how the string class works.

Damn! I was almost sure I understood how it works.