Arduino PWM Problem

For some time i’ve been making some LED Dimmer programs for my personal use, and on all then i found some really strange problem that just happens some times. I’ve tried to change my program many times but with no success. I’ve made a video showing the problem, here is the link: Video Link
AS you can see sometimes when the PWM is 0 and i start dimming the LED goes high then after get down and start increasing slowly as it needs to be.

Here is my code:

 int buttonPin;
 int buttonPin = 50;
 int channelPin = 7;
 int fade = 0;
 boolean state;

void setup() {
 Serial.begin(9600);
}

void loop() {
if(digitalRead(buttonPin) == 0) {
delay(200);
if(digitalRead(buttonPin) == 1) {
       if(fade >  0) {
     fade = 0;
     state = 1;
   }else{
     fade = 255;
     state = 0;
   }
   analogWrite(channelPin, fade);
     }else{
       while(digitalRead(buttonPin) == 0) {
         if(state == 1) {
           fade += 2;
           if(fade >= 255) {
             fade = 255;
           }
         }
         if(state == 0) {
           fade -= 2;
           if(fade <= 2) {
             fade = 0;
           }
         }
           analogWrite(channelPin, fade);
           if(digitalRead(buttonPin) == 1) {
             state = !state;
             break;
           }
           delay(30);
         }
         state = !state;
     }
   }
}

Looks like you have annoyed someone with that link, it has been taken down.

Post your code and a schematic if you want help.

TheGMX:
For some time i've been making some LED Dimmer programs for my personal use, and on all then i found some really strange problem that just happens some times. I've tried to change my program many times but with no success. I've made a video showing the problem, here is the link: Video Link
AS you can see sometimes when the PWM is 0 and i start dimming the LED goes high then after get down and start increasing slowly as it needs to be.

Sounds like you're decrementing an unsigned variable to below zero where it rolls over to 255. But as mentioned above, we're just guessing without seeing your code.

Sounds like you're decrementing an unsigned variable to below zero where it rolls over to 255

That's a sharp observation. It never occurred to me but since you mention it , it sounds like exactly that.

Sorry for taking that long to reply, i was the whole week without internet.
I had updated the main post with the code and fixed the link.

Henry_Best, i will try solving the problem you proposed but i think that is not the case.

if(digitalRead(buttonPin) == 1) {
       if(fade >  0) {
     fade = 0;
     state = 1;
   }else{
     fade = 255;
     state = 0;
   }
   analogWrite(channelPin, fade);
     }else{
       while(digitalRead(buttonPin) == 0) {
         if(state == 1) {
           fade += 2;
           if(fade >= 255) {
             fade = 255;
           }
         }
         if(state == 0) {
           fade -= 2;
           if(fade <= 2) {
             fade = 0;
           }
         }

I can’t help but wonder how you can be posting on the forum and not have any debug serial prints in your code to show that you as LEAST made an effort to ascertain what the uP is doing. It seems to me that instead of asking us you should interogating your prime suspect, the uP , (preferably in a small room with only one chair and a low hanging saucer shaped bright lamp with a 200W bulb in it)

       if(fade >  0) {[color=#222222][/color]
     fade = 0;[color=#222222][/color]
     state = 1;[color=#222222][/color]
   }else{

That else can only be true if fade ==0, as nowhere in your program can fade become negative.
As you're setting fade to 0 in the previous if statement, the next time round loop() the else will be true, so your program will be constantly flipping between fade ==0 and fade==255.
I don't think that's what you want.

raschemmel:

if(digitalRead(buttonPin) == 1) {

if(fade >  0) {
    fade = 0;
    state = 1;
  }else{
    fade = 255;
    state = 0;
  }
  analogWrite(channelPin, fade);
    }else{
      while(digitalRead(buttonPin) == 0) {
        if(state == 1) {
          fade += 2;
          if(fade >= 255) {
            fade = 255;
          }
        }
        if(state == 0) {
          fade -= 2;
          if(fade <= 2) {
            fade = 0;
          }
        }




I can't help but wonder how you can be posting on the forum and not have any debug serial prints in your code to show that you as LEAST made an effort to ascertain what the uP is doing. It seems to me that instead of asking us you should interogating your prime suspect, the uP , (preferably in a small room with only one chair and a low hanging saucer shaped bright lamp with a 200W bulb in it)

Sorry but i never will post here before doing the tests, and instead of putting here my full code with the Serial prints which are in portuguese, my native language, i prefer to post a clear and easy to understand code.

Henry_Best:

       if(fade >  0) {[color=#222222][/color]

fade = 0;
    state = 1;
  }else{



That else can only be true if fade ==0, as nowhere in your program can fade become negative.
As you're setting fade to 0 in the previous if statement, the next time round loop() the else will be true, so your program will be constantly flipping between fade ==0 and fade==255. 
I don't think that's what you want.

The ideia of the program is that if the user press the button one time it will check if the LED is whether on or off and will change the state, so if the LED is on than the arduino will turn it off. But if the button is hold then the arduino will start fading up or down depending on the state variable, which will tell if the last time the fade was up or down, so if the last time the fade was up then now it will fade down.

How I’d do that, off the top of my head.

byte fade=0;
byte ofade=0; 


void loop()
{
if (digitalRead(buttonPin)) {
if (fade < 255) {
fade++;}
else {
if (fade > 0) {
fade--;
}
if (ofade!=fade){ //if the value has changed
ofade=fade; //save the new value to ofade
analogWrite(ledPin,fade); //and change the brightness
}
delay(15); //do it twice as fast as you do, that way it fades at same speed, but I can go in steps of 1; I mean, why not?
}

Sorry but i never will post here before doing the tests, and instead of putting here my full code with the Serial prints which are in portuguese, my native language, i prefer to post a clear and easy to understand code.

So ? If you read Portugese then what did you learn from the serial prints you had in your code BEFORE you posted here ? I find it hard to believe this issue can not be resolved in 30 minutes of looking at serial prints in every stage of your code . If I run it at home with serial prints everywhere are you going to tell me I won't find the problem ?

How I'd do that, off the top of my head.

I couldn't help but notice you didn't use "states". I take it that means they are unnecessary ?