Fading multiple leds

Hello everyone,

I have a problems with my arduino micro, i want to fade multiples leds in same time by pushed button, one for "on" and another for "off". So here the code :

int brightness = 0; //fade

int pbuttonPin = 2; // first button
int val = 0;
int on = 0;

int pbuttonPin2 = 3; //second button
int val2 = 0;
int off = 0;

int ledPin1=4; //led pin
int ledPin2=5; //led pin
int ledPin3=6; //led pin
int ledPin4=7; //led pin
int ledPin5=8; //led pin
int ledPin6=9; //led pin
int ledPin7=10; //led pin
int ledPin8=11; //led pin
int ledPin9=12; //led pin
int ledPin10=13; //led pin

void setup()
{
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);

pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);
pinMode(ledPin6, OUTPUT);
pinMode(ledPin7, OUTPUT);
pinMode(ledPin8, OUTPUT);
pinMode(ledPin9, OUTPUT);
pinMode(ledPin10, OUTPUT);

Serial.begin(9600);

}

void loop()
{
val = digitalRead(pbuttonPin);
val2 = digitalRead(pbuttonPin2);

if(val == LOW && on == 0){
on = on + 1;
off = 0;
delay(100);
}

if(on == 1){
brightness = brightness + 5;
brightness2 = brightness2 + 5;
analogWrite(ledPin1, brightness);
analogWrite(ledPin2, brightness2);
analogWrite(ledPin3, brightness);
analogWrite(ledPin4, brightness2);
analogWrite(ledPin5, brightness);
analogWrite(ledPin6, brightness2);
analogWrite(ledPin7, brightness);
analogWrite(ledPin8, brightness2);
analogWrite(ledPin9, brightness);
analogWrite(ledPin10, brightness2);
delay(50);

while (brightness >= 255) {
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, HIGH);
digitalWrite(ledPin4, HIGH);
digitalWrite(ledPin5, HIGH);
digitalWrite(ledPin6, HIGH);
digitalWrite(ledPin7, HIGH);
digitalWrite(ledPin8, HIGH);
digitalWrite(ledPin9, HIGH);
digitalWrite(ledPin10, HIGH);
val2 = digitalRead(pbuttonPin2);
if (val2 == LOW){
on = on - 1;
off = off + 1;
brightness = brightness - brightness;
}
}
Serial.println("val");
Serial.println(val);
Serial.println("val2");
Serial.println(val2);
Serial.println("bright");
Serial.println(brightness);
Serial.println("on");
Serial.println(on);
Serial.println("off");
Serial.println(off);
}

if(off == 1){
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
digitalWrite(ledPin5, LOW);
digitalWrite(ledPin6, LOW);
digitalWrite(ledPin7, LOW);
digitalWrite(ledPin8, LOW);
digitalWrite(ledPin9, LOW);
digitalWrite(ledPin10, LOW);
delay(100);
}
}

My problems is, some leds do not fading and i don't know why. The leds do not fade stay HIGH when brightness == 255. Is it the arduino the problems or my code, or the power supply ?

Can you help me :slight_smile:

thanks.

PS: I'm french and a beginner, my english is not very good so if you don't understand tell me ^^

There are only 8 PWM pins on a Arduino Micro. According to the schematic these are 4, 5, 6, 9, 10, 11, 12 and 13. Any chance that the leds that don't fade are on pins 7 and 8?

Please read How to use this forum - please read., specifically point #7 about posting code.

[Edit]
PS not sure if the schematic is 100% correct regarding this, I think that pin 4 does not have PWM but pin 3 has.
[/Edit]

yes there are pin number 4, 7, 8 and 12

Connect the led on pin 4 to pin 3 and change the code accordingly.
Not sure why pin 12 does not work in you scenario; what's the behaviour (always on, always off)?

There is no solution for pins 7 and 8; as said, there are only 8 PWM pins. For a hardware solution, you can look at e.g. Adafruit 16-Channel 12-bit PWM/Servo Driver - I2C interface; for a software solution you can consider software PWM.

pin 12 works like the 4, 7 and 8. stay HIGH when brightness == 255.
the pin 3 and 2 is for push button (pin 2 --> on and pin 3 --> off).
if i connect leds to analog pins, can this work?

Move your switches to other pins; you can use digitalRead on analog pins.

Not sure about pin 12.

i can move only on analog pins, what code for fade on analog pins ?

[Edit]

i try analogWrite(A0, brightness);

but again the led stay LOW but when brightness == 255 led stay HIGH

[/Edit]

analogWrite(A0, brightness);A0 is not a PWM pin.

so i can't use analog pins for fading ? so bad

i'm gonna buy an arduino with more pwm pins

thanks for your answer :slight_smile: