Six leds dimmer

hi every one, can somebody help me with this code?
what I'm trying to do is a kind of dimmer with six led, when I press a button, the firs led should turn on and as I press the button again the led should increase the brightness and, when is completely on, the next led will turn on and do the same, then, when all the led are completely on, when I press the button, all the led should turn of.

Here is my code, the code isn't working and I don't know why.

//push button pin
#define pull 3

//pins and others variables declaration
byte ledpin[] = {25,26,27,29,32,33};
int i, j, ledbrid, pullval;
int pinlength = sizeof(ledpin); 

void setup() 
{
  //this for loop is for declaring the pins in one stepp
  for (i=0; i<pinlength; i++)
  {
    pinMode(ledpin[i], OUTPUT); 
  } 
    pinMode(pull, INPUT); 
}

void loop() 
{
  pullval = digitalRead(pull);
  
  for(i=0; i<pinlength; i++) 
   {
     for(j=0; j<=255; j+=ledbrid) 
      {
        if(pullval==HIGH) 
        {
          ledbrid += 63;
        } 
       analogWrite(ledpin[i], ledbrid); 
      }
      
   }
    
   
      
 digitalWrite(ledpin[i], LOW); 
    
}```

Isn't working is not an adequate description

Is that all the code?

  1. Thanks for the explanation of what your code is supposed to do!
  2. Please state what your code IS doing.
  3. Please post your entire code. The code you posted does not compile.
  4. What Arduino are you using?

for (a; b; c) statement; is a shortcut for:

  a;
  while (b)
  {
     statement;
     c;
  }

When you say for(i=0; i>pinlength; i++) the loop isn't going to do anything because:

   i = 0;
   while (i > pinlength)  // 0 is not >pinlength so do nothing.

You probably meant i < pinlength.

When you say for(j=0; j>=255; j+ledbrid) the loop isn't going to do anything because:

   j = 0;
   while (j >= 255)  // 0 is not >= 255 so do nothing.

You probably meant j <= 255. Also, your increment expression j+ledbrid doesn't do anything. You probably meant j += ledbrid which is a shortcut for j = j + ledbrid.

1 Like

You are configuring the button with INPUT rather than INPUT_PULLUP. You are using HIGH to determine if the button is pressed. These imply you have a pull-down resistor. Is that the case?

You are looking at the state of the button to determine whether to increment the brightness. If you are incrementing on individual button presses you need to detect the change in state of the button (from off to on) rather than the current state of the button. See the following tutorial: StateChangeDetection

You are doing this:

digitalWrite(ledpin[i], ledbrid);

digitalWrite() only supports HIGH and LOW (on or off in this case). You need to use analogWrite() for PWM and make sure all of your LEDs are connected to PWM outputs to control the brightness.

my friend, I have the pull down resistor in the button and I all ready have all the led in pwm support pins, but you're completely right when you say that I'm reading wrong the button's satate, I'll trie with statement and I also will check the page you sent me.
Thanks a lot.

Thanks a lot for all your support, I'll make the corrections

yes my friend, that is the complete code, the problem is in the code because the components are properly connected in the bread board, but the circuit is not turning on any led

And does that code compile for you?

Now, the code isn't doing anything, (I mean, the board) is just not working, and I edit the text and put the correct code, I putted an incomplete code and I'm using an ESP32 Devki TV1

I already edit the text and put the complete code, It was just a copy and paste error

I've seeing the information page that you sent me and it was very helpful, I have another code and it's working good. Thanks a lot my friend.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.