Switch \ Learning \ Wiring ALPHA 1.0
Author: Hernando Barragán
Description: Wiring is an electronic sketchbook and hardware electronics for developing ideas. It is a context for learning fundamentals of computer programming and prototyping with electronics within the context of the electronic arts.
Keywords: Wiring, Processing, Interactive Media, Electronic Arts, Programming, C, C++, Hernando Barragán

Search wiring.org.co:

This example is for Wiring version 0024+. If you have a previous version, use the examples included with your software. If you see any errors or have comments, please let us know.

switch by BARRAGAN http://barraganstudio.com
Demonstrates the use of digital pins and a switch. The pin used as input is connected to a switch and the pin used as output is the onboard LED When the switch is pressed, the light blinks, the light goes off when the switch is released.

    int direction = 1;  // used for the blinking cycle
    int switchPin = 0;  // digital pin to attach the switch
    int ledPin = 48;    // Wiring onboard LED

    void setup()
    {
      pinMode(switchPin, INPUT);  // sets digital pin 0 as input
      pinMode(ledPin, OUTPUT);    // sets digital pin 48 as output
    }

    void loop()
    {
      if(digitalRead(switchPin) == HIGH)  // if the switch is pressed
      {
        direction *= -1;                  // alternate between 1 or -1
        if (direction == 1)               // to decide to turn on or off the light
        {
          digitalWrite(ledPin, HIGH);     // turns the light on
        }
        else
        {
          digitalWrite(ledPin, LOW);      // turns the light off
        }
        delay(200);                       // wait 200 milliseconds
      }
      else                                // if switch not pressed
      {
        digitalWrite(ledPin, LOW);        // keeps the light off
      }
    }