Code Bahaves Eratically

So I’m trying to make a program that everytime I push the toggle button it increments the brightness of an LED by 25. When it gets up to 250 then it turns off, until you hit the button again.

It works, but only if I have either a delay or a serial println at the end of the code. I have it commented out now. If I don’t have the delay or serial prinln, the light blinks eratically and does increment correctly.

If there are some variables in there that aren’t doing anything it’s because I have been editting the code, and haven’t cleared everything out yet.

int ledPin = 11;               
int inputPin = 3;               
int val = 0; 
int value = 0;              
int brightness = 0;
int laststate = 0;
int ledstate = 0;
int strength = 25;
int time = 0;

void setup() {
  pinMode(ledPin, OUTPUT);      
  pinMode(inputPin, INPUT);    
  digitalWrite(inputPin, HIGH);
  Serial.begin(9600);
}

void loop(){
  val = digitalRead(inputPin);  
  if (val != laststate)             
    {
      if (val == HIGH)
        {
          if (brightness < 7)
          {
            analogWrite(ledPin, strength);
            ledstate = 1;
            strength += 25;
            brightness++;
          }
          else{
                if (brightness >= 7){
                brightness = 0;
                strength = 50;
                digitalWrite(ledPin, LOW);
            }
                
        }
 
    }
         laststate = val;  
  }
    
 //Serial.println(brightness);   
}

Its probably contact bounce, mechanical switches often don't switch cleanly and make multiple contacts when opened and closed. The usual technique for debouncing switches generally involves a delay of some kind. If the delay works for you, I'd go with it.

How much delay can I put in without affecting anything?

What do you have that's time critical ? . The easy answer is the minimum it takes to do the job. 50 milliseconds ? 100 milliseconds ? I'd suck it and see.

Well what I don't want it to ignore button presses because it's doing a delay.

How much delay can I put in without affecting anything?

I’ve had success with 25ms. I’ve been using good quality / new push buttons that theoretically aren’t too bouncy.

Well what I don't want it to ignore button presses because it's doing a delay.

your best bet is starting at a number (plenty of examples given already) and see how it does, setup some print statements so you can see how its reacting, and aim lower, dont go psychotic trying to find the exact minimum delay, cause lets face it... the human finger is only going to react so fast, and the mcu can react much faster

or you could just toss a small cap into the hardware mix on each button and save yourself a ton of software headache

          else{
                if (brightness >= 7){
                brightness = 0;
                strength = 50;
                digitalWrite(ledPin, LOW);
            }

Just a small thing. As far as I know i wouldn't think that if Statement is necessary because before you have defined it sufficiently

For debouncing you can run a for loop that makes sure the value remains stable for a minimum number of counts. If the value is unstable (i.e. bouncing) exit and do nothing. If it remains stable, consider it to have finally stopped bouncing and do the intended action.

And for clarity, digital logic code can only behave logically. Only authors can behave erratically. ;)

Its probably contact bounce, mechanical switches often don't switch cleanly and make multiple contacts when opened and closed.

Explains a lot. Thank you.