Pages: [1]   Go Down
Author Topic: ATTiny84 and SerialSofware  (Read 506 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I am using the SoftwareSerial library to communicate with my ATTiny84. I can see that I am able to send and receive data via the FTDI breakout. In the Serial Monitor, I am receiving "junk" and it seems I am sending "junk". I have set the baud rate to 300, but I don't think it is a speed issue. Any ideas?

Here is my code:

Code:
#include <SoftwareSerial.h>

#define rxPin 2
#define txPin 3
// Attach the serial display's RX line to digital pin 2
SoftwareSerial mySerial(rxPin,txPin); // pin 2 = TX, pin 3 = RX (unused)
int led = 0;
int GREEN = 7;
int val = 109;

void setup()
{
  mySerial.begin(300); // set up serial port for 300 baud
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  pinMode(led, OUTPUT);
  pinMode(GREEN, OUTPUT);
  delay(500); // wait for display to boot up
}

void loop()
{
  val = mySerial.read();
  mySerial.println(val);
 
  mySerial.write("Hello, world!");
  
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(500);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(500);
  
  if (val == 110) { // "n" = 110 in dec from ascii
    
    digitalWrite(GREEN, HIGH);   // turn the LED on (HIGH is the voltage level)
    
  }
  
}
« Last Edit: February 16, 2013, 07:29:47 am by PhillyNJ » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49365
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  val = mySerial.read();
  mySerial.println(val);
Read and echo the first byte available in the buffer, even if there is nothing in the buffer == garbage.

Code:
  mySerial.write("Hello, world!");
Use a binary method to send ASCII data. Novel idea. I wonder why it's not more common.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Use a binary method to send ASCII data. Novel idea. I wonder why it's not more common.

Hi Thanks for the help. - Can you provide an example?

Read and echo the first byte available in the buffer, even if there is nothing in the buffer == garbage.

I understand, Garbage in, garbage out. So if I send a char, e.g. 'n', val should = '110' (ASCII) and should print out to serial '110'. Is this correct? Or will val always equal garbage. I am learning, so I appricate all the 'dumb down' help I can get  smiley
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49365
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Can you provide an example?
I presume that you are able to read the documentation on the SoftwareSerial class, and see that it derives from Stream which derives from Print. I presume that you are then able to took at the various methods offered - write() for binary data and print() for ASCII data. I further presume that examples exist in the documentation.

Quote
So if I send a char, e.g. 'n', val should = '110' (ASCII) and should print out to serial '110'. Is this correct?
Yes. However, loop() executes quite often - millions of times per second, depending on what happens in each iteration of loop. Between the time that you send an 'n' and the Arduino receives it, it could have send 5000 garbage characters back to the sender. Then, one good character or string, then a few thousand more garbage characters.

There is an available() method that bears investigating, to prevent reading what isn't there.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I understand the docs and understand what you're saying. The issue isn't that I get SOME junk, I am saying when I send a char, it will print out to the serial monitor as 'ÿ'. So even if I use binary or ASCII, the result is the same.

So,

Code:
mySerial.write("Hello world");

shows in the serial monitor as 'ÿ'

Below is an example that shows that I a receiving data on the ATTiny84, and my serial monitor is receiving data:

Code:
#include <SoftwareSerial.h>

#define rxPin 1
#define txPin 2
#define ledPin 8

// set up a new serial port
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);
byte pinState = 0;

void setup()  {
  // define pin modes for tx, rx, led pins:
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
  mySerial.write("Hello World - SoftwareSerial");
}

void loop() {
  // listen for new serial coming in:
  if(mySerial.available()){
 
    char someChar = mySerial.read();
    // print out the character:
    mySerial.write(someChar);
    // toggle an LED just so you see the thing's alive. 
    // this LED will go on with every OTHER character received:
    toggle(ledPin);
   
  }
 
}

void toggle(int pinNum) {
  // set the LED pin using the pinState variable:
  digitalWrite(pinNum, pinState);
  // if pinState = 0, set it to 1, and vice versa:
  delay(50);
  pinState = !pinState;
}

As you can see I am only toggling the led if I am receiving data on the ATTiny84. Once again, I get 'ÿ' in tbe serial monitor.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49365
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The y with the two dots over is is what happens when you send -1 to the serial monitor. That is the return code from read() when there is nothing to read. All those funny ys are from printing -1s, not from writing "Hello world", which you shouldn't be doing.

When are you intending to start using the correct function (print(), if you need another hint) to send ASCII data?

Oh, and what clock rate are you using with the ATTiny? Have you selected the correct speed on both ends? If you are using other than a 16MHz chip, the speeds won't be the same.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

When are you intending to start using the correct function (print(), if you need another hint) to send ASCII data?

Oh, and what clock rate are you using with the ATTiny? Have you selected the correct speed on both ends? If you are using other than a 16MHz chip, the speeds won't be the same.

Thanks - starting using print as hinted  smiley, also set my clock speed to 8 MHz. I followed these instructions (http://hlt.media.mit.edu/?p=1695):

Code:
Configuring the ATtiny to run at 8 MHz (for SoftwareSerial support)

By default, the ATtiny’s run at 1 MHz (the setting used by the unmodified “ATtiny45″, etc. board menu items). You need to do an extra
step to configure the microcontroller to run at 8 MHz – necessary for use of the SoftwareSerial library. Once you have the microcontroller
connected, select the appropriate item from the Boards menu (e.g. “ATtiny45 (8 MHz)”). Then, run the “Burn Bootloader” command
from the Tools menu. This configures the fuse bits of the microcontroller so it runs at 8 MHz. Note that the fuse bits keep their value
until you explicitly change them, so you’ll only need to do this step once for each microcontroller. (Note this doesn’t actually burn a
bootloader onto the board; you’ll still need to upload new programs using an external programmer.)

Still no luck
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49365
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What baud rate are you using on the tiny? On the Serial Monitor?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've tried 9600 and 4800.
Logged

Denmark
Offline Offline
Edison Member
*
Karma: 35
Posts: 1072
Happy Hobbyist
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Are you sure you are using the right pin.
The sketch below will use pin D3/A7/PA7  physical pin 6 to output from Serial

Edit:
I use this core

http://code.google.com/p/arduino-tiny/

And Arduino 1.01

As far as I  remember there was problems with SoftwareSerial in version 1.00


Code:
#include <SoftwareSerial.h>

#define rxPin 2
#define txPin 3
SoftwareSerial mySerial(rxPin,txPin);
void setup() {
 mySerial.begin(9600);
}

void loop() {
  mySerial.println("Testing ");
  
}
« Last Edit: February 16, 2013, 12:46:36 pm by Erni » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Erni - you rock! My pins were off by one.. thanks so much.

Also thanks to Paul. I appreciate all the help and guidance.
Logged

Pages: [1]   Go Up
Jump to: