user48671: I am trying to program a sketch such that an LED will light up for as long as a push button is pressed down. I have tested the components and electronics configuration before with a simpler program, and it all works, but there seems to be a problem in the code. I wanted to know how my sketch may be altered so the code will work:
const int ledPin = 12;
const int switchPin = 2;
const int Time = 100;
int buttonState = 0;
int count = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(switchPin, INPUT); 
}

void loop() {
buttonState = digitalRead(switchPin);
while (buttonState == HIGH) {
    count++;
    delay(100);
  }
  count=count*10;

  if (count>1) {
    digitalWrite(ledPin, HIGH);
    delay((count*10));
    digitalWrite(ledPin, LOW);
  }

}


user359: while (buttonState == HIGH) {
    count++;
    delay(100);
}

this will never exit when the button is ever high. You should also reset count after the if.
Instead you want to either do buttonState = digitalRead(switchPin); inside the loop or rejig the function to be a state machine using timestamps like blink without delay.

48671: <p>I am trying to program a sketch such that an LED will light up for as long as a push button is pressed down. I have tested the components and electronics configuration before with a simpler program, and it all works, but there seems to be a problem in the code. I wanted to know how my sketch may be altered so the code will work:</p>

<pre><code>const int ledPin = 12;
const int switchPin = 2;
const int Time = 100;
int buttonState = 0;
int count = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(switchPin, INPUT); 
}

void loop() {
buttonState = digitalRead(switchPin);
while (buttonState == HIGH) {
    count++;
    delay(100);
  }
  count=count*10;

  if (count&gt;1) {
    digitalWrite(ledPin, HIGH);
    delay((count*10));
    digitalWrite(ledPin, LOW);
  }

}
</code></pre>
 359: <pre><code>while (buttonState == HIGH) {
    count++;
    delay(100);
}
</code></pre>

<p>this will never exit when the button is ever high. You should also reset count after the if.</p>

<p>Instead you want to either do <code>buttonState = digitalRead(switchPin);</code> inside the loop or rejig the function to be a state machine using timestamps like blink without delay.</p>
