Switch (button)

This example is for Wiring version 0027+. 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.

Multiple switches by BARRAGAN http://barraganstudio.com

Demonstrates the use of digital pins and switches. The pin used as input is connected to a switch


int switchPin_0 = 0; // switch connected to pin 0 
int switchPin_1 = 1; // switch connected to pin 1 
int switchPin_2 = 2; // switch connected to pin 2 
int switchPin_3 = 3; // switch connected to pin 3 
int switchPin_4 = 4; // switch connected to pin 4 

void setup() 
{
  pinMode(switchPin_0, INPUT);  // sets digital pin 0 as input
  pinMode(switchPin_1, INPUT);  // sets digital pin 0 as input
  pinMode(switchPin_2, INPUT);  // sets digital pin 0 as input
  pinMode(switchPin_3, INPUT);  // sets digital pin 0 as input
  pinMode(switchPin_4, INPUT);  // sets digital pin 0 as input
  Serial.begin(9600);
}

void loop() 
{
  if(digitalRead(switchPin_0) == HIGH)  // if the switch is pressed 
  {
    Serial.print("ON ");
  }
  else                                // if switch not pressed
  {
    Serial.print("OFF ");
  }
  if(digitalRead(switchPin_1) == HIGH)  // if the switch is pressed 
  {
    Serial.print("ON ");
  }
  else                                // if switch not pressed
  {
    Serial.print("OFF ");
  }
  if(digitalRead(switchPin_2) == HIGH)  // if the switch is pressed 
  {
    Serial.print("ON ");
  }
  else                                // if switch not pressed
  {
    Serial.print("OFF ");
  }
  if(digitalRead(switchPin_3) == HIGH)  // if the switch is pressed 
  {
    Serial.print("ON ");
  }
  else                                // if switch not pressed
  {
    Serial.print("OFF ");
  }
  if(digitalRead(switchPin_4) == HIGH)  // if the switch is pressed 
  {
    Serial.println("ON");
  }
  else                                // if switch not pressed
  {
    Serial.println("OFF");
  }
  delay(100);
}