Problem with SerialSoftware library on ATtiny85. [SOLVED]

Good evening to every one.

I have connected an attiny85 to an Arduino board via Software Serial library(ATtiny85 chip pins are 2=rx and 3=Tx and on the Arduino board are the numbers 4=Rx and 6=Tx).
When the '85 sends a data, -1 appear among the chars arriving from the attiny85, why happen?
If, an example, the attiny sends this V88.66
Arduino receive these V -1 -1 -1 8 8 . 6 -1 6
or, in bytes, 86 255 255 255 56 56 46 54 255 54
I have tried other libraries but nothing has changed.
Can anyone could be help me? Thanks :-)

Antonio (Italy)


Arduino board: Arduino UNO 16 MHz
Attiny85: Vcc5V, fclock = internal, 8MHz
IDE version: 1.8.12

The code of the Attiny85:

#include <SoftwareSerial.h>
#define Rx 3 // 3=pin2
#define Tx 4 // 4=pin3
SoftwareSerial NSP(Rx, Tx);
float i, num;

void setup()
{
  NSP.begin(38400);
}

void loop()
{
  num = i/3;
  NSP.print("V");
  NSP.println(num);
  delay(10);
  i++;
}

The code of Arduino board:

#include <SoftwareSerial.h>
SoftwareSerial ss(4, 6); // 4=Rx, 6=Tx
char c;
String comando;
void setup()
{
  pinMode(4, INPUT_PULLUP);
  pinMode(6, OUTPUT);
  ss.begin(38400);
  Serial.begin(2000000);
}

void loop()
{
  if(ss.available())                  //lettura dati da porta seriale
    {
//      c = ss.read();
//      Serial.println(c);
      do
        {
          c = (ss.read());            //acquisisci fintantoche...
          if (c != '\n')              // && c > -1)   //non appare il carattere di fine riga
            {
              comando = comando + c;  //nel frattempo aggiungi il carattere letto alla stringa
            }
         } while(c != '\n');           //rimani nel ciclo fino a che non arriva il fine riga
      Serial.println(comando);
      c = ' ';
      comando = "";
    }
}

Baudrate 38400 is too high for SoftwareSerial on a non-crystal 8MHz AVR chip. Try 9600 baud, report if that helps.

When the '85 sends a data, -1 appear among the chars arriving from the attiny85, why happen?

-1 means read failed. In your code this is probably the case because the character were not received yet. The ss.available() will return a true value even if only one byte is in the reception buffer. If read then until you reach a newline character your read will probably fail several times.

I have tried lower speeds but even so nothing has changed.

If I try to read the chars that came from ss.read one by one and send them one by one to Serial.port (without "ln"), the problem vanishing.
From this I deduced that you were right: much too readings in a bit of time generate the error. I have added a delay of 0.75ms in the Arduinos sketch and the problem is solved.
Many thanks pylon :slight_smile:

Antonio
p.s. excuse me for my bad english...

Attiny85 Code

#include <SoftwareSerial.h>
#define Rx 3 // 3=pin2
#define Tx 4 // 4=pin3
SoftwareSerial NSP(Rx, Tx);
float i, num;
void setup()
{
  NSP.begin(38400);
}

void loop()
{
  num = i/3;
  NSP.print("V");
  NSP.println(num);
  delay(75);
  i++;
}

Arduino UNO code

#include <SoftwareSerial.h>
SoftwareSerial ss(4, 6); // 4=Rx, 6=Tx
char c;
String comando;
void setup()
{
  ss.begin(38400);
  Serial.begin(2000000);
}

void loop()
{
  if(ss.available())                  
    {
      do
        {
          c = (ss.read());            
          if (c != '\n')              
            {
              comando = comando + c;  
            }
          delayMicroseconds(750);     // wait 0.75ms
         } while(c != '\n');          
      Serial.println(comando);
      c = ' ';
      comando = "";
    }
}