Need help to explain the sketch please

Hi,
The code from:
https://create.arduino.cc/projecthub/lightthedreams/how-to-connect-remote-control-4-channel-to-arduino-dc8e33?ref=platform&ref_id=424_popular___&offset=4959

In my understood, because of the D0/D1 are momentary 'HIGH' when be trigger, the LED ought to ON just a moment then off, in fact it keep on till next trigger. why?
Thanks
Adam

/* https://create.arduino.cc/projecthub/lightthedreams/how-to-connect-remote-control-4-channel-to-arduino-dc8e33?ref=platform&ref_id=424_popular___&offset=4959
 *   
 *  
 */

// constants won't change. They're used here to
// set pin numbers:
const int button1 = 10;    //  WAS:1-4the number of the pushbutton pin
const int button2 = 11; 
const int ledPin1 = 12;      // the number of the LED pin
const int ledPin2 = 13; 
// Variables will change:
int ledState1 = HIGH;         // the current state of the output pin
int ledState2 = HIGH;
int buttonState1;             // the current reading from the input pin
int buttonState2; 
int lastButtonState1 = LOW;   // the previous reading from the input pin
int lastButtonState2 = LOW;

// the following variables are unsigned long's because the time, measured in miliseconds,
// 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(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);

  // set initial LED state
  digitalWrite(ledPin1, ledState1);
  digitalWrite(ledPin2, ledState2);
}

void loop() {
  // read the state of the switch into a local variable:
  int reading1 = digitalRead(button1);
  int reading2 = digitalRead(button2);
  
  // 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 (reading1 != lastButtonState1) {
    // 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 (reading1 != buttonState1) {
      buttonState1 = reading1;

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

  // set the LED:
  digitalWrite(ledPin1, ledState1);

  // save the reading.  Next time through the loop,
  // it'll be the lastButtonState:
  lastButtonState1 = reading1;
  
  
   // If the switch changed, due to noise or pressing:
  if (reading2 != lastButtonState2) {
    // 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 (reading2 != buttonState2) {
      buttonState2 = reading2;

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

  // set the LED:
  digitalWrite(ledPin2, ledState2);

  // save the reading.  Next time through the loop,
  // it'll be the lastButtonState:
  lastButtonState2 = reading2;
}

State Change Detection. The LED state is toggled:
ledState1 = !ledState1;
each time the button state goes from LOW to HIGH. Nothing is changed when it goes from HIGH to LOW so the LED doesn't go off when the input pulse ends.

1 Like

Great!
Thank you.

I highly recommend you goto: https://www.arduino.cc/en/Tutorial/HomePage and read the tutorials, they will help you a lot. ledState1 = !ledState1; "!" is the not changes the state of ledState from a 1 to a zero or if zero to a 1. Hopefully that will help.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.