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 <avr/io.h>
#include <util/delay.h>
#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 < 8; i++) {
_delay_ms(1000);
PORTC |= _BV(PORTC2); // Make Port C2 high (indicates a bit will be read)
if ((PINC & (0b1 << 3)) != 0) {
b |= (1 << i);
}
_delay_ms(100); // Wait for the user to prepare the next bit (because this is now done by hand)
PORTC &= ~_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 & (0b1 << 3)) != 0) {
b |= (1 << 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.