Latching Input

Presently this is just the code required to get a push-to-make input to latch and LED on and off. A full explanation will follow.



* by Joshua Maxey
* created 26/03/2013
* requirements:
*  - 1 x LED (with suitable resistor)
*  - 1 x switch (push to make, non latching)
* instructions for this sketch can be found at the following URL:

int led = 11;
int button = 13;

void setup() {
  pinMode(led, OUTPUT);
  pinMode(button, INPUT_PULLUP);
  //we will use the serial port to alert us once the setup function has finished
  Serial.println("setup is complete");

void loop() {
  //if the button is pushed and the LED is off
  if (digitalRead(button) == LOW && digitalRead(led) == LOW) {
    //turn it on
    digitalWrite(led, HIGH);
    Serial.println("LED is on");
  //if the button is pushed and the LED is on
  if (digitalRead(button) == LOW && digitalRead(led) == HIGH) {
    //turn it off
    digitalWrite(led, LOW);
    Serial.println("LED is off");

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s