Go Down

Topic: Using the RDM630 RFID Tag Reader and NewSoftwareSerial library (Read 4 times) previous topic - next topic

AWOL

Because it is mostly unnecessary (I have written one in the last thirty years, but it was deep in a Linux device driver, and required to be signed-off by a director), and has the potential of producing spaghetti code.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Koop

Quote
Code: [Select]
SoftwareSerial RFID =  SoftwareSerial(RFID_RX, RFID_TX);

 pinMode(RFID_TX, OUTPUT);//Define RFID TX pin as OUTPUT (Not nessesary)
 pinMode(RFID_RX, INPUT);//Define RFID RX pin as INPUT (Nessesary)

You have told the SoftwareSerial instance that the pins are its to manage. So, why are you diddling with them?

I'm changing their pinModes (even though its not really needed) because thats what it does in the SoftwareSerial tutorial:
http://arduino.cc/en/Reference/SoftwareSerialAvailable

Koop


Because it is mostly unnecessary (I have written one in the last thirty years, but it was deep in a Linux device driver, and required to be signed-off by a director), and has the potential of producing spaghetti code.
Fair enough.  It just seemed convenient and quick.  Its not like I used it a lot, only once.

Grumpy_Mike

Once is once too many.
Simply a GOTO is not a structured piece of code, it can be used to jump into the middle of structures that have not been set up with totally unpredictably consequences.
If you feel the need to use a GOTO then you have not thought out the problem correctly.

AWOL

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Koop


See what I mean about attracting flak?   ;)
Yep ;)  I didn't realise that Goto was such a taboo!  It must be drilled into your heads at uni!

PaulS

#21
Jul 12, 2012, 04:37 pm Last Edit: Jul 12, 2012, 04:40 pm by PaulS Reason: 1
Quote
I'm changing their pinModes (even though its not really needed) because thats what it does in the SoftwareSerial tutorial:

It's on the internet. It must be right. Not in this case:

Code: [Select]
SoftwareSerial::SoftwareSerial(uint8_t receivePin, uint8_t transmitPin, bool inverse_logic /* = false */) :
 _rx_delay_centering(0),
 _rx_delay_intrabit(0),
 _rx_delay_stopbit(0),
 _tx_delay(0),
 _buffer_overflow(false),
 _inverse_logic(inverse_logic)
{
 setTX(transmitPin);
 setRX(receivePin);
}


And, what does setTX do?
Code: [Select]
void SoftwareSerial::setTX(uint8_t tx)
{
 pinMode(tx, OUTPUT);
 digitalWrite(tx, HIGH);
 _transmitBitMask = digitalPinToBitMask(tx);
 uint8_t port = digitalPinToPort(tx);
 _transmitPortRegister = portOutputRegister(port);
}


And setRX?
Code: [Select]
void SoftwareSerial::setRX(uint8_t rx)
{
 pinMode(rx, INPUT);
 if (!_inverse_logic)
   digitalWrite(rx, HIGH);  // pullup for normal logic!
 _receivePin = rx;
 _receiveBitMask = digitalPinToBitMask(rx);
 uint8_t port = digitalPinToPort(rx);
 _receivePortRegister = portInputRegister(port);
}


So, the pin modes have already been set when you diddle with them.

Koop

#22
Jul 12, 2012, 04:44 pm Last Edit: Jul 12, 2012, 04:47 pm by Koop Reason: 1

Quote
I'm changing their pinModes (even though its not really needed) because thats what it does in the SoftwareSerial tutorial:

It's on the internet. It must be right. Not in this case:

Code: [Select]
SoftwareSerial::SoftwareSerial(uint8_t receivePin, uint8_t transmitPin, bool inverse_logic /* = false */) :
 _rx_delay_centering(0),
 _rx_delay_intrabit(0),
 _rx_delay_stopbit(0),
 _tx_delay(0),
 _buffer_overflow(false),
 _inverse_logic(inverse_logic)
{
 setTX(transmitPin);
 setRX(receivePin);
}


