Dear community,
After asking my previous question, I now have another one.
I am attempting to make Serial work on my ATtiny85. I am using the SoftwareSerial library that comes with the Arduino 1.6.4, and the Arduino IDE version 1.6.4. For some reason, Serial refuses to work at all, only working when I jiggle the pins. I can see that when I attach my LED to the serial port of the ATtiny something is happening, but I can't read it with my Uno.
ATtiny85 Code
#include <SoftwareSerial.h>
const int RX = 2;
const int TX = 3;
SoftwareSerial mySerial(RX, TX);
const int ledPin = 1;
int state = 0;
void setup()
{
pinMode(RX, INPUT);
pinMode(TX, OUTPUT);
mySerial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop()
{
if(mySerial.available())
{
state = mySerial.readString().toInt();
}
digitalWrite(ledPin, state);
}
Obviously, this is not the code that I wish to use, but it recreates the problem well enough.
Pin 2 on the Arduino is connected to pin 3 on the ATtiny, and pin 2 on the ATtiny is connected to pin 3 on the Arduino.
I have flashed the bootloader to internal 8 MHz, and I am using the "Arduino as ISP" programmer. The ATtiny is on a common ground with the Arduino, being powered by it, and it has 5V from the Arduino on-board power supply. A tantalum 0.1uF capacitor is attached between the ATtiny's power and ground.
I do not believe there is a problem with your sketch, but how it is built by the compiler giving your software serial port the wrong timing for 9600 baud. I would suggest looking at this port on an oscilloscope to better understand what it is doing.
What hardware profile have you set up in Arduino for your ATtiny85? Consider David A. Mellis's if you don't already have a working hardware profile set up.
Although many parts of a sketch will continue to work despite the correct hardware profile, serial port timing is one that is directly tied to the speed of the microcontroller.
I am currently using the ATtinyCore from GitHub - SpenceKonde/ATTinyCore: Arduino core for ATtiny 1634, 828, x313, x4, x41, x5, x61, x7 and x8. (That is what you meant by Hardware profile, right?) I initially used the ATtiny core provided by MIT, but I stopped using it when I realized it was 5 years old, whereas the ATtinyCore was updated 2 days ago. I am uncertain if tuning my internal oscillator would do anything, so that is the next item I am going to try to address.
As soon as I can figure out where to get an oscilloscope, I will do so.
I don't mean to be rude, but I think I do a more rigorous job of maintaining my core than David Mellis does (I also support far more parts, and include "universal" SPI and Wire libraries, so you can use I2C and SPI devices on parts that have a USI without modifying libraries)
Took a closer look at that - there's a problem.
Software serial is half duplex - you cannot send and receive at the same time. Usually attempting to do so generates gibberish (very similar to baud rate mismatch due to non-tuned oscillator).
If you blindly echo every character you receive, if you send more than one character (including line ending) at a time, the second character will be coming in when you're trying to echo back the first character - attempting to send and receive at the same time.
I made the same mistake when I was first working with software serial.
To tell the two apart, it may be useful to just have the attiny send data, and see if you can read that.
Also, your code is flawed - you are converting a string to an int - but you don't know that you've gotten the whole message by the time that you read it. Similarly, you're turning the input into probably a multi-character output, but the receiving attiny wouldn't know that there are more characters coming after the first charachter (and would try to send the data after the first character, which you can't do as described above).
The normal solution to this sort of problem is to use a marker character to denote the start and end of a message. Also, String class is evil, you should avoid using it, instead use c strings (fixed length character arrays) and the c string manipulation functions. They are less intuitive, but free of the problems that String does due to it's use of dynamic memory allocation on a highly-memory-constrained system.
DrAzzy:
I don't mean to be rude, but I think I do a more rigorous job of maintaining my core than David Mellis does (I also support far more parts, and include "universal" SPI and Wire libraries, so you can use I2C and SPI devices on parts that have a USI without modifying libraries)
I didn't mean to step on toes by suggesting one core over another. I only meant to suggest that it's important to set up Arduino for the target hardware. I've done some stuff with the ATtiny85 and ATtiny84, but not with Arduino. I will keep your core in mind for future use.
I didn't mean to step on any toes either. I am very sorry!
Both cores are great, as they both do what is intended. I agree that it is probably my ATtiny's fault, as I purchased it off of ebay and I probably got what I paid for. I am attempting to get what I bought to work, and I realize that my relative inexperience probably does not help things.
I apologize for any offense I may have caused (as well as for not responding immediately). Can we please all be friends?
Anyway, an update. I have gotten my ATtiny to receive serial, and I am trying to learn how to use the cstrings. I am still new, and I will make mistakes (sorry!). For some reason, the ATtiny can receive, but not send. I am beginning to think the reason this doesn't work is because the ATtiny is supposed to run at 5V, but I driving it with 3V3. I will do more tests to try and figure this out.
Oh, you didn't step on any toes - if anything I was the one being more obnoxious there, for promoting my core. Sorry if that was misunderstood.
Cheap ATTiny's off ebay generally work fine - certainly, if you're getting any functionality at all, I would expect them to be working with this. I do not think the problem is related to the specific ATTiny you're using.
Did you read the rest of my reply above? I think the issue was probably that your code was attempting to send and receive at the same time (that's a no-no for software serial - either SoftwareSerial library, or the software serial port "Serial" included with my core).
If you're running the attiny at 8mhz, it's totally within spec at 3.3v; see the speed grades listed in the database.