AVR (Arduino Uno) Serial.print and Serial.println only printing 1 or 2 chars I first used the Arduino IDE but now I'm using PlatformIO which is awesome. But I have some big trouble with Serial.print() and Serial.println(). To show this error I have the following example code:

#include &lt;avr/io.h&gt;
#include &lt;util/delay.h&gt;
#include "Arduino.h"
#include "main.h"

int main(int argc, char const *argv[]) {
  Serial.begin(9600);

  section5ex34();

  return 0;
}

void section5ex34() {
  // Initialization
  DDRC = 0b00000100; // Port C2 as output and the rest (including Port C3) as input. (Port C2 is used for a led that indicates when en bit is being read)

  uint8_t b = 0b00000000;

  // here the code reads bits from PORTC3
  for (int i = 0; i &lt; 8; i++) {
    _delay_ms(1000);
    PORTC |= _BV(PORTC2); // Make Port C2 high (indicates a bit will be read)

    if ((PINC &amp; (0b1 &lt;&lt; 3)) != 0) {
      b |= (1 &lt;&lt; i);
    }

    _delay_ms(100); // Wait for the user to prepare the next bit (because this is now done by hand)
    PORTC &amp;= ~_BV(PORTC2); // Then make Port C2 low
  }

  Serial.println(b, HEX);
}


When I have the code like this, it prints the right Hexadecimal value corresponding with the 8 read bits.

When I add Serial.println("test"); after the print, it doesn't print that.

Or when I change Serial.println(b, HEX); to Serial.println(b, BIN); it prints 11.

When I replace those 2 prints with Serial.println("Hi test test."); it prints Hi, so it seems like it can only print 2 characters.

When I add Serial.println("Hello world . hello . hi"); four times after Serial.begin(9600);, it prints Hello world . h and nothing more. So here it seems like it can print more than 2 characters, but not all. If I wait a few seconds it adds ello to this, so it says Hello world . hello.

In the initial code, when I replace the if-statement with

if ((PINC &amp; (0b1 &lt;&lt; 3)) != 0) {
  b |= (1 &lt;&lt; i);
  Serial.println("1");
} else {
  Serial.println("0");
}


and when the input is connected to 5v (so high) it prints a 1 once and nothing else, not even a hexadecimal value when the reading is finished.

This problem drives me crazy.

I have an Arduino Uno and use a baudrate of 9600 , 8,N,1.

I hope someone can help me out with this.
You have two issues here:

The first is that you are using the Arduino core library and failed to
initialize it. This is done automatically if you rely on the core's
supplied main(), i.e. you stick with the setup()/loop() style of
coding. If you write your own main(), you should start by calling
init() to initialize the core.

Edit: In this particular instance, you could get away by just enabling
interrupts with sei(). But I consider it good practice to initialize the
core if you are ever going to use it.

The second issue is the abrupt termination of main:
Serial.print() and println() do not really print to the serial port.
Instead, they put the characters to be printed in the output buffer, and
let an interrupt take care of actually sending them through the UART.

Your program ends with

return 0;


This will stop it. It will stop the whole program, including
interrupt-driven tasks. So you will see whatever characters did make it
to the UART, and nothing more.

If you instead end the program with and endless loop

for (;;) ;


it should work as expected.