Switching an Output to opposite to last status

I use the onboard LED on pin 13 as a simple visual monitor, to see that, 1; the program is running and 2; how fast the it get around the loop.

So I have a function like this:

void HeartBeat(void)
{
if (HeartBeatState>0) {
digitalWrite(13,HIGH);
HeartBeatState= 0;
}
else{
digitalWrite(13,LOW);
HeartBeatState= 1;
}
return;

Is there a way to use XOR on the output ?

I guess my question really is: What is the fastest way of switching an output pin ?

Cheers

K

fiddler:
I use the onboard LED on pin 13 as a simple visual monitor, to see that, 1; the program is running and 2; how fast the it get around the loop.

So I have a function like this:

void HeartBeat(void)
{
if (HeartBeatState>0) {
digitalWrite(13,HIGH);
HeartBeatState= 0;
}
else{
digitalWrite(13,LOW);
HeartBeatState= 1;
}
return;

Is there a way to use XOR on the output ?

I guess my question really is: What is the fastest way of switching an output pin ?

Cheers

K

I remember time ago someone asking for a DigitalWrite(TOGGLE); but I think it was never added. You can however use something like this:

boolean s;
void HeartBeat()
{
   digitalWrite(13,s=!s);
}

You can read outputs, so you could do.

digitalWrite(13, !digitalRead(13) );

Or you could have the hardware invert the pin by writing a 1 into the PIN instead of the PORT register.

Thanks guys, awesome :slight_smile:

fiddler:
I guess my question really is: What is the fastest way of switching an output pin ?

How fast is fast enough? I find it hard to believe you're actually going to notice the LED switching frequency if you're calling this code frequently enough for performance to be an issue.