Arduino LED brightness switching using pushbutton
Although your code has some weaknesses it shouldn't generate the behavior you describe. You haven't provided any shematic of the setup so my answer is really just a guess but a missing pull-down resistor would probably cause exactly what you're telling : the digital reading of the button pin sporadically switches between HIGH and LOW, and the led appears to flicker randomly instead of just changing brightness as expected.