Skip to main content
moved comments away from the code
Source Link
Greenonline
  • 3.2k
  • 7
  • 37
  • 49

First, fix the indentation:

New = digitalRead(button);
if ( New!=old) {                        // If the button state has changed
    if ( New == HIGH ) {                // If the button is now in the pressed state
        if (LEDstatus == LOW) {         // If the LED was off 
            digitalWrite(LED,HIGH);     // Turn the LED on
            LEDstatus = HIGH;           // Remember the LED state for next time
        } else {                        // The LED was on, so turn it off
            digitalWrite(LED,LOW);
            LEDstatus=LOW;              // Remember the LED state for next time
        }
    }
    old=New;
}

That makes the code a whole lot easier to understand.

It only does something when the button is pressed (switches to HIGH state.). It doesn't do anything when you release the button.

Each button press changes the state of the LED.

if it was off, it turns it on. If it was on, it turns it off.

First, fix the indentation:

New = digitalRead(button);
if ( New!=old) {                        // If the button state has changed
    if ( New == HIGH ) {                // If the button is now in the pressed state
        if (LEDstatus == LOW) {         // If the LED was off 
            digitalWrite(LED,HIGH);     // Turn the LED on
            LEDstatus = HIGH;           // Remember the LED state for next time
        } else {                        // The LED was on, so turn it off
            digitalWrite(LED,LOW);
            LEDstatus=LOW;              // Remember the LED state for next time
        }
    }
    old=New;
}

That makes the code a whole lot easier to understand.

It only does something when the button is pressed (switches to HIGH state.) It doesn't do anything when you release the button.

Each button press changes the state of the LED.

if it was off, it turns it on. If it was on it turns it off.

First, fix the indentation:

New = digitalRead(button);
if ( New!=old) {                        // If the button state has changed
    if ( New == HIGH ) {                // If the button is now in the pressed state
        if (LEDstatus == LOW) {         // If the LED was off 
            digitalWrite(LED,HIGH);     // Turn the LED on
            LEDstatus = HIGH;           // Remember the LED state for next time
        } else {                        // The LED was on, so turn it off
            digitalWrite(LED,LOW);
            LEDstatus=LOW;              // Remember the LED state for next time
        }
    }
    old=New;
}

That makes the code a whole lot easier to understand.

It only does something when the button is pressed (switches to HIGH state). It doesn't do anything when you release the button.

Each button press changes the state of the LED.

if it was off, it turns it on. If it was on, it turns it off.

First, fix the indentation:

New = digitalRead(button);
if ( New!=old) {                        // If the button state has changed
    if ( New == HIGH ) {                // If the button is now in the pressed state
        if (LEDstatus == LOW) {         // If the LED was off 
            digitalWrite(LED,HIGH);     // Turn the LED on
            LEDstatus = HIGH;           // Remember the LED state for next time
        } else {                        // The LED was on, so turn it off
            digitalWrite(LED,LOW);
            LEDstatus=LOW;              // Remember the LED state for next time
        }
    }
    old=New;
}

That makes the code a whole lot easier to understand.

It only does something when the button is pressed (switches to HIGH state.) It doesn't do anything when you release the button.

Each button press changes the state of the LED.

if it was off, it turns it on. If it was on it turns it off.

First, fix the indentation:

New = digitalRead(button);
if ( New!=old) {  //If the button state has changed
    if ( New == HIGH ) {  //If the button is now in the pressed state
        if (LEDstatus == LOW) {  //If the LED was off 
            digitalWrite(LED,HIGH);   //Turn the LED on
            LEDstatus = HIGH; //Remember the LED state for next time
        } else { //The LED was on, so turn it off
            digitalWrite(LED,LOW);
            LEDstatus=LOW;  //Remember the LED state for next time
        }
    }
    old=New;
}

That makes the code a whole lot easier to understand.

It only does something when the button is pressed (switches to HIGH state.) It doesn't do anything when you release the button.

Each button press changes the state of the LED.

if it was off, it turns it on. If it was on it turns it off.

First, fix the indentation:

New = digitalRead(button);
if ( New!=old) {                        // If the button state has changed
    if ( New == HIGH ) {                // If the button is now in the pressed state
        if (LEDstatus == LOW) {         // If the LED was off 
            digitalWrite(LED,HIGH);     // Turn the LED on
            LEDstatus = HIGH;           // Remember the LED state for next time
        } else {                        // The LED was on, so turn it off
            digitalWrite(LED,LOW);
            LEDstatus=LOW;              // Remember the LED state for next time
        }
    }
    old=New;
}

That makes the code a whole lot easier to understand.

It only does something when the button is pressed (switches to HIGH state.) It doesn't do anything when you release the button.

Each button press changes the state of the LED.

if it was off, it turns it on. If it was on it turns it off.

Source Link
Duncan C
  • 5.8k
  • 3
  • 19
  • 31

First, fix the indentation:

New = digitalRead(button);
if ( New!=old) {  //If the button state has changed
    if ( New == HIGH ) {  //If the button is now in the pressed state
        if (LEDstatus == LOW) {  //If the LED was off 
            digitalWrite(LED,HIGH);   //Turn the LED on
            LEDstatus = HIGH; //Remember the LED state for next time
        } else { //The LED was on, so turn it off
            digitalWrite(LED,LOW);
            LEDstatus=LOW;  //Remember the LED state for next time
        }
    }
    old=New;
}

That makes the code a whole lot easier to understand.

It only does something when the button is pressed (switches to HIGH state.) It doesn't do anything when you release the button.

Each button press changes the state of the LED.

if it was off, it turns it on. If it was on it turns it off.