XBee and USB Communication; Program blocks in setup() function and restarts.

Hello all,

I'm having a problem when using my Xbee Shield in my MEGA 2560. To upload I just move the pins to USB communication. For the antennas talk to each other, I move the pins to XBEE position!

I have a pretty simple code for the receiver antenna and I obtain good values from the sender antenna configured in XCTU!

The problem is when I try to use this piece of code:

void loop() {
 if (Serial.available() >= 21) { // Make sure the frame is all there
if (Serial.read() == 0x7E) { // 7E is the start byte
for (int i = 1; i<19; i++) { // Skip ahead to the analog data
byte discardByte = Serial.read();
}
int analogMSB = Serial.read(); // Read the first analog byte data
int analogLSB = Serial.read(); // Read the second byte
int analogReading = analogLSB + (analogMSB * 256);

Serial.print(analogReading);

}
}
}

in one of my other projects:

void setup() {
  Wire.begin();
  Serial.begin(9600);

  Serial.print("Initializing SD card...");

  pinMode(HCS, OUTPUT);

  // MORE TFT STUFF - SEE EXAMPLE SKETCHES
  tft.initR(INITR_BLACKTAB);   // initialize a ST7735S chip, black tab

  //ROTATES DISPLAY 90 DEGREES FOR LANDSCAPE VIEW
  tft.setRotation(3);
  tft.fillScreen(YELLOW);

  // see if the card is present and can be initialized:
  if (!SD.begin(sd)) {
    Serial.println("Card failed, or not present");

    tft.fillScreen(WHITE);

    tft.setTextSize(1);
    tft.setTextColor(BLACK);
    tft.setCursor(30, 40);
    tft.print("1) Insert SD CARD");
    tft.setCursor(30, 60);
    tft.print("2) Reset Energy Box");

    usingSD = false;

   // while (1) ;
  }

  else  usingSD = true;

  Serial.println("card initialized.");
  tft.fillScreen(YELLOW);
  bmpDraw("images/right.bmp", 35, 45);
  // analogWrite(5, 255);  //jump "LITE" and Digital pin 5 to dim
  delay(1500);
  // BLANKS OUT THE SCREEN AND DRAWS THE BLACK BAR
  tft.fillScreen(YELLOW);


  readSDSettings();
  Serial.print(TariffType);
  Serial.print(enteredValueMaximum);

  pinMode(redLedPin, OUTPUT);
  pinMode(greenLedPin, OUTPUT);
  pinMode(blueLedPin, OUTPUT);

  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  if (timeStatus() != timeSet)
    Serial.println("Unable to sync with the RTC");
  else
    Serial.println("RTC has set the system time");

  //writeSettingsSDInit();
  // readSettingsSDInit();





  Serial.print("      DATE");
  Serial.print("\t");
  Serial.print("     HOUR");
  Serial.print("\t");
  Serial.print("Power (W)");
  Serial.print("\t");
  Serial.print("Energy (kWh)");
  Serial.print("\t");
  Serial.print("Money Spent (Eur)");
  Serial.print("\t");
  Serial.print("Temperature (");
  Serial.print((char)247);
  Serial.println("C)");
  Serial.println("");

  // SDLayout();

  delay(1000);
}

when I upload the all program, it runs without problems after changing the pins in the Xbee Shield to "XBEE".. the problem is after I reset the arduino... the setup executes until a certain line, and it restarts again from the beginning..never going to the loop!
WHY??

the setup executes until a certain line, and it restarts again

Temporary implement and that remove to next location:

Serial.print(LINE);
for(semicolon semicolon ) semicolon // stop processing here

Put at places you suspect the code to fail until you find the cause.

And if you select "display line numbers " in "preferences" it would be a easier.

Or post full code and or current monitor outputs.

Hello, I know found out that when I reset the arduino with the sample program (first one):

void setup() {
    Serial.begin(9600);
    Serial1.begin(9600);
    Serial.print("Hey");

}

void loop() {
 
if (Serial.available() >= 21) { // Make sure the frame is all there
if (Serial.read() == 0x7E) { // 7E is the start byte
for (int i = 1; i<19; i++) { // Skip ahead to the analog data
byte discardByte = Serial.read();
}
int analogMSB = Serial.read(); // Read the first analog byte data
int analogLSB = Serial.read(); // Read the second byte
int analogReading = analogLSB + (analogMSB * 256);

Serial.println(analogReading);

}

it stops working and the message "Hey" in setup() is not printed! why?

Xbee Shield with USB pins connected to Upload! It uploads properly;
Change from USB to XBEE pins: i receive the values...

But after reset, nothing executes...