Am I using the right code?

Hi! I am trying to write a code to produce a continuous pulsing light when the button is pressed. However, I cant get it to continue pulsing after the switch is used to turn it on - it just pulses for one loop, and then it goes on to a HIGH state.

/* sketch 3
turn on a LED to produce a pulsing light when the button is pressed and let it run
until the button is pressed again to turn it off
*/
int pinButton = 8;
int LED = 3;
int stateLED = LOW;
int stateButton;
int previous = LOW;
long time = 0;
long debounce = 200;
//
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by

void setup() {
pinMode(pinButton, INPUT);
pinMode(LED, OUTPUT);
}

void loop() {
stateButton = digitalRead(pinButton);
if(stateButton == HIGH && previous == LOW && millis() - time > debounce) {
brightness = 255; //if brightness is = 255, it will fade in decrements of 5 to 140
analogWrite(LED, brightness);
while(brightness > 90) {
brightness = brightness - fadeAmount;
analogWrite(LED, brightness);
delay(10);
}
while(brightness < 210) { // if brightness drops to less than 180 from previous it will + fadeamount
brightness = brightness + fadeAmount;
analogWrite(LED, brightness);
delay(10);
}
while(brightness > 140) { // if brightness drops to less than 180 from previous it will + fadeamount
brightness = brightness - fadeAmount;
analogWrite(LED, brightness);
delay(10);
}
while(brightness < 190) { // if brightness drops to less than 180 from previous it will + fadeamount
brightness = brightness + fadeAmount;
analogWrite(LED, brightness);
delay(40);
}
while(brightness > 20) {
brightness = brightness - fadeAmount;
analogWrite(LED, brightness);
delay(10);
}
delay(200);
if(stateLED == HIGH){
stateLED = LOW;

delay(200);

}
else {
stateLED = HIGH;
}
time = millis();
}
digitalWrite(LED, stateLED);
// previous == stateButton;
}

Hi! I am trying to write a code to produce a continuous pulsing light when the button is pressed. However, I cant get it to continue pulsing after the switch is used to turn it on - it just pulses for one loop, and then it goes on to a HIGH state, till i push the button to turn it off and with that, it pulses for another loop till the lights are out.

so i inserted my pulsing light, in my void loop as such and closed off, i suspect the closing off might be an issue

stateButton = digitalRead(pinButton);
if(stateButton == HIGH && previous == LOW && millis() - time > debounce) {
brightness = 255; //if brightness is = 255, it will fade in decrements of 5 to 140
analogWrite(LED, brightness);
while(brightness > 90) {
brightness = brightness - fadeAmount;
analogWrite(LED, brightness);
delay(10);
}
while(brightness < 210) { // if brightness drops to less than 180 from previous it will + fadeamount
brightness = brightness + fadeAmount;
analogWrite(LED, brightness);
delay(10);

and closed off like that

if(stateLED == HIGH){
stateLED = LOW;

}
else {
stateLED = HIGH;
}
time = millis();
}
digitalWrite(LED, stateLED);
// previous == stateButton;
}

i suspect the closing off might be an issue

Possibly. Or, it could be just about anything else. You need to take your snippets to http://snippets-r-us.com. Here, we expect to see ALL of your code PROPERLY INDENTED. Use Tools + Auto Format!

if(stateButton == HIGH && previous == LOW && millis() - time > debounce)

Do you understand what this line is doing?

This says, only do this (you fade routine) if the button is HIGH and was LOW the last time it was checked and some certain amount of time has passed since the last time it changed.

So you only run this code when the button state changes. When it goes from LOW to HIGH you run this once. After that, the previous will be HIGH so it won't run again until you satisfy that condition of going from LOW to HIGH.

Stick with your original thread. Quit wasting our time.

Look at the way code is organized in several things at a time and in planning and implementing a program. Separate your code into a part that reads and saves the state of the button and another part that blinks an led depending on the state.

...R