DigitalToggle Library for Arduino
Author:  David Knaack
Contact: davidknaack@gmail.com


Navigation


History

0.1 2009-07-6: Initial Release


Description

DigitalToggle is a library for the Arduino.

It provides the functions digitalToggle() and digitalToggleFast() which toggle the state of the specified digital pin by looking up the appropriate AVR PIN register for the digital pin and then writing the appropriate bit in that register, causing the corresponding PORT bit to be toggled.

This is useful when it is desirable to change the state of a pin, regardless of the current state.


Download, install and import

Download here: Attach:DigitalToggle.zip

Put the DigitalToggle folder in "hardware\libraries\".

In the Arduino IDE, create a new sketch (or open one) and select from the menubar "Sketch->Import Library->DigitalToggle".


Setup

No setup is required, simply call the function.


Methods

void digitalToggle(byte Pin);

Toggle the state of the specified digital pin. Note that digitalToggle does not check whether the pin has been set up as an output or if it is being used as a PWM output.

The first use of digitalToggle() adds about 58 bytes to the compiled code, subsequent calls add about 8 bytes each.

void digitalToggleFast(byte Pin);

Toggle the state of the specified digital pin. This differs from digitalToggle() in that it is declared with a #define in order to eliminate the overhead of one function call, providing about a 20% reduction in the time needed to toggle the pin at the cost of code size.

Each call to digitalToggleFast() adds 44 bytes to the compiled code.


Example

Blinking

  1. /*
  2.  * Description:
  3.  * Blink an LED using the DigitalToggle library.
  4.  *
  5.  */
  6. #include <DigitalToggle.h>
  7.  
  8. int ledPin = 13;
  9.  
  10. void setup() {
  11.   pinMode(ledPin, OUTPUT);
  12. }
  13.  
  14. void loop(){
  15.   delay(500);
  16.   digitalToggle(ledPin);
  17. }

User Comments

  • note: I had to edit DigitalToggle.cpp and add an include of Arduino.h in order to get this to compile. --cellularmitosis 2012/11/3


Information about this page

Last Modified: November 03, 2012, at 05:26 PM
By: cellularmitosis

Share