looping error

Hi,
I am having a problem with the project I am doing. Basically, it uses PWM to light up and dim the led. The problem with the program is…

  • sometimes when I press the button nothing happens
  • and for the ‘if’ condition when i write 255 or 0 it exceeds the values, so i have written one value lower

Can someone tell me how to fix this problems

Thanks.

int switchpin=2, ledpin=10, reading, current_state, brightness;
int initial_state=LOW, counter=HIGH;

void setup()
{
  pinMode(ledpin, OUTPUT);
  pinMode(switchpin, INPUT);
  Serial.begin(9600);
}

void loop()
{
  reading=digitalRead(switchpin);
  current_state=debounce(reading);
  
  if (current_state!=initial_state)
  {
    if (current_state==HIGH && counter==HIGH)
    {
      brightness=PWM_increase(brightness);
    }
    
    if (current_state==HIGH && counter==LOW)
    {
      brightness=PWM_decrease(brightness);
    }
    if (current_state==LOW)
    {
      counter=!counter;
    }      
  }
  initial_state=current_state;
  Serial.println(current_state);
}

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);
}

int PWM_decrease(int brightness)
{
  while (reading==HIGH)
  {
    reading=digitalRead(switchpin);
    if(brightness>=15 && reading==HIGH)
    {
      brightness=brightness-15;
      analogWrite(ledpin, brightness);
      Serial.println(brightness);
      delay(500);
    }
    else
    {
      return(brightness);
    }
      
  }
  return(brightness); 
}
  
    
  
int debounce(int reading)
{
  current_state=digitalRead(switchpin);
  if (current_state!=reading) 
  {
    delay(30);
    current_state=digitalRead(switchpin);
    return(current_state);
  }
  else
  {
    return(current_state);
  }
}
  • sometimes when I press the button nothing happens

You've said nothing about how the switch is wired. Could be any number of reasons. Like, for instance nothing happens during those delay()s.

It's silly to be worried about bounce when you have a minimum of half a second delay between switch reads. No switch bounces that long. If one did, the 0.03 second delay wouldn't take care of it.

  • 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);
}

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

It does.

. I had to create another IF condition inside the WHILE loop to check if pushbutton goes low

I can't see your before code, or your serial output, or any proof that the switch is wired correctly. The if statement in the while loop does not cause a break out of the while loop, so that should be adequate proof that the while loop does end when the switch state is no longer HIGH.

It appears, though, that you expect the body of the while loop to not be executed, if, after entering the while body, the exit condition becomes true. That is NOT how a while statement works.