Go Down

Topic: suggestion to add TOGGLE in digitalWrite() (Read 13392 times) previous topic - next topic


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


Aug 18, 2010, 10:45 pm Last Edit: Aug 18, 2010, 10:48 pm by Osgeld Reason: 1
what speed does it toggle at? (and what if I want it faster or slower)


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

pin = !pin works
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:

Code: [Select]

// Example is for digital pin 2.

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


Aug 19, 2010, 03:10 pm Last Edit: Aug 19, 2010, 03:10 pm by mpeuser Reason: 1
There seems to be a problem with the forum's software
The code should read
Code: [Select]


The code should read

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


Aug 19, 2010, 10:32 pm Last Edit: Aug 20, 2010, 11:40 am by mpeuser Reason: 1

I see....
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!


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...

I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.


coughheaderspacingcough (although its way too late for that)

now what were you saying?



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


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...

Go Up