Serial on ATtiny84

hi there i would like to send serial to my ATtiny84 from my pc to turn outputs on and off.

i have come up with this but does nath-all what have i dun wrong? and can i us any pins?

#include <SoftwareSerial.h>
const int rx=6;
const int tx=-1; // not used
SoftwareSerial mySerial(rx,tx);

int i=0;
char buf[12];

const int ledPin = 0;

void setup() {
pinMode(rx,INPUT);
pinMode(tx,OUTPUT);
mySerial.begin(9600);

pinMode(ledPin, OUTPUT);
}

void loop() {

if (mySerial.available()>0) {
buf = mySerial.read();

  • if (i == '1') {*
  • digitalWrite(ledPin, HIGH);*
  • }*
  • if (i == '2') {*
  • digitalWrite(ledPin, LOW);*
  • }*
  • }*
    }[/quote]

What behavior are you getting with that sketch?

just does nothing :confused:

does it look ok then?

Ah, I thought I replied here again.

I see no obvious problems; software serial can be flakey (that's why I don't use the Tiny84 anymore - I switched to the pin compatible 841, which has 2 hardware uarts for virtually the same price - see sig for core, atmel's datasheet for details). I'd try to narrow things down a bit... Like - can you make transmitting work? Do you get garbage? If you get garbage, maybe it's the oscillator? The internal oscillator can be way off (look at the datasheet - i think it's +/- 10% for the '84) I've seen people recently reporting issues with SoftwareSerial on the '85, though - so there may be a problem with the library too :confused:

i don't get any garbage so i don't no :confused:

was looking at this, would this be better maybe? Nerd Ralph: AVR half-duplex software UART supporting single pin operation

just been playing about with my ATtiny84 using the blink sketch if i us 1mhz all is fine but using 8 or 20mhz the output comes high and that is it, could i have a problem with my library?

thanks Joe

That's the same behavior someone else reported with the tiny85 - worked at 1mhz, but not at 8...

was there a outcome?

I actually had my first attempt at software serial on a tiny85 just a few days ago. The only way I was able to get it to work was setting the RX & TX pins to 3 and 4 (physical pins 5 & 6), I never got any other combination of pins to work (not that there are many different combinations to try on a tiny). For the record I was only using the TX pin to feed some things back to serial monitor for troubleshooting, didn't test the RX pin at all. This was a tiny85 8mhz internal.

#define rxPin 3
#define txPin 4

well I've just tried the pins you suggested when i powered up my output pin went high and didn't do any thing else.

so i don't know if its the library i am using as I've had it for a couple of years now so maybe i should download it again as their might have been some update? if so where should i find it?

I don't see how your code could do anything other than nothing. The LED being lit or not depends on the variable "i" which is never changed.

A couple of other less important notes. You don't need to set the pinMode() for the TX and RX pins; the SoftwareSerial library does that for you. Second, it's not a good idea to set the pinMode() for pin=-1. The bounds checking in the core is weak and this will result in some unknown location being written to. If you don't need the transmit function but can't afford to waste a pin then it's pretty easy to clone the SoftwareSerial library and remove what you don't need.

jboyton:
I don't see how your code could do anything other than nothing. The LED being lit or not depends on the variable "i" which is never changed.

Exactly. And you won't see any garbage because you don't send any feedback to the PC (TX disabled).

Read the serial data into a single char, then check that for a '1' or '2' to switch the LED.

Are you sure you have it wired correctly?
If it were me, I'd first just verify that I was getting an interrupt when the pin changed.

You could use the SoftwareSerial library for this, assuming it's correct. And assuming you have something connected to it that actually changes. For a simple test you could just take the bare wire and go from ground to Vcc.

#include <SoftwareSerial.h>
 const int rx=6;
 const int tx=-1; // not used and hopefully not a problem
SoftwareSerial mySerial(rx,tx);

const int ledPin = 0;

void setup()
{
mySerial.begin(9600);
pinMode(ledPin, OUTPUT);
}

void loop()
{
  if (mySerial.available()) {
    mySerial.read();
    digitalWrite(ledPin, HIGH);
  } else
    digitalWrite(ledPin, LOW);

  delay(100);  // human visibility
}

edit: added read()

jboyton:
Are you sure you have it wired correctly?
If it were me, I'd first just verify that I was getting an interrupt when the pin changed.

You could use the SoftwareSerial library for this, assuming it's correct. And assuming you have something connected to it that actually changes. For a simple test you could just take the bare wire and go from ground to Vcc.

#include <SoftwareSerial.h>

const int rx=6;
const int tx=-1; // not used and hopefully not a problem
SoftwareSerial mySerial(rx,tx);

const int ledPin = 0;

void setup()
{
mySerial.begin(9600);
pinMode(ledPin, OUTPUT);
}

void loop()
{
 if (mySerial.available()) {
   mySerial.read();
   digitalWrite(ledPin, HIGH);
 } else
   digitalWrite(ledPin, LOW);

delay(100);  // human visibility
}




edit: added read()

right that works :slight_smile:
so what have i dun wrong with this bit then?

buf[i]= mySerial.read(); 
    
     if (i == '1') {
           digitalWrite(ledPin, HIGH);
     }
     if (i == '2') {
           digitalWrite(ledPin, LOW);
     }

thanks for all your input

This would work...

Martin-X:
Read the serial data into a single char, then check that for a '1' or '2' to switch the LED.

Your array is named buf and it is indexed by i . You have stored the serial data in buf at position i , so compare the stored value against '1' or '2', not the index.

right ok am i getting there?

this still does not work but does flicker the led using any letter or number

    if (mySerial.available())  {
    buf[i]= mySerial.read(); 
    
    if (buf[i] = 1) {
           digitalWrite(ledPin, HIGH);
     }
     if (buf[i] = 2) {
           digitalWrite(ledPin, LOW);
     }
  }

is this a good way of doing this?
thanks all

Almost there, just tweak the if statements by adding single quotes around the '1' and '2' (like these) and make the single = into doubles ==.

like this?

    if (mySerial.available())  {
    buf[i]= mySerial.read(); 
    
    if (buf[i] == '1') {
           digitalWrite(ledPin, HIGH);
     }
     if (buf[i] == '2') {
           digitalWrite(ledPin, LOW);
     }
  }

still not getting anything back :confused:

That should work if everything was going to plan. As the code from jboyton gave you a flashing LED, it looks like a timing issue. The only thing I can suggest is trying a different baud rate or clock frequency.