Skip to main content
added 4 characters in body
Source Link

I just grabbed the version from the ones above here and made is somewhat smaller in terms of code. Feel free to use it in your Arduino app.

// This will store the last known state of the button
int lastButtonState = LOW;

void loop()
{
  // Get the current state of the button
  int currentButtonState = digitalRead(buttonPin);

  // Has the button gone high since we last read it?
  if (newButtonStatecurrentButtonState == HIGH && lastButtonState == LOW) {
     // Switch the state of the output
     digitalWrite(ledPin, !digitalRead(ledPin));
  }

  // Store the button's state so we can tell if it's changed next time round
  lastButtonState = newButtonState;currentButtonState;
}

I just grabbed the version from the ones above here and made is somewhat smaller in terms of code. Feel free to use it in your Arduino app.

// This will store the last known state of the button
int lastButtonState = LOW;

void loop()
{
  // Get the current state of the button
  int currentButtonState = digitalRead(buttonPin);

  // Has the button gone high since we last read it?
  if (newButtonState == HIGH && lastButtonState == LOW) {
     // Switch the state of the output
     digitalWrite(ledPin, !digitalRead(ledPin));
  }

  // Store the button's state so we can tell if it's changed next time round
  lastButtonState = newButtonState;
}

I just grabbed the version from the ones above here and made is somewhat smaller in terms of code. Feel free to use it in your Arduino app.

// This will store the last known state of the button
int lastButtonState = LOW;

void loop()
{
  // Get the current state of the button
  int currentButtonState = digitalRead(buttonPin);

  // Has the button gone high since we last read it?
  if (currentButtonState == HIGH && lastButtonState == LOW) {
     // Switch the state of the output
     digitalWrite(ledPin, !digitalRead(ledPin));
  }

  // Store the button's state so we can tell if it's changed next time round
  lastButtonState = currentButtonState;
}
Source Link

I just grabbed the version from the ones above here and made is somewhat smaller in terms of code. Feel free to use it in your Arduino app.

// This will store the last known state of the button
int lastButtonState = LOW;

void loop()
{
  // Get the current state of the button
  int currentButtonState = digitalRead(buttonPin);

  // Has the button gone high since we last read it?
  if (newButtonState == HIGH && lastButtonState == LOW) {
     // Switch the state of the output
     digitalWrite(ledPin, !digitalRead(ledPin));
  }

  // Store the button's state so we can tell if it's changed next time round
  lastButtonState = newButtonState;
}