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 aString
and then printing it Tx
ing/Rx
ing 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();
}
1 Answer 1
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– nxet2016年09月17日 10:27:15 +00:00Commented Sep 17, 2016 at 10:27
-
2You 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 problemsRoberto Lo Giacco– Roberto Lo Giacco2016年09月17日 10:48:46 +00:00Commented Sep 17, 2016 at 10:48
tSerial.print(F("whatever you want"));
and i saves lot of RAM.