The following short program works fine on an Uno but not on a genuine Mega. It will work on the Mega if I use digitalRead() in place of digitalReadFast()
It seems to me strange that digitalWriteFast() works when digitalReadFast() does not.
I wonder if anyone has experienced this or knows of a solution.
Comparing against HIGH is dangerous, since for quite some time and some people, digitalRead() returns a "boolean" value, where 0 means LOW and anything else is fair game for HIGH.
In fact, the library you reference has:
In some relatively unrelated experiments, I've seen that the compiler will optimize away the !! when it can,
so if (digitalReadFast(8)) { // blah } still compiles to an SBIS instruction or equiv...
westfw:
Comparing against HIGH is dangerous, since for quite some time and some people, digitalRead() returns a "boolean" value, where 0 means LOW and anything else is fair game for HIGH.
I'm not sure I understand what you had in mind as the alternative as you say that 0 is LOW and anything else is HIGH.
The code seems to work properly if I change
if (selectVal == HIGH) {
to
if (selectVal != 0) {
Also I added a line to print the value of selectVal and tried it on a few different I/O pins. For some it was 32 and for another it was 4.
AFAIK digitalRead() returns a 0 or 1 and I had assumed that digitalWriteFast() would be the same.
Yet another option is to treat 'selectVal' as a bool:
if (selectVal) {
That would be my preferred style as well.
Note that digitalRead() is supposed to return a "PinStatus" enum these days, which is distinctly NOT a boolean. Which is unfortunate, IMO.
johnwasser:
Another option would be declaring 'selectVal' as 'bool' instead of 'byte'.
I have not had time to do more tests until now.
And, yes, declaring selectVal as bool works.
In fact the original manifestation of the problem was with code like this
if (digitalReadFast(inPin) == HIGH) {
which works fine with the regular digitalRead()
And a small modification like this
if ((bool) digitalReadFast(inPin) == HIGH) {
also solves the problem.
The fact remains that digitalReadFast() does not behave the same as digitalRead().
I appreciate the efforts of people to get the library updated. I don't plan to make any changes to my own copy of the library simply so that if I recommend it to others they will be using the same code as I am.