looping error

  • the push button switch is connected with a pull-down resistor. i was assuming it was not working sometimes due to a problem in the main loop, otherwise it works fine.

  • the 30millisdelay is set for the first read; 500millsdelay is set for the PWM increase.

  • Also, here when i have created a WHILE loop saying to loop while the pushbutton is high, why doesn't it stop once the switch goes LOW. I had to create another IF condition inside the WHILE loop to check if pushbutton goes low

  • And, also why can't i set the IF condition inside the WHILE loop be set for 255.. it exceeds the value?

int PWM_increase(int brightness)
{
  while (reading==HIGH)
  {
    reading=digitalRead(switchpin);
    if(brightness<=240 && reading==HIGH)
    {
      brightness=brightness+15;
      analogWrite(ledpin, brightness);
      Serial.println(brightness);
      delay(500);
    }
    else
    {
      return(brightness);
    }      
  }
  return(brightness);
}