user63496: I am trying to implement a toggle switch to turn blinking ON & OFF. However, the code only turns the led ON or OFF and that too unreliably. Can you help me understand why it isn't working?
   const int redLed = 10;
const int buttonPin = 2;
unsigned long newTime;
unsigned long oldTime = 0;
byte newButtonState;
byte oldButtonState = 0;
boolean ledState = false;
boolean blinkState = false;
boolean state = LOW;


void setup() {
  pinMode(redLed, OUTPUT);
  Serial.begin(9600);
  pinMode(buttonPin, INPUT);
}

void loop() {

  newButtonState = digitalRead(buttonPin);
  delay(100);


  // check if button state has changed
  if (newButtonState != oldButtonState) {
    // if button was pushed
    if (newButtonState == HIGH) {
      // check led state
      if (ledState == false) {
        ledState = true;
        blinkState = true;
      } else {
        ledState = false;
        blinkState = false;
      }
      if (ledState == true && blinkState == true) {
        newTime = millis();
        if (newTime - oldTime >= 250) {
          state = !state;
          oldTime = newTime;
        }
        digitalWrite(redLed, state);
      }
    }
  }
  oldButtonState = newButtonState;
}


user31105: Note, this is not an answer, but I want to show some code examples based on the comment:
You can use as said in the comment:

Remove '== true' in a boolean condition
Replace '== false' by '!' in a boolean condition

You will get this:
  // check if button state has changed
  if (newButtonState != oldButtonState) {
    // if button was pushed
    if (newButtonState == HIGH) {
      // check led state
      if (!ledState) {
        ledState = true;
        blinkState = true;
      } else {
        ledState = false;
        blinkState = false;
      }
      if (ledState && blinkState) {
        newTime = millis();
        if (newTime - oldTime >= 250) {
          state = !state;
          oldTime = newTime;
        }
        digitalWrite(redLed, state);
      }
    }
  }
  oldButtonState = newButtonState;
}

About the following fragment:
  if (!ledState) {
    ledState = true;
    blinkState = true;
  } else {
    ledState = false;
    blinkState = false;
  }

Since you write ledState and blinkState in both clauses, and the condition is simple (so you don't need to put it in a temporary variable, you can replace this fragment by:
blinkState = !ledState;
ledState = !ledState;

Note you have to reverse the order as blinkState depends on ledState.

63496: <p>I am trying to implement a toggle switch to turn blinking ON &amp; OFF. However, the code only turns the led ON or OFF and that too unreliably. Can you help me understand why it isn't working?</p>

<pre><code>   const int redLed = 10;
const int buttonPin = 2;
unsigned long newTime;
unsigned long oldTime = 0;
byte newButtonState;
byte oldButtonState = 0;
boolean ledState = false;
boolean blinkState = false;
boolean state = LOW;


void setup() {
  pinMode(redLed, OUTPUT);
  Serial.begin(9600);
  pinMode(buttonPin, INPUT);
}

void loop() {

  newButtonState = digitalRead(buttonPin);
  delay(100);


  // check if button state has changed
  if (newButtonState != oldButtonState) {
    // if button was pushed
    if (newButtonState == HIGH) {
      // check led state
      if (ledState == false) {
        ledState = true;
        blinkState = true;
      } else {
        ledState = false;
        blinkState = false;
      }
      if (ledState == true &amp;&amp; blinkState == true) {
        newTime = millis();
        if (newTime - oldTime &gt;= 250) {
          state = !state;
          oldTime = newTime;
        }
        digitalWrite(redLed, state);
      }
    }
  }
  oldButtonState = newButtonState;
}
</code></pre>
 31105: <p>Note, this is not an answer, but I want to show some code examples based on the comment:</p>

<p>You can use as said in the comment:</p>

<ul>
<li>Remove '== true' in a boolean condition</li>
<li>Replace '== false' by '!' in a boolean condition</li>
</ul>

<p>You will get this:</p>

<pre><code>  // check if button state has changed
  if (newButtonState != oldButtonState) {
    // if button was pushed
    if (newButtonState == HIGH) {
      // check led state
      if (!ledState) {
        ledState = true;
        blinkState = true;
      } else {
        ledState = false;
        blinkState = false;
      }
      if (ledState &amp;&amp; blinkState) {
        newTime = millis();
        if (newTime - oldTime &gt;= 250) {
          state = !state;
          oldTime = newTime;
        }
        digitalWrite(redLed, state);
      }
    }
  }
  oldButtonState = newButtonState;
}
</code></pre>

<p>About the following fragment:</p>

<pre><code>  if (!ledState) {
    ledState = true;
    blinkState = true;
  } else {
    ledState = false;
    blinkState = false;
  }
</code></pre>

<p>Since you write <code>ledState</code> and <code>blinkState</code> in both clauses, and the condition is simple (so you don't need to put it in a temporary variable, you can replace this fragment by:</p>

<pre><code>blinkState = !ledState;
ledState = !ledState;
</code></pre>

<p>Note you have to reverse the order as <code>blinkState</code> depends on <code>ledState</code>.</p>
