Serial Monitor Issue

Good Day,
I am having trouble with a serial console on both platformIO and arudino IDE. I have coded and debugged plenty of arduino projects in the past and can't recall ever having this happen and I'm at wits end. I searched the forums people had the same issue in much larger codes where another line was causing their problem. When I simply try to print something to the console I only get the first 2 letters. Here is the lowest level of code that produces the error. It produces the error with or without the return statement commented out and similarly for the while statement.

#include <Arduino.h>
#include "BT.h"

int main(){
  Serial.begin(9600);
while(!Serial){}
 initBT();
  Serial.println("Welcome to serial console");



  //return 0;
}

In case you were wondering initBT(); calls a software serial on pins 10,11 to send commands to a HM-10. I didn't run the code in a loop because this is for a one off function I need to periodically get data from the HM-10 in the course of a much larger program. I know the serial.print function works when I put it in a loop and also works if I create a timer to countdown before executing the next line of code. I was under the impression that only SPI needs to send junk data to push values through from the slave but it seems this is how serial is operating right now.

What to you get if you comment out initBT()?

Does the Serial print still work? It will narrow down your problem.

You have used your own main() functions so the hidden one used by the Arduino environment will not be used.

Here is the Arduino main() function

/*
  main.cpp - Main loop for Arduino sketches
  Copyright (c) 2005-2013 Arduino Team.  All right reserved.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include <Arduino.h>

// Declared weak in Arduino.h to allow user redefinitions.
int atexit(void (* /*func*/ )()) { return 0; }

// Weak empty variant initialization function.
// May be redefined by variant files.
void initVariant() __attribute__((weak));
void initVariant() { }

void setupUSB() __attribute__((weak));
void setupUSB() { }

int main(void)
{
 init();

 initVariant();

#if defined(USBCON)
 USBDevice.attach();
#endif
 
 setup();
    
 for (;;) {
 loop();
 if (serialEventRun) serialEventRun();
 }
        
 return 0;
}

Note that this one calls initialisation functions that yours doesn't. Could that be a problem ?

I have tried to comment out initBT() as well. I've made it lowest possible level with just a main, as Serial.begin and Serial.print and it still does it. I'll take a look at their main function and see what I can deduce from it. It's weird because I've never really had this issue using my own main function before but usually my code runs inside a while loop. I'm thinking therein lies the problem that I'll have to go searching for.

This works

#include <Arduino.h>

int main()
{
  init();
  Serial.begin(9600);
  while (!Serial) {}
  Serial.println("Welcome to serial console");
  delay(25);
}

With no delay() or with with a lower delay() period the program ends before the serial output is completed. If you increase the baud rate then a shorter delay() can be used

With no delay() or with with a lower delay() period the program ends before the serial output is completed. If you increase the baud rate then a shorter delay() can be used

Or, you could use Serial.flush() which will block until the outgoing buffer is empty. This is the ONLY place where Serial.flush() is useful.