Serial with ATtiny85

Hey guys,

I'm fairly new to Arduino and need some help.

I followed this tutorial and eventually got my project accomplished with some tweaks on my Uno.

However since what I'm trying to accomplish is fairly simple I wanted to try porting it over to Attiny85 and free up my Uno again.

I was able to use my Uno has a ISP and flash an ATtiny85 and was able to get a test program to work (the blink example). So with that newfound confidence I moved onto trying to figure out how to accomplish getting the Tiny to run this code:

#include <SoftwareSerial.h>
SoftwareSerial RFID(2, 3); // RX and TX

int i;

void setup()
{
  RFID.begin(9600);    // start serial to RFID reader
  Serial.begin(9600);  // start serial to PC 
}

void loop()
{
  if (RFID.available() > 0) 
  {
     i = RFID.read();
     Serial.print(i, DEC);
     Serial.print(" ");
  }
}

This resulted in this "Serial' was not declared in this scope" and the like:

Arduino: 1.6.5 (Mac OS X), Board: "ATtiny, ATtiny85, 8 MHz (internal)"

The sketch name had to be modified. Sketch names can only consist
of ASCII characters and numbers (but cannot start with a number).
They should also be less than 64 characters long.










Build options changed, rebuilding all
RFID_Key_Reading.ino: In function 'void setup()':
RFID_Key_Reading:9: error: 'Serial' was not declared in this scope
RFID_Key_Reading.ino: In function 'void loop()':
RFID_Key_Reading:17: error: 'Serial' was not declared in this scope
'Serial' was not declared in this scope

  This report would have more information with
  "Show verbose output during compilation"
  enabled in File > Preferences.

Doing some googling this seems to occur because the library for my Tiny is missing the serial command.

I had downloaded the library from here:

https://github.com/damellis/attiny/archive/702aa287455f7e052cf94fd4949398fec0ef21b8.zip

And installed it via this tutorial:

I also "burn the bootloader" so that it runs at 8 MHz just like the tutorial said for the serial command to work, but alas nada.

So I'm lost.

Some seem to say the Tiny85 can run serial, some say no. I'm posting here in the hopes that maybe someone could give me a hand and get this figured out.

The end goal is to run this bit of code on my Tiny85:

#include <SoftwareSerial.h>
SoftwareSerial RFID(2, 3); // RX and TX

int data1 = 0;
int ok = -1;
int yes = 13;
int no = 12;

// use first sketch in http://wp.me/p3LK05-3Gk to get your tag numbers
int tag1[14] = {2,48,54,48,48,50,70,52,49,48,50,54,65,3};
int tag2[14] = {2,52,48,48,48,56,54,67,54,54,66,54,66,3};
int newtag[14] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // used for read comparisons

void setup()
{
  RFID.begin(9600);    // start serial to RFID reader
  Serial.begin(9600);  // start serial to PC 
  pinMode(yes, OUTPUT); // for status LEDs
  pinMode(no, OUTPUT);
}

boolean comparetag(int aa[14], int bb[14])
{
  boolean ff = false;
  int fg = 0;
  for (int cc = 0 ; cc < 14 ; cc++)
  {
    if (aa[cc] == bb[cc])
    {
      fg++;
    }
  }
  if (fg == 14)
  {
    ff = true;
  }
  return ff;
}

void checkmytags() // compares each tag against the tag just read
{
  ok = 0; // this variable helps decision-making,
  // if it is 1 we have a match, zero is a read but no match,
  // -1 is no read attempt made
  if (comparetag(newtag, tag1) == true)
  {
    ok++;
  }
  if (comparetag(newtag, tag2) == true)
  {
    ok++;
  }
}

void readTags()
{
  ok = -1;

  if (RFID.available() > 0) 
  {
    // read tag numbers
    delay(75); // needed to allow time for the data to come in from the serial buffer.

    for (int z = 0 ; z < 14 ; z++) // read the rest of the tag
    {
      data1 = RFID.read();
      newtag[z] = data1;
    }
    RFID.flush(); // stops multiple reads

    // do the tags match up?
    checkmytags();
  }

  // now do something based on tag type
  if (ok > 0) // if we had a match
  {
    Serial.println("Accepted");
    digitalWrite(yes, HIGH);
    delay(1000);
    digitalWrite(yes, LOW);

    ok = -1;
  }
  else if (ok == 0) // if we didn't have a match
  {
    Serial.println("Rejected");
    digitalWrite(no, HIGH);
    delay(1000);
    digitalWrite(no, LOW);

    ok = -1;
  }
}

void loop()
{
  readTags();
}

Thanks for reading!

The tiny85 doesn't have any "serial port to PC"; you'll have to use another SoftwareSerial port, or something like the DigiSpark CDC library (both of which are probably "problematic" to run at the same time as a second software-serial port.)

You could look at some of the newer ATtiny chips that DO have hardware uarts (attiny841?), or just go with one of the ATmega chips. (an ATmega8 is about $1 from many sellers, and clone "Pro mini" board clones are very cheap.)

Hi

glucklich21:
Doing some googling this seems to occur because the library for my Tiny is missing the serial command.

That's almost right. The ATtiny85 is actually missing the hardware UART needed for that hardware serial implementation to work. That's why SoftwareSerial is the chosen path - it doesn't require that support hardware and implements serial in software (not just a clever name).

glucklich21:
Some seem to say the Tiny85 can run serial, some say no. I'm posting here in the hopes that maybe someone could give me a hand and get this figured out.

So you see both are right. You can get serial functionality happening, but not using the inbuilt serial commands that need that UART to be present in the IC.

If you're just wanting to read the tags and match them, you'd be able to get something running reasonably simply by adding LEDs to some of the unused legs on your ATtiny85 and have them display if there's a good read with no match, bad read or match.

Cheers ! Geoff

Note that some ATTiny cores (such as mine) do have a software serial instance built in named "Serial" - though it's still a software implementation.

@glucklich21:

You can try the core files from here: GitHub - rxpc/attiny: Attiny cores for Arduino

I've been using these attiny cores for more than a year now. I haven't had any issues with them and
your code should complie ok. I just add these to the hardware directory.

If your trying to debug using serial, use tinydebugknockbang to display data in the serial monitor when debugging any attiny code. But you will need to use TinyISP instead of ArduinoISP.