김아연gledel
Published © GPL3+

Reading a SPDT Switch

Using an SPDT switch, I will turn on the LED.

BeginnerShowcase (no instructions)30 minutes1,382
Reading a SPDT Switch

Things used in this project

Hardware components

Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Power MOSFET N-Channel
Power MOSFET N-Channel
×1
LED (generic)
LED (generic)
×1
Resistor 100 ohm
Resistor 100 ohm
×1

Story

Read more

Schematics

Reading a SPDT switch

Code

Reading a SPDT switch

Arduino
/*
  SparkFun Inventor's Kit
  Example sketch 06

  SPDT Switch

  Use a Single Pole Double Throw Switch (SPDT) to select an LED to blink

  This sketch was written by SparkFun Electronics,
  with lots of help from the Arduino community.
  This code is completely free for any use.
  Visit http://learn.sparkfun.com/products/2 for SIK information.
  Visit http://www.arduino.cc to learn about the Arduino.
*/


// Create constants for the pins we will be using
const int switchPin = 3;
const int led1Pin = 12;
const int led2Pin =  13;


void setup()
{
  // Set up the switch pins to be an input:
  pinMode(switchPin, INPUT);

  // Set up the LED pins to be an output:
  pinMode(led1Pin, OUTPUT);
  pinMode(led2Pin, OUTPUT);
}


void loop()
{
  // variables to hold the switch state
  int switchVal;

  // Since a switch has only two states, either HIGH (3.3V)
  // or LOW (GND) there is now way for you to have a floating point situation so there       //is no need for a pull-down resistor.

  //store the switch value to the switchVal variable
  switchVal = digitalRead(switchPin);

  //if switchVal is HIGH blink led1Pin
  if (switchVal == HIGH)
  {
    digitalWrite(led1Pin, HIGH);
    delay(500);
    digitalWrite(led1Pin, LOW);
    delay(500);
  }
  //else blink led2Pin
  else
  {
    digitalWrite(led2Pin, HIGH);
    delay(500);
    digitalWrite(led2Pin, LOW);
    delay(500);
  }
}

Credits

김아연

김아연

5 projects • 1 follower
gledel

gledel

100 projects • 115 followers
Looking back on my childhood, I was happy when I was making something and I was proud of myself. "Making is instinct!"

Comments