And, what does setTX do?
Code: [Select]
void SoftwareSerial::setTX(uint8_t tx)
{
 pinMode(tx, OUTPUT);
 digitalWrite(tx, HIGH);
 _transmitBitMask = digitalPinToBitMask(tx);
 uint8_t port = digitalPinToPort(tx);
 _transmitPortRegister = portOutputRegister(port);
}


And setRX?
void SoftwareSerial::setRX(uint8_t rx)
{
 pinMode(rx, INPUT);
 if (!_inverse_logic)
   digitalWrite(rx, HIGH);  // pullup for normal logic!
 _receivePin = rx;
 _receiveBitMask = digitalPinToBitMask(rx);
 uint8_t port = digitalPinToPort(rx);
 _receivePortRegister = portInputRegister(port);
}

So, the pin modes have already been set when you diddle with them.

Well it was on the official Arduino page, which is supposed to be moderated by experts like you and your counterparts, so it's not like I grabbed it off Wikipedia and stuck it in my code.  Either way, nowhere have I read that you don't need to do that (At least anywhere on here), so obviously I'm not the only one, and no harm was done - the code works perfectly fine - so I don't think that any of this matters.

PaulS

Quote
Well it was on the official Arduino page, which is supposed to be moderated by experts like you and your counterparts

The official Arduino page contains many, many errors. Not a one of us "experts" or moderators can change the official Arduino page.

I didn't mean to come across too hard on the issue. The pins belong to the instance (and it doesn't matter what the instance is an instance of), so they should not be diddled with by you (or any one else). Only the class that you give them to should diddle with them.

You can, of course, look at the code, like I did, to see what is necessary, and what isn't, in the future.

(That has to be a record for the number of commas I've ever put in one sentence.)

Koop

Quote
I didn't mean to come across too hard on the issue. The pins belong to the instance (and it doesn't matter what the instance is an instance of), so they should not be diddled with by you (or any one else). Only the class that you give them to should diddle with them.

You can, of course, look at the code, like I did, to see what is necessary, and what isn't, in the future.

Thanks for that.  Its hard to 'read' a person's emotions with only text to go by, and I thought that you were being one of those annoying nit-pickers, but you weren't so thats good :)

Quote
The official Arduino page contains many, many errors. Not a one of us "experts" or moderators can change the official Arduino page.

Well that's not good is it :/  It should be checked for errors more frequently... and all examples I've ever seen do the same thing I did so it must be stemming from that one coding mistake.  Thanks for all your help.  I'll be sure to read the header files before I do anything with libraries again.

Thanks to you all,
Koop

Grumpy_Mike

Quote
It must be drilled into your heads at uni!

A bit condescending.
When I went to Uni there was no instruction about code. We had to learn it ourselves.

When I taught at Uni, I never introduced it as a valid instruction.

dxw00d


AWOL

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Nick Gammon

At my first technical programming job I handed in my first piece of code to my supervisor. He said "rewrite it without the gotos". I did that, and haven't used them since.

I can think of one place where a goto might be useful, and that is in the regular expression library where an expert (Roberto Ierusalimschy) used it in one place in order to avoid consuming the stack when doing tail recursion. This is a guy who knows what he is doing. He is an associate professor of Informatics and a compiler-writer (Lua).

If you are trying to avoid using the stack, when doing tail recursion, fair enough.

For other cases, you don't need goto, and they obscure what the code is doing.


Koop

#29
Jul 13, 2012, 08:58 am Last Edit: Jul 13, 2012, 09:09 am by Koop Reason: 1
Quote
A bit condescending.

Quote
Some of us never attended a university.

Sorry, Grumpy_Mike, AWOL and dxw00d!  In no means was that meant to be condescending, I just assumed that seeing as though you guys are so good at this, and you knew to never, EVER, use a goto (except in the circumstance said by Nick) you had some form of tertiary education on the matter.  Being a schoolkid, I've never experienced uni or TAFE or community college or any tertiary education, so I have no idea of how or what they teach you there.  It was just an (obviously uneducated) guess :P

Go Up