I am new in arduino programming and trying to make a simple program that allows changing the led brightness every time a push button which is assigned to input PIN# 2 is pressed. The output is PIN# 9 (PWM).
Unfortunately the LED is not even getting ON. Can someone assist in checking the below program please.
Thank you.
int led = 9;
int button = 2;
int Bcount = -1;
byte ledflicker[]={0,25,50,75,100,125,150,175,200,225,255};
void setup() {
pinMode(button,INPUT);
pinMode(led,OUTPUT);
}
void loop() {
if (button == HIGH)
{
Bcount = Bcount + 1;
analogWrite(led,ledflicker[Bcount]);
delay(200);
}
if (Bcount==10)
{
Bcount=-1;
}
}
It's awkward the way you pre-increment the index, this forces you to use a signed variable and initialize it to a value outside the array, -1. You should increment after use, instead like: