SparkFun Inventor's Kit 4.1-push button input + LED output in 'while' loop only

I'm working in Arduino IDE. I have a SparkFun inventor's kit 4.1 and I'm trying to get my code to have a single push-button to light an LED with specific input values for analog; to light at max value (255 for analogWrite function) for the first push of the button, then with another push of the button to minus 1/3 (-85) or to be 170 of the brightness, then 85 with the next push of the button, until pressing the button yields no light or 0- and I can repeat this process forever. Going from max light, to 2/3, to 1/3, to 0 light emitted from my LED controlled with one push each from my push button. Also, criteria: I need to have most of my code in the 'while' loop and no other functions- just variables.

While my current code is fulfilling most of what I want, the problem I'm having is that after I've pressed the button 4 times, the next sequence of button pushes there is only 255, 170, and 85 values that display, but not 0 or no light value- it never turns the LED off after one pass through. Please advise?

My code below:

const int button = 2; //pin2 is linked to the button mechanism in the circuitry, as a constant.
const int led = 11; //pin11 is linked to the LED circuitry, as a constant. 
int t = 500; //'t' for time, var used in time delay. 
int brightness = 255; //'brightness' stores baseline value (0-255 max) of power emitted from LED. 
int buttonpress = 0; //buttonpress recalls last value of the function "storing" it. Baseline is 0 until loop (button-pressing) occurs.

void setup() { pinMode(led, OUTPUT); pinMode(button, INPUT); }//close void setup

void loop() {

int buttonpress = digitalRead(button);

while (buttonpress == 1) { //while button is pressed down, emitting HIGH voltage-

analogWrite(led, brightness); // turn on led to 0, 85, 170, 255 in analog values. delay(t);
brightness = brightness -85; //increases brightness by 85- one third the power of 255.
buttonpress = digitalRead(button); //refresh value of buttonpress variable.

}//end 'while' statement }//close void loop

I think it has something to do with the variable brightness

You have declared it as int which means it has a possible value of [−32,767, +32,767]

This part of the code basically makes the variable negative after a few pushes

brightness = brightness -85;

It starts as 255...


and so on

And I'm not sure how analogWrite handles negative numbers.

If you want your variable to only have a possible value of 0 to 255, you can declare it as uint8_t

for example

uint8_t brightness = 255;

And the way you handle pushbutton is not the correct way. But that is subject of a more advance topic

Thank you! Yes, I'm new to learning Arduino syntax and other concepts. I appreciate your input. I am curious about the latter input, so I can learn more...