Toggle

From Pinguino
Jump to: navigation, search

Name

toggle

Description

toggle "toggles" the level on the specified pin. If the pin is LOW (=0) its level will be changed to HIGH (=1) and if the pin is HIGH (=1) its level will be changed to LOW (=0).

Since Beta X

Syntax

void toggle(u8 pin);
Parameters
  • pin = pin to change
Returns
[None]

pin must be configured for output using pinMode

Example

In this example we see how to toggle a the output of a pin using two different methods. The first method is used for the green led and simply uses the toggle() command.

The second method is used for the yellow led and reads the pin value then inverts it using (!) - the pin is then set using digitalWrite(). [code]

/* +---------------------------------------------------------------------+
   | toggle_test.pde           by Jan Zumwalt         rev 2015.12.17     |
   +---------------------------------------------------------------------+
   | Copyright: COPYRIGHT by Jan Zumwalt                                 |
   |            Licensed under the GNU public license.                   |
   | Build    : Olimex Pic32 Micro dev brd w/Pinguino.cc IDE (V11)       |
   | Purpose  : Demonstrate toggle command                               |
   +---------------------------------------------------------------------+  */

 // --------   ARDUINO COMPATIBILITY   --------
 // GREENLED   = IDE 32, LED1, USERLED, GREENLED
 // YELLOWLED  = IDE 10, LED2, YELLOWLED 
 
 // --------       USER DEFINED       --------
 #define BUTTON 13   // IDE 13, BUTTON up=1, dn=0
 
 void setup() {      // setup code, run once
   pinMode(GREENLED, OUTPUT);  
   pinMode(YELLOWLED, OUTPUT);  
 }
  
 void loop() {                                        // main code, loops forever 
   toggle(GREENLED);                                  // 1st method
   digitalWrite(YELLOWLED,!digitalRead(YELLOWLED));   // 2nd method (custom)
   delay(100);                                        // blink 10x per sec
 }

Bugs

See also

Comments