Arduino Sketch ReadSwitchInput

Arduino Sketch ReadSwitchInput (1)

This sketch is used by Exercise: Read Switch Input.

Arduino Sketch ReadSwitchInput (1)

Full Source Code

The full code is all in one file ReadSwitchInput.ino.

// ReadSwitchInput - read a digital input and report its value using the LED and serial monitor
//
// Copyright (c) 2016, Garth Zeglin.  All rights reserved. Licensed under the
// terms of the BSD 3-clause license as included in LICENSE.
//
// This program assumes that:
//
//  1. An SPST switch connected between digital pin 2 and ground, and a 10K
//  pullup resistor between digital pin 2 and +5V.
//
//  2. The serial console on the Arduino IDE is set to 9600 baud communications speed.
// ================================================================================
// Define constant values.

// The wiring assignment.
#define SWITCH_PIN 2

// ================================================================================
// Configure the hardware once after booting up.  This runs once after pressing
// reset or powering up the board.

void setup()
{
  // Initialize the serial UART at 9600 bits per second.
  Serial.begin(9600);

  // Initialize the hardware digital pin 13 as an output.  The 'OUTPUT' symbol
  // is pre-defined by the Arduino system.
  pinMode(LED_BUILTIN, OUTPUT);
  
  // Initialize the switch pin for input.
  pinMode(SWITCH_PIN, INPUT);
}


Read more: Arduino Sketch ReadSwitchInput

Leave a Comment

Your email address will not be published. Required fields are marked *