2

I'm trying to print some data to and from an ATtiny85 via serial, using the SoftwareSerial library. So far I've tried these setups with no results:

  • 1/8MHz clock
  • baud rates from 9600 to 57600 (library doesn't seem to support higher rates)
  • printing to a newline or a single one
  • packing up a char array just as much as a String and then printing it
  • Txing/Rxing through an Arduino Nano/Uno/Mega or a cheap USB-Serial port

Reading up on the subject seemed to suggest that some values I was passing (such as '1000') might have been erroneously interpreted as EOL characters breaking the code, but attempts with different values (i.e. 845) returned the same failures.

As you might have guessed I'm writing code with the ArduinoIDE, and no errors are returned when compiling and uploading. Many other parts of the project involving serial work just fine, and I can communicate with the ATtiny on pretty much any of the above setups (actually packing up the String or char[] crash the whole thing). The 'Ready' at the end of the setup is also printed fine, and the loop runs as long as it won't encounter any of the above issues.

Here's the code:

int VALUE = 1000;
int ANOTHER = 12;

#include <SoftwareSerial.h>
SoftwareSerial tSerial(3,4);

void _serialEvent() {
  if (tSerial.available()) {
    char mode = tSerial.read();
    if (mode == '?') {
      tSerial.println("someTextHere 1.23moreText");
      tSerial.print("value:");
      tSerial.print(VALUE);
      tSerial.print(";another:");
      tSerial.println(ANOTHER);
    }
    else if ...
  }
}

void setup() {
  tSerial.begin(57600);
  ...
  tSerial.println("Ready");
}

void loop() {
  ...
  _serialEvent();
}
  • Try to use F() macro to use string literals directly from flash. They must be copied into the RAM as they are now. Usage: tSerial.print(F("whatever you want")); and i saves lot of RAM. – KIIV Sep 17 '16 at 9:02
  • ok that did it. can you please explain a bit more (a simple link to documentation will suffice) and set this as answer so I can accept it? – nxet Sep 17 '16 at 9:21
  • 1/8 MHz is a little slow for using SoftwareSerial. I would rather clock it at 8 MHz. – Edgar Bonet Sep 17 '16 at 16:00
  • Yes that's what I ended up doing, still that wasn't the cause. – nxet Sep 17 '16 at 16:10
2

It is much better to store string literals in the FLASH only. In Arduino there is __FlashStringHelper class and F() macro ensures that correct version of print method is used and that the string literal is not copied into the ram before it's printed.

Basically if you don't have enough memory, using lots of string literals results in stack overflows, coruption and/or returning to random adresses and it looks like crashes/resets.

  • Extremely helpful insight, thank you! This actually led to solve a couple other glitches I've been experiencing without understanding them. I was wondering if there is any tool available to debug this kind of situation? – nxet Sep 17 '16 at 10:27
  • 2
    You might want to read this article (learn.adafruit.com/memories-of-an-arduino/measuring-free-memory) reporting a quite clear explanation of typical micros memory usage, along with some advices to debug related problems – Roberto Lo Giacco Sep 17 '16 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.