Im trying to turn and LED on when I press it once and turn it off when I press it a second time. Can someone tell me whats wrong with this code?

  int led = 2;
  int button = 4;
  bool ledState = false;


void setup()
{
  pinMode(led,OUTPUT);
  pinMode(button,INPUT);
  
}

void loop()
{
    if(digitalRead(button == HIGH)) {
       if(ledState == false){
        digitalWrite(led, HIGH);
        ledState, true;
        
        } else{
        digitalWrite(led, LOW);
        }
            if(digitalRead(button == LOW)) {
            digitalWrite(led,LOW);
            ledState, false;
          }
      
      }
}

There are several issues with the provided code that prevent it from functioning as intended. Here's a corrected version of the code with explanations for the changes:

int led = 2;
int button = 4;
bool ledState = false;

void setup() {
 pinMode(led, OUTPUT);
 pinMode(button, INPUT);
}

void loop() {
 if(digitalRead(button) == HIGH) { // Corrected the syntax error here
    if(ledState == false){
      digitalWrite(led, HIGH);
      ledState = true; // Corrected the syntax error here
    } else {
      digitalWrite(led, LOW);
      ledState = false; // Corrected the syntax error here
    }
 }
}
  1. Syntax Error in if Statement: The original code incorrectly used if(digitalRead(button == HIGH)), which is not valid syntax. The correct way to check if the button is pressed is if(digitalRead(button) == HIGH).

  2. Incorrect Assignment Syntax: The original code used ledState, true; and ledState, false; which are not valid assignment statements in C++. The correct syntax for assignment is ledState = true; and ledState = false;.

  3. Unnecessary Second if Statement: The original code included a second if statement to check if the button is LOW, which is unnecessary because the LED state is already toggled based on the button's HIGH state. The corrected code removes this unnecessary check.

  4. Debounce Handling: The original code does not include any debounce handling, which is crucial for physical buttons to prevent false triggers due to mechanical bouncing. Consider implementing a debounce mechanism to improve the button's reliability. This can be done by checking the button state only after a certain delay since the last state change.

Here's an example of how you might implement debounce handling:

#define LED_PIN 2
#define BUTTON_PIN 4

byte lastButtonState = LOW;
byte ledState = LOW;

unsigned long debounceDuration = 50; // millis
unsigned long lastTimeButtonStateChanged = 0;

void setup() {
 pinMode(LED_PIN, OUTPUT);
 pinMode(BUTTON_PIN, INPUT);
}

void loop() {
 if (millis() - lastTimeButtonStateChanged > debounceDuration) {
    byte buttonState = digitalRead(BUTTON_PIN);
    if (buttonState != lastButtonState) {
      lastTimeButtonStateChanged = millis();
      lastButtonState = buttonState;
      if (buttonState == HIGH) {
        ledState = (ledState == HIGH) ? LOW : HIGH;
        digitalWrite(LED_PIN, ledState);
      }
    }
 }
}

This code includes a debounce mechanism by checking the button state only after a certain delay (debounceDuration) since the last state change, which helps to prevent false triggers due to mechanical bouncing of the button.

1 Like

Thank you very much! I appreciate your help a lot! :grin:

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