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.

LED_latch

Code


/*
* 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.begin(9600);
  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");
    delay(200);
  }
  //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");
    delay(200);
  }
}

Leave a Reply

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

WordPress.com Logo

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

Google+ photo

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

Twitter picture

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

Facebook photo

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

Connecting to %s