'why are my first and second LED not lighting up? is it a problem with the for loops?

When I power my Arduino it only lights up the 2-5 and leds and skips the 1st and the 6th led. I tried seeing what's the problem by giving a check on the value of i in the for loops but it is alright.

These are the for loops I am talking about:

for (int i = 0; i < 6; i++)
{
    analogWrite(arr[i], ledval);
    delay(100);
}
 
for (int i = 0; i < 6; i++)
{
    analogWrite(arr[i], 0);
    delay(100);
    //digitalWrite(arr[i], LOW);
}
 
for (int i = 0; i < 6; i++)
{
    analogWrite(arr1[i], ledval);
    Serial.println(i);
    delay(100);
}
 
for (int i = 0; i < 6; i++)
{
    analogWrite(arr1[i], 0);
    delay(100);        
}

Here is the list:

#include <IRremote.h>
     
int receiver = 12;
int led1 = 3;
int led2 = 5;
int led3 = 6;
int led4 = 9;
int led5 = 10;
int led6 = 11;
int knob = A0;
int arr[] = {led1, led2, led3, led4, led5, led6};
int arr1[] = {led6, led5, led4, led3, led2, led1};
int worknum1 = 0;

IRrecv irrecv(receiver);

and I have initialized every LED

void setup(){
    Serial.begin(9600);
    irrecv.enableIRIn();
    pinMode(led1, OUTPUT);
    pinMode(led2, OUTPUT);
    pinMode(led3, OUTPUT);
    pinMode(led4, OUTPUT);
    pinMode(led5, OUTPUT);
    pinMode(led6, OUTPUT);
    
    pinMode(knob, INPUT);
}

Do you know what's causing this problem? How can I fix this?

Thanks!



Solution 1:[1]

The IR receiving library needs to use a hardware interrupt timer to run code at a certain interval for processing the incoming signal in real time.

It is using timer 2 for this purpose, which is normally responsible for managing the PWM signal on pins 3 and 11 on the Arduino Uno.

See docs:

Incompatibilities to other libraries and Arduino commands like tone() and analogWrite()

If you use a library which requires the same timer as IRremote, you have a problem, since the timer resource cannot be shared simultaneously by both libraries.

[...]

Hardware-PWM signal generation for sending

Board/CPU Receive & PWM Timers Hardware-PWM Pin analogWrite() pins occupied by timer
ATmega328 2 3 3 & 11

Therefore, you cannot use PWM on outputs 3 & 11 together with this library. The docs say you can also manually change which timer is used but that won't help you either because it would then just affect other pins (9 & 10) instead.

If you need more PWM pins, you can instead use an external chip like a TLC5940. See this tutorial for details.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1