5 PWM outputs on Arduino Nano with IR Remote

Hi all,

I’m doing my first project and I’m stuck on the number of PWM pins I can use on the Nano.

I know there are 6 and it seems that IR Remote uses either 3 or 9, however no matter what I do with either of those pins on digitalwrite its either 255 or 0, anything in between doesnt work. it also seems to crash the nano. Having looked into this it seems it’s likely a timer issue, however this is a bit beyond my current knowledge

Can I easily swap IR Remote for another library without much change to my code?

Thanks for any help

//www.elegoo.com
//2016.12.9

#include "IRremote.h"

int receiver = 12; // Signal Pin of IR receiver
int led1 = 10;           // the PWM pin the LED is attached to
int led2 = 9;           // the PWM pin the LED is attached to
int led3 = 6;           // the PWM pin the LED is attached to
int led4 = 5;           // the PWM pin the LED is attached to
int led5 = 3;           // the PWM pin the LED is attached to
const int buttonPin = 8;     // the number of the pushbutton pin for ON/OFF


int led1on = 0; // Changes to remember state if not OFF
int led2on = 0;
int led3on = 0;
int led4on = 0;
int led5on = 0;
int ledallon = 3;
int aledon = 0;
int brightness = 255;
int buttonState = 0;

/*-----( Declare objects )-----*/
IRrecv irrecv(receiver);     // create instance of 'irrecv'
decode_results results;      // create instance of 'decode_results'

void setup()   /*----( SETUP: RUNS ONCE )----*/
{
  Serial.begin(9600);
  Serial.println("Ollie Rocket - IR Debug");
  irrecv.enableIRIn(); // Start the receiver
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(buttonPin, INPUT);

}/*--(end setup )---*/

void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/
{
  buttonState = digitalRead(buttonPin); // Checks state of on/off button
  
  if (irrecv.decode(&results)) {            // Receives IR codes and flip/flops on/off per command
    unsigned int value = results.value;
    Serial.println(results.value);
    switch (value) {

      case 0xFF30CF:                          // If IR code for button 1 received
        if (led1on == 1) {
          led1on = 0;
        } else {
          led1on = 1;
        }
        break;

      case 0xFF18E7:                          // If IR code for button 2 received
        if (led2on == 1) {
          led2on = 0;
        } else {
          led2on = 1;
        }
        break;

      case 0xFF7A85:                          // If IR code for button 3 received
        if (led3on == 1) {
          led3on = 0;
        } else {
          led3on = 1;
        }
        break;

      case 0xFF10EF:                          // If IR code for button 4 received
        if (led4on == 1) {
          led4on = 0;
        } else {
          led4on = 1;
        }
        break;

      case 0xFF38C7:                          // If IR code for button 5 received
        if (led5on == 1) {
          led5on = 0;
        } else {
          led5on = 1;
        }
        break;

      case 0xFF906F:                          // If IR code for button UP received
        if (brightness <= 216) {
          brightness = brightness + 40;
        } else {
          brightness = brightness;
        }
        Serial.println("UP");
        Serial.println(brightness);
        break;

      case 0xFFE01F:                          // If IR code for button DOWN received
        if (brightness >= 40) {
          brightness = brightness - 40;
        } else {
          brightness = brightness;
        }
        Serial.println("DOWN");
        Serial.println(brightness);
        break;

      case 0xFFA25D:
        if (ledallon == 1 || ledallon == 2 || aledon == 1) {
          ledallon = 0;
        } else {
          ledallon = 1;
        }
        break;
    }
    delay(50); // Do not get immediate repeat
    irrecv.resume(); // Receive the next value
  }





  // WHAT TO DO WHEN FLIP/FLOP on device received

  if (led1on == 1) {          // Check if led1on = 1, if so set led on, if not turn off.
    analogWrite(led1, brightness);           // and set its state as on
    aledon = 1;
  } else {                      // else set it off
    analogWrite(led1, 0);
  }

  if (led2on == 1) {     // If led 2 'ON'
    analogWrite(led2, brightness);
    aledon = 1;
  } else {
    analogWrite(led2, 0);
  }

  if (led3on == 1) {     // If led 3 'ON'
    analogWrite(led3, brightness);
    aledon = 1;
  } else {
    analogWrite(led3, 0);
  }

  if (led4on == 1) {     // If led 4 'ON'
    analogWrite(led4, brightness);
    aledon = 1;
  } else {
    analogWrite(led4, 0);
  }

  if (led5on == 1) {     // If led 5 'ON'
    analogWrite(led5, brightness);
    aledon = 1;
  } else {
    analogWrite(led5, 0);
  }

  if (ledallon == 1) {     // If all led on = 1
    Serial.println("Led ALL ON");
    analogWrite(led1, 50);
    delay(50);
    analogWrite(led1, 0);
    delay(50);
    analogWrite(led1, 255);
    delay(450);
    analogWrite(led2, 50);
    delay(50);
    analogWrite(led2, 0);
    delay(50);
    analogWrite(led2, 255);
    delay(450);
    analogWrite(led3, 50);
    delay(50);
    analogWrite(led3, 0);
    delay(50);
    analogWrite(led3, 255);
    delay(450);
    analogWrite(led4, 50);
    delay(50);
    analogWrite(led4, 0);
    delay(50);
    analogWrite(led4, 255);
    delay(450);
    analogWrite(led5, 255);
    led1on = 1;
    led2on = 1;
    led3on = 1;
    led4on = 1;
    led5on = 1;
    brightness = 255;
    ledallon = 2;
    aledon = 1;
  }

  if (ledallon == 0) {     // If all led on = 0
    Serial.println("Led ALL OFF");
    analogWrite(led5, 0);
    delay(450);
    analogWrite(led4, 0);
    delay(450);
    analogWrite(led3, 0);
    delay(450);
    analogWrite(led2, 0);
    delay(450);
    analogWrite(led1, 0);
    led1on = 0;
    led2on = 0;
    led3on = 0;
    led4on = 0;
    led5on = 0;
    ledallon = 3;
    aledon = 0;
  }




}/* --(end main loop )-- */

You'd need to find an IR remote library that doesn't use a hardware timer; I do not know if one of those exists.

ok thanks,

however should it be using two PWM pins?

I find that I cant use either Pin 11 or Pin 3?

Pin 3 'sort of' works, in that it's on/off, but it wont dim

thx

however should it be using two PWM pins?

Yes using a timer for purposes other than PWM generation means that PWM can not be generated on either pin.

ok thanks all, at least i know I'm not going mad!

Can you suggest any other way to dim my final led?

I've got the lighting effect I want on everything else, but im stuck on getting LED5 to dim nicely :frowning:

Can you suggest any other way to dim my final led?

Try a soft PWM libiary.

Brilliant, thank you all :slight_smile: