Back to Parent

// We will be using D2 to control our LED
int ledPin = D2;

// Our button wired to D3
// We wire D0 to the middle terminal on the switch
// And any of the two other terminals to ground
int switchPin = D3;

void setup()
{

  // For input, we define the
  // switch as an input-pullup
  // this uses an internal pullup resistor
  // to manage consistent reads from the device

  pinMode( switchPin , INPUT_PULLUP); // sets pin as input

  // We also want to use the LED

  pinMode( ledPin , OUTPUT ); // sets pin as output

}

void loop()
{
   // find out if the button is pushed
   // or not by reading from it.
   int buttonState = digitalRead( switchPin );

  // Using a pulldown resistor we get a LOW
  // Signal when its on
  
  if( buttonState == LOW )
  {
    // turn the LED On
    digitalWrite( ledPin, HIGH);
  }else{
    // otherwise
    // turn the LED Off
   digitalWrite( ledPin, LOW);

  }


}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0