Pages: [1]   Go Down
Author Topic: How to store button values(Trigger)?  (Read 142 times)
0 Members and 1 Guest are viewing this topic.
Chennai
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Just because it hurts, doesn't mean i shouldn't try it
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This is the program which i found in Arduino Forum for Controlling a LED using a Push Button.
int ledPin = 12;                // LED is connected to pin 12
int switchPin = 2;              // switch is connected to pin 2
int val;                        // variable for reading the pin status


void setup() {
  pinMode(ledPin, OUTPUT);      // Set the LED pin as output
  pinMode(switchPin, INPUT);    // Set the switch pin as input
}


void loop(){
  val = digitalRead(switchPin);   // read input value and store it in val
  if (val == HIGH) {               // check if the button is pressed
    digitalWrite(ledPin, LOW);   // turn LED on
  }
  if (val == LOW) {              // check if the button is not pressed
    digitalWrite(ledPin, HIGH);    // turn LED off
  }
}

Now i would like to know how to store the value of the Button Input in Memory. For eg: If i press the Button it should store the value as HIGH and give the HIGH signal to LED and wait till i press the button again to Send LOW signal to LED. In simple words, it should turn ON and OFF the LED by remebering the Button Trigger
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 548
Posts: 46048
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
For eg: If i press the Button it should store the value as HIGH
You are already doing this, storing the value in val. Why is this not sufficient?

val is going to be overwritten on every pass through loop.

It isn't clear what you want to do.
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 65
Posts: 2929
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Oh, you want a latch.
Try this:

Boolean latch= false;

If(ButtonPress == HIGH) {
latch = !latch;

digitalWrite(LEDpin, latch ? HIGH : LOW);
}

This is the very compact way to do it.
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Pages: [1]   Go Up
Jump to: