i Know there is a library called "digitalToggle" that enables one to toggle a pin, But I found it easier to add the function to my Wiring_digital.c and Arduino.h in the core
I copied digitalWrite and changed a line to get a toggle method:
void digitalToggle(uint8_t pin)
{
uint8_t timer = digitalPinToTimer(pin);
uint8_t bit = digitalPinToBitMask(pin);
uint8_t port = digitalPinToPort(pin);
volatile uint8_t *out;
if (port == NOT_A_PIN) return;
// If the pin that support PWM output, we need to turn it off
// before doing a digital write.
if (timer != NOT_ON_TIMER) turnOffPWM(timer);
out = portOutputRegister(port);
uint8_t oldSREG = SREG;
cli();
if(*out & bit){ // This is all that is different from digitalWrite
*out &= ~bit;
}
else{
*out |= bit;
}
SREG = oldSREG;
}
I have been using toggling a lot to make heartbeat LEDs or to reverse motor directions etc.
it might be useful to others.
I only discovered "
http://playground.arduino.cc/Code/DigitalToggle" afterwards :(