Changing brightness of LED with switch

Hey guys!

I'm trying to pretty much make a light that turns on with a button, and then when it's on, you can change the brightness using a potentiometer. Should be simple, right? I've got it sort of up and running, but it's only changing the brightness when you press the button, as opposed to changing continuously after the button has been pressed, and until it gets pressed again. Any clues as to what I'm doing wrong? Popped my code beneath. I've Frankensteined it together from various sources, but it seems like it's close? Would love some help, thank you :).

int inPin = 2; //button pin
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to

int state = HIGH;
int reading;
int previous = LOW;

long time = 0;
long debounce = 200;

int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)

void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
pinMode (inPin, INPUT);
}

void loop() {

reading = digitalRead (inPin); //find out what the button is up to
outputValue = map(sensorValue, 0, 1023, 0, 255); //mapping analog range to what can be output.

if (reading == HIGH && previous == LOW && millis() - time > debounce) {
sensorValue = analogRead(analogInPin); //reading pot value, and storing it in 'sensorValue'.

analogWrite(analogOutPin, outputValue); //assign pot value to LED.

// print the results to the serial monitor:
Serial.print("sensor = " );
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);

// wait 2 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:
delay(2);

time = millis();
}

previous = reading;

}

Some bugs:

outputValue = map(sensorValue, 0, 1023, 0, 255); //mapping analog range to what can be output.
This makes sense only after sensorValue has been read.

if (reading == HIGH && previous == LOW && millis() - time > debounce) {
This condition enters the block only if the switch was just pressed. Use another variable, say LEDon, to remember whether the LED is switched on or off. Toggle this variable whenever above conditions is true. Perform the remaining statements when LEDon is true.

sensorValue = analogRead(analogInPin); //reading pot value, and storing it in 'sensorValue'.
Now you can map sensorValue and set the brightness accordingly.

Ahh, perfect! That's it. Thank you so much for your help! I appreciate it so much :).

Hey again, I'm so close. It's turning on, but I can't quite get it to turn off. I've done basically the opposite at the bottom, but I don't really know what to do. Sorry, I know this is stupid and probably pretty simple. I'm pretty new to all this, so I really appreciate your patience and help :). I've tried putting a boolean check in the next loop that checks to see if LEDon is on and to react from that, but still nothing. Thanks again :)!

int inPin = 2; //button pin
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to
int LEDon = LOW;

int state = HIGH;
int reading;
int previous = LOW;

long time = 0;
long debounce = 200;

int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)

void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
pinMode (inPin, INPUT);
}

void loop() {

reading = digitalRead (inPin); //find out what the button is up to

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

LEDon = HIGH;

// wait 2 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:
delay(2);
}

if (LEDon == HIGH) {
sensorValue = analogRead(analogInPin); //reading pot value, and storing it in 'sensorValue'.
outputValue = map(sensorValue, 0, 1023, 0, 255); //mapping analog range to what can be output.

analogWrite(analogOutPin, outputValue); //assign pot value to LED.

// print the results to the serial monitor:
Serial.print("sensor = " );
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
time = millis();

}
previous = reading;

reading = digitalRead (inPin); //check to see if button is being pressed

if (reading == HIGH && previous == LOW && LEDon == HIGH && millis() - time > debounce) {

LEDon = LOW;

// wait 2 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:
delay(2);
}

if (LEDon == LOW) {
outputValue = 0;
}

}

Here's the bug:

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

LEDon = HIGH;

You only turn on, never off.

BTW where do you update "time"?