Arduino read from serial and convert to int

My sketch:

#include <SoftwareSerial.h>

SoftwareSerial rfid(7, 8);

int incomingByte = 0;
int convertedIncoming = 0;
int i = 0;
int j = 0;
int k = 0;
int bar[20];
int Str1[20];
int Str2[20];
int flag = 0;
int writeResponse[20];
int suffix = 0;
int CSUM = 0;
boolean stringComplete = false;

void setup() {
  Serial.begin(9600);
  Serial.println("Barcode to Tag Associator");
  Serial.println("Ready");
  rfid.begin(19200);
  delay(10);
  halt();
}

void loop() {
  if (stringComplete) {
    printbarcode();
    stringComplete = false;
    incomingByte = 0;
    convertedIncoming = 0;
    // int i = 0;
    int j = 0;
    int k = 0;
    int flag = 0;
    int suffix = 0;
    int CSUM = 0;
    for (i = 0; i < 21; i++) {
      bar[i] = 0;
      Str1[i] = 0;
      Str2[i] = 0;
    }
  }
}

void serialEvent()
{
  while (Serial.available()) {
    for (i = 0; i < 14; i++) {
      incomingByte = Serial.read();
      convertedIncoming = incomingByte - 48;
      bar[i] = convertedIncoming;
    }
  }
  stringComplete = true;
}

void printbarcode() {
  Serial.print("Envelope: ");
  for (i = 0; i < 10; i++) {
    Serial.print(bar[i]);
  }
  Serial.print("-");
  Serial.print(bar[10]);
  Serial.print(bar[11]);
  Serial.println(bar[12]);
  Serial.println("----------------------------------------");
}

When I run that and input "52001121607001" into the serial terminal, I get:

Barcode to Tag Associator
Ready
Envelope: 5-49-49-49-49-49-49-49-49-49--49-49-49
----------------------------------------
Envelope: 2001121607-001
----------------------------------------

I only want the second entry to print obviously, so I add Serial.println(bar[0]); in to the printbarcode() object:

void printbarcode() {
  Serial.println(bar[0]);
  Serial.print("Envelope: ");
  for (i = 0; i < 10; i++) {
    Serial.print(bar[i]);
  }
  Serial.print("-");
  Serial.print(bar[10]);
  Serial.print(bar[11]);
  Serial.println(bar[12]);
  Serial.println("----------------------------------------");
}

The output changes to:

Barcode to Tag Associator
Ready
5
Envelope: 5-49-49-49-49-49-49-49-49-49--49-49-49
----------------------------------------
2
Envelope: 2001121607-001
----------------------------------------

I say, okay, I'll add a conditional to the printbarcode() method to only print if bar[0] == 2.

void printbarcode() {
  Serial.println(bar[0]);
  if (bar[0] == 2) {
    Serial.print("Envelope: ");
    for (i = 0; i < 10; i++) {
      Serial.print(bar[i]);
    }
    Serial.print("-");
    Serial.print(bar[10]);
    Serial.print(bar[11]);
    Serial.println(bar[12]);
    Serial.println("----------------------------------------");
  }
}

Here is the output to that:

Barcode to Tag Associator
Ready
5
2
Envelope: 2-49-49-49-49-49-49-49-49-49--49-49-49
----------------------------------------
0

Clearly something is going on that I'm not seeing.
I've troubleshot it for a couple hours and can't figure it out.
Anyone see the bug?

int bar[20];

...

   for (i = 0; i < 21; i++) {
      bar[i] = 0;

...

   for (i = 0; i < 14; i++) {
      incomingByte = Serial.read();
      convertedIncoming = incomingByte - 48;
      bar[i] = convertedIncoming;

...

 for (i = 0; i < 10; i++) {
    Serial.print(bar[i]);
  }

You don't seem to have made up your mind you many elements are in "bar".

Decide. Make it a constant, eg.

const int NUMBER_OF_ELEMENTS = 20;
int bar[NUMBER_OF_ELEMENTS];
...
for (i = 0; i < NUMBER_OF_ELEMENTS; i++) {
...

Then use that in your code. Don't salt it with "magic numbers".


Personally I wouldn't use serialEvent. Read this: Gammon Forum : Electronics : Microprocessors : How to process incoming serial data without blocking

Thanks for the reply Nick.

Yes, I declared the bar array with 21 elements because while I knew it would contain as many as 14 or possibly more components, I decided to round up so as to avoid any potential overflow.

Thanks for reminding me that I can keep my number of elements variable.
I remember learning that in class but had forgotten since. Much appreciated.

What is the disadvantage of declaring my array with a higher index that I plan to utilize?

void serialEvent()
{
  while (Serial.available()) {
    for (i = 0; i < 14; i++) {
      incomingByte = Serial.read();
      convertedIncoming = incomingByte - 48;
      bar[i] = convertedIncoming;
    }
  }
  stringComplete = true;
}

There's your problem. You are trying to convert the string as soon as you have the first byte, rather than waiting until you have all the bytes.

See: https://www.arduino.cc/en/Serial/available

mgiara:
What is the disadvantage of declaring my array with a higher index that I plan to utilize?

It doesn't particularly matter, except for wasting RAM.

Yes, I declared the bar array with 21 elements

actually, no you didn't

int bar[20];

This gives you an array with 20 elements numbered from 0 to 19

 for (i = 0; i < 21; i++) {
      bar[i] = 0;

This puts a zero in bar[20] which is somewhere in memory that the program is not expecting, possibly overwriting something else.

And that is why you use constants (not magic numbers). It is tedious to go around checking for loops like that whether there really are 21 elements in that array. Especially when you might be primed to think that because of:

    for (i = 0; i < 14; i++) {

... the array is only 14 elements.

Instead of using the code in your serialEvent() function to receive the data use one of the examples (probably the second one) in serial input basics. They are simple reliable examples that receive all the data without blocking. There is also a parse example if you need it.

...R