Serial.print stops code running

Hi,

I have code for reading data from ultrasonic, OneWire temp sensors and BME280

This code works, and I now transmit over serial link, with start character "O" and End character "K"

However, this code now no longer runs and a little bit of commenting out has brought to light a very weird problem in that not commenting out one Serial.Print right at the end causes the whole program to stop running. Any thoughts as this one has got me!

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS A0
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
Adafruit_BME280 bme; // I2C
#define SEALEVELPRESSURE_HPA (1013.25)

#include <SoftwareSerial.h>
SoftwareSerial Ultrasonic(3, 4, false); // define serial port PIN

#define version 1.0


int mm[20];
int iterations = 20;
const byte numChars = 8;
char receivedChars[numChars];
bool newData = false;
int  deviceCount;


void setup()
{

  Serial.begin (9600);
  Ultrasonic.begin(9600);
  bme.begin(0x76, &Wire);

  pinMode(13, OUTPUT);  //LED
  digitalWrite(13, LOW);

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  display.display();

  display.setCursor(0, 0);
  display.setTextSize(2); // Draw 2X-scale text
  display.setTextColor(WHITE);
  display.println("Measuring");
  display.display();
}

void loop()
{


  Serial.print("Temperature = ");
  Serial.print(bme.readTemperature());
  Serial.println(" *C");


  Serial.print("Humidity = ");
  Serial.print(bme.readHumidity());
  Serial.println(" %");

  delay(1000);

  Serial.print(bme.readPressure());
  Serial.println(" hPa");

  Serial.print("Approx. Altitude = ");
  Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
  Serial.println(" m");





  while (iterations >= 0)

  {
    //digitalWrite(13, HIGH);
    recData();

    if (newData == true)
    {
      newData = false;
      mm[iterations] = (atoi(receivedChars)) ;
      iterations--;
    }
  }

  bubbleSort(mm, 20);
  digitalWrite(13, HIGH);


  sensors.begin();


  deviceCount = sensors.getDeviceCount();

  sensors.requestTemperatures(); // Send the command to get temperatures

  float T1 = (sensors.getTempCByIndex(0));
  float T2 = (sensors.getTempCByIndex(1));

  Serial.write("O");
  Serial.print(mm[10]);
  Serial.print(",");
  Serial.print(T1);
  Serial.print(",");
  Serial.print(T2);

 //Serial.print("If this is commented out, code runs, else won't even start");
 
 while (1);


}




void bubbleSort(int a[], int size) {
  for (int i = 0; i < (size - 1); i++) {
    for (int o = 0; o < (size - (i + 1)); o++) {
      if (a[o] > a[o + 1]) {
        int t = a[o];
        a[o] = a[o + 1];
        a[o + 1] = t;
      }
    }
  }
}

Edited to add, it seems I can do

Serial.print(",");

But anything with a character, as was

Serial.print("K");

Causes the non start.

int mm[20];
int iterations = 20;

You are writing out of array bounds here. mm[20] is out of bounds, the array indexes run from 0 to 19.

while (iterations >= 0)

  {
    //digitalWrite(13, HIGH);
    recData();

    if (newData == true)
    {
      newData = false;
      mm[iterations] = (atoi(receivedChars)) ;
      iterations--;
    }
  }

Thank you, I will correct that.