2

I have put together an Arduino circuit that turns the led's off when the button is pressed. How do I code it so when I press it once it comes on and stays on and will only turn off once its pressed again? Any help would be appreciated

My Current code is:

  int ledred = 12;
  int ledgreen = 8;
  int BUTTON = 4;
  int speakerPin = 1;

  void setup() {
   // initialize the digital pin as an output.
   Serial.begin(9600);
   pinMode(ledgreen, OUTPUT);
   pinMode(ledred, OUTPUT);
   pinMode(BUTTON,INPUT);
  }

void loop() {
 if(digitalRead(BUTTON) == HIGH){
  digitalWrite(ledred,HIGH);
  digitalWrite(ledgreen,HIGH);
   }else
   {
    digitalWrite(ledred,LOW);
    digitalWrite(ledgreen,LOW);
   }
  }
Fruchtzwerg
  • 10,999
  • 12
  • 40
  • 49
XieTy
  • 49
  • 2
  • 4

2 Answers2

1

If all you want is do this, you can use one of the interrupt pins and watch for the RISING (or FALLING) event.

Something similar to this example:

const byte ledPin = 13;
const byte interruptPin = 2;
volatile byte state = LOW;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), blink, RISING);
}

void loop() {
  digitalWrite(ledPin, state);
}

void blink() {
  state = !state;
}

Mind that you may still need some debouncing strategy.

Also, you don't need to use an interrupt for that, but then you'd need some edge-detection algorithm. These are quite well explained in the debouncing article above. I personally prefer these, since interrupt pins in the UNO board are precious enough not to be used with humble button pressings... :o)

heltonbiker
  • 26,657
  • 28
  • 137
  • 252
  • This seems like a good answer except for the first sentence. I don't see how watching for rising or falling signals there will tell you if it's a new button press. – Adrian McCarthy May 20 '16 at 18:50
  • @AdrianMcCarthy, you mean because of bouncing? Because I am sure I have already used interrupts for buttons. It only didn't go that far precisely because of bouncing. With an ideal (or hardware-debounced) button, it should work fine. – heltonbiker May 20 '16 at 23:06
1
/*
  Debounce

  Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
  press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's a
  minimum delay between toggles to debounce the circuit (i.e. to ignore noise).

  The circuit:
  - LED attached from pin 13 to ground
  - pushbutton attached from pin 2 to +5V
  - 10 kilohm resistor attached from pin 2 to ground

  - Note: On most Arduino boards, there is already an LED on the board connected
    to pin 13, so you don't need any extra components for this example.

  created 21 Nov 2006
  by David A. Mellis
  modified 30 Aug 2011
  by Limor Fried
  modified 28 Dec 2012
  by Mike Walters
  modified 30 Aug 2016
  by Arturo Guadalupi

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Debounce
*/

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;    // the number of the pushbutton pin
const int ledPin = 13;      // the number of the LED pin

// Variables will change:
int ledState = HIGH;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);

  // set initial LED state
  digitalWrite(ledPin, ledState);
}

void loop() {
  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);

  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH), and you've waited long enough
  // since the last press to ignore any noise:

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer than the debounce
    // delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
        ledState = !ledState;
      }
    }
  }

  // set the LED:
  digitalWrite(ledPin, ledState);

  // save the reading. Next time through the loop, it'll be the lastButtonState:
  lastButtonState = reading;
}
UUstatic
  • 11
  • 4