suggestion to add TOGGLE in digitalWrite()

hello,
i didn't do much of a research about it (not much free time), but i think the library does not provide an easy/efficient way to toggle a pin.

anyway, i modified wiring.h and added:
...
#define HIGH 0x1
#define LOW 0x0
#define TOGGLE 0x2 // added line
...

and wiring_digital.c and changed to:
if (val == LOW) *out &= ~bit;
if (val == HIGH) *out |= bit; // added line
__ if (val == TOGGLE) *out ^= bit; // added line__
__ // replaced line // else *out |= bit;__

now it looks like i can write for example:
digitalWrite(ledPin, TOGGLE);
instead of calling other functions or keeping the state of the pin in the program.

if this modification does not create any issues, i think it would be a nice addition to a next version.

  • chris

what speed does it toggle at? (and what if I want it faster or slower)

:wink:

and it already keeps the state of the pin that is why

pin = !pin works
[edit]
oh nevermind, i just re-read that, yea I guess that could be more noob friendly than the above, so why not [/edit]

so why not

Well, changing the "any non-zero value causes a 1 to be written" behavior may screw up any number of existing sketches...

Well, changing the "any non-zero value causes a 1 to be written" behavior may screw up any number of existing sketches...

I agree to this - it really is not wise to change this behavior considering backwards compatibility.

That AVR's support a lesser known direct pin toggle, so for those who don't mind using direct port IO, you can do toggle as follows:

// Example is for digital pin 2.

PIND = _BV(PIND2);  // toggle pin2 on AtMega168/328

There seems to be a problem with the forum's software
The code should read

PIND ^= _BV(PIND2)

The code should read

No, it should not! Go and look it up.

:o

I see....
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1266933248/3#3

And it works for the 168 and 328 only, not with old mega8

Ah. Using the PINx magic toggle port, and a variation of the FastDigitalWrite macro that has been floating around, it should be possible to create a "digitalToggle()" function (macro) that takes up no space unless it is used, and operates in a single cycle if all the arguments are constant, and IT DOESN'T HAVE TO BE BACKWARD COMPATIBLE WITH ANYTHING!
:slight_smile:

Well, changing the "any non-zero value causes a 1 to be written" behavior may screw up any number of existing sketches...

I don't necessarily think this is "one of those times", but sometimes you have to decide to say "fsck backwards compatibility" and make a change "for the better".

Deprecate and move on...

:wink:

coughheaderspacingcough (although its way too late for that)

now what were you saying?

IMHO he wanted to say "To hell with compatibility!" ;D

Hello,
I think it is a good idea.
The same thing should be done to reverse the direction of a pin
(an input becomes an output, and an output becomes an input).
It would be great to write
pinMode(ThisPin, REVERSE);
Best regards,

well, i can not see why one should use macro's, or secret codes, or any other tricks to do what the digitalWrite() function should do anyway.

where is the logic in it then? if we do direct port manipulation, then digitalWrite() is completely useless (it is bigger and slower anyway),

on the other hand, if for any reasons we want a digitalWrite(), it should do all 3 useful bit manipulations (and, or, xor)

as for backwards compatibility:
this thing: "any non-zero value causes a 1 to be written"
looks seriously wrong anyway...