ATtiny85 SoftSerial, takes a while to stablise?

I'm experimenting with Serial communication with an ATtiny85 and the SoftwareSerial Library, this simple example kind of works but only after a few seconds. To begin with the ATtiny only seems to respond correctly around 50% of the time, but after a couple of seconds it functions correctly. Take this example code, which simply sends the character "a" to the Attiny, which echoes it back to the Mega, which then echos it to the PC:

Arduino Mega:

void setup()
{
	Serial.begin(4800);
	Serial1.begin(4800);
}

void loop()
{
	if (Serial1.available() > 0)
	{
		char c = Serial1.read();
		Serial.println(c);
	}

	Serial1.print("a");
	delay(100);
}

ATtiny85

#include <SoftwareSerial.h>
const int rx=2;
const int tx=3;
const int led=0;

SoftwareSerial mySerial(rx, tx);

void setup() 
{ 
  pinMode(rx,INPUT);
  pinMode(tx,OUTPUT);
  pinMode(led,OUTPUT);
  mySerial.begin(4800);
} 
 
void loop() 
{ 
  if (mySerial.available()>0)
  {
     digitalWrite(led, HIGH);
     char c = mySerial.read(); 
     mySerial.println(c);
   }
   
   digitalWrite(led, LOW);
}

Then on the serial monitor I might see something like this:

a



a



a


?


a



a



a



a



a



a
a
a
a
a
a
a

Where as I would expect just:

a
a
a
a
a
a
a
a

Any thoughts as to why this happens?

You send an "a". The 85 sends back "a". Why?

PaulS:
You send an "a". The 85 sends back "a". Why?

Well spotted thanks.

So I guess my question changes to: Why does the 85 start by sending "a", but then changes to just "a" ?

Does the same behavior occur if you add delay(100); to setup(), at the end? Perhaps you are trying to read data before the 85 is quite ready.

Yeah same behavior with that delay. Another example showing weird behavior:

Mega:

const int led=13;

void setup()
{
	pinMode(led, OUTPUT);
	Serial.begin(4800);
	Serial1.begin(4800);
	delay(100);
}

void loop()
{
	if (Serial1.available() > 0)
	{
		char c = Serial1.read();
		Serial.println(c);
	}
	digitalWrite(led, HIGH);
	Serial1.print("hello");
	delay(50);
	digitalWrite(led, LOW);
	delay(50);
}

85:

#include <SoftwareSerial.h>
const int rx=2;
const int tx=3;
const int led=0;

SoftwareSerial mySerial(rx, tx);

void setup() 
{ 
  pinMode(rx,INPUT);
  pinMode(tx,OUTPUT);
  pinMode(led,OUTPUT);
  mySerial.begin(4800);
  delay(100);
} 
 
void loop() 
{ 
  if (mySerial.available()>0)
  {
     digitalWrite(led, HIGH);
     char c = mySerial.read(); 
     mySerial.print(c);
   }
   
   digitalWrite(led, LOW);
}

Arduino Serial Monitor

h
Ø
±
½
ÿ
h
Ø
±
½
ÿ
h
Ø
±
½
ÿ
h
¸
±
½
ÿ
h
Ø
±
½
ÿ
h
Ø
±
½
ÿ
h
Ø
±
½
ÿ
h
Ø
±
½
ÿ
h
Ø
±
½
ÿ
h
Ø
±
½
ÿ
h
¸
±
½
ÿ
h
Ø
±
½
ÿ
h
?
±
½
ÿ
h
Ø
±
½
ÿ
h
Ø
±
½
ÿ
h
Ø
±
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
e
l
l
o
h
e
l
l
o
h
e
l
l
o
h
e
l
l
o
h
e
l
l
o
h
e
l
l
o
h
e
l
l
o
h
e
l
l
o
h
e
l
l
o
h
e
l
l
o

What clock speed are you using, try 8MHZ

The internal oscillator is not particularly precise which can affect the serial communication, but you can tune it

http://arduino.cc/forum/index.php/topic,8553.0.html

Yeah I'm selecting ATtiny85 (internal 8Mhz Clock) from the boards menu. Thanks for the link, although I would like a solution that will not involve so much calibration since I plan to use a lot of ATiny85's! (or similar)

If you only need one way communication (from the Tiny to PC) you could use TinyDebugSerial or TinyISP and the knockbang library:

http://www.ernstc.dk/arduino/tinycom.html

Yeah I'm selecting ATtiny85 (internal 8Mhz Clock) from the boards menu.

And select "Burn Bootloader" ?