Pages: 1 [2] 3 4   Go Down
Author Topic: Softwareserial  (Read 6518 times)
0 Members and 2 Guests are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 9
Arduino 1 - Programmer 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I just wrote a simple program that creates a software serial and just keeps printing the ASCII character T.

My Mini and USB are connected as shown in: http://www.arduino.cc/en/uploads/Guide/ArduinoMiniBreadboardPhoto.jpg

I programmed my Mini using the Mini USB, disconnected the whole thing, connected the Mini USB to the SoftwareSerial ports, powered it up and monitored the USB (using the Arduino IDE). It didn't work and once more the Mini flashes the LED 3 times every 5 seconds or so.

Code follows:

#include <SoftwareSerial.h>

#define rxPin 10
#define txPin 11
#define ledPin 13

// set up a new serial port
SoftwareSerial mySerial(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);
}

void loop() {  
  // print 'T'  
  mySerial.print(84, BYTE);
  // show that I'm alive
  toggle(13);
  // take a break
  delay(500);
}

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:
  pinState = !pinState;
}


It is so simple that there should be no reason for it not to be working. Any thoughts on this?
« Last Edit: April 24, 2007, 12:41:17 pm by tms.martins » Logged

Forum Administrator
Cambridge, MA
Offline Offline
Faraday Member
*****
Karma: 9
Posts: 3538
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I've used the software serial library to send characters via the Mini USB to the computer.  It works.

tmsm: it sounds like you have a more general problem.  If the LED flashes 3 times every 5 seconds or so it means that the bootloader is restarting over and over again, meaning that there's no program on your Arduino.  Does the same thing happen no matter what program you upload (e.g. the LED blink)?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 9
Arduino 1 - Programmer 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No, I've also been interfacing with an Hitachi H48C accelerometer module and an Hitachi HM55B compass module (both from Parallax) through shifting in and out and this works fine.

Also in previous attempts to use SoftwareSerial, this rebooting does not happen if I never get to call SoftwareSerial.read() or SoftwareSerial.write(). If I do, however, the program executes just fine until the call, at which point the Mini reboots.

If the fact that the bootloader keeps on restarting means that there is no program, then what does it mean if it restarts during the exectution of a program? That somewhere the program itself is somehow corrupted?  :-?

I am wondering if there might be something wrong with the library itself in my installation of Arduino 7.
« Last Edit: April 24, 2007, 04:02:12 pm by tms.martins » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 9
Arduino 1 - Programmer 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

...and howcome using the function SWprint copied from

http://www.arduino.cc/en/Tutorial/SoftwareSerial

actually manages to send something (altough not exactly the character I tell ito send), while a properly set up SoftwareSerial reboots at the print() call?

Geez, FFS...!
Logged

Forum Administrator
Cambridge, MA
Offline Offline
Faraday Member
*****
Karma: 9
Posts: 3538
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The problem is that Arduino 0007 doesn't recompile your libraries when you switch from the ATmega8 (which is the default) to the ATmega168 (which should be the default, since all new boards come with it).  You need to go in the lib/targets/libraries/SoftwareSerial sub-directory of your Arduino application directory and delete SoftwareSerial.o.  Then re-upload your sketch and it should work.  

This is fixed in Arduino 0008, which should come out soon.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 9
Arduino 1 - Programmer 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks!

I am not quite sure however that recompiling the .o file would work because I tried it earlier today. At least in my case, the Mini still rebooted. But I have to re-check this tomorrow...  :-/

Nevertheless I actually solved the problem this afternoon by resorting to the code in
http://www.arduino.cc/en/Tutorial/SoftwareSerial
and changing the delay values for 9600 baud communication.

This code was working (no reboots) but printing the wrong data. After some experiments I guessed it might be some timing problem. The Atmega168 is faster than the Atmega8 so maybe this is the source of the timing problem?   :-?

The proper delay was obtained through a brute-force trial-and-error program I wrote. It kept decrementing the delay starting at 104 msec (1000000/9600 rounded down), each time printing the character 'T'; the character would appear correctly around 100 msec delay (and some steps further) so that's what I am using now. Changing also the half-bit delay, it works for both input and output.  8-)

Checking out SoftwareSerial.cpp afterwards, the reboot issue would disappear if - instead of calculating the delay - I used this fixed value (obviously only valid for 9600 baud rate).

Hope this may be of use to someone other than me.

Cheers,
TMSM
Logged

Forum Administrator
Cambridge, MA
Offline Offline
Faraday Member
*****
Karma: 9
Posts: 3538
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Huh.  On the Arduino board, an ATmega168 runs at the same speed as an ATmega8 since either will use the 16 MHz clock (the ATmega168 can use clocks as fast as 20 MHz, but for compatibility we've stuck with 16 MHz).  The timing of the SoftwareSerial is pretty fusy, though, so I'm not surprised you had to adjust it.

I'm confused about your last line...  the software serial library only works with a fixed delay, not a calculated one?  Did you try putting the code back to the way it was (calculated delay) and trying it again?  I can't imagine the chip resets because of a slight modification in the timing, I strongly suspect it depends which chip the code was compiled for.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 9
Arduino 1 - Programmer 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yeah, it's weird as hell.

Nevertheless the Mini rebooted whenever I made a call to read() or write() of the SoftwareSerial with the original library. Replacing the calculation did make it work (still with the delay problem).

Tomorrow I give it another shot since I am not 100% sure - once I saw it work I left it alone and moved to other pending tasks - and obviously I believe you know what you are talking about smiley-wink

Today's work was already enough for me  :o
Logged

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

hi tmsm69, it seems as i have been having many of the same issues as you. i too, was able to see something come through using the - http://www.arduino.cc/en/Tutorial/SoftwareSerial - code. but yes, it did not print properly. i am going to try to use your suggestion - thanks. i will let you know how it goes.

mellis i will also try your suggestion and see how things go.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 9
Arduino 1 - Programmer 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yep, removing the .o and recompiling seems to do the trick, so simple.

Thanks!!!  [smiley=thumbsup.gif]

PS: I just loooooove to complicate  [smiley=grin.gif]
Logged

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

wow. thanks mellis deleting the .o file also worked for me. i'm very excited that this works now!
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 9
Arduino 1 - Programmer 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I am wondering now if there is a nice way to make the softwareSerial.read() call non-blocking.
I mean, this is its major weakness as I see it, and I believe that if it were easy to do it then it would have already been done in the IDE distribution.

Still, does anyone know a workaround for this, such as using an interrupt or (as I think maybe more simple) subtituting the "while (digitalRead(rx));" by a timeout?

Greets to all the Arduinoers out there!  smiley-wink
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey all. I'm having an odd problem with my Arduino Diecimila. If I try the Software Serial example on pins 0 and 1 and use the arduino IDE to send a character, it works fine. However, if I use any other two pins (2 and 3 for example, softwareserial.read() doesn't wait for a byte to be sent. It just automatically reads in a 0, which means that nothing is ever actually read. I can have the pins not plugged into anything and the software serial read() function will still automatically read in a 0. Anyone have a similar problem? (Using Arduino 10, btw)
Logged

0
Offline Offline
God Member
*****
Karma: 0
Posts: 731
skcor oniudrA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Check this thread, there's another library for Diecimila somewhere in here:-

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1202245205/0

It's a common problem, here's my solution:-

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1205243372

Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Awesome. I guess I'll try out the library as soon as I get home from work. As for your solution, well, I haven't yet graduated to straight AVR programming yet. Thank you very much for your help smiley
Logged

Pages: 1 [2] 3 4   Go Up
Jump to: