analogWrite, arduino pro micro

Trying to make IR controlled RGB strip. PWM pins of micro are connected to n-channel mosfet-s.
Everything works as expected only blue (pin 10) wont change brightness gradually, just from 0 to 100 and then if increased to 255. Tried to move to pin 3 but same thing happens.

#define DECODE_RC5
#include <Arduino.h>
#include "PinDefinitionsAndMore.h"

#include <IRremote.h>
int bila = 6;
int crvena = 3;
int zelena = 9;
int plava = 10;
int r = 5;
int g = 5;
int b = 5;
int w = 5;
void setup() {


pinMode(bila, OUTPUT); 
pinMode(crvena, OUTPUT); 
pinMode(zelena, OUTPUT); 
pinMode(plava, OUTPUT);
    Serial.begin(115200);
    
    IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK, USE_DEFAULT_FEEDBACK_LED_PIN);
}

void loop() {
  analogWrite(plava, b);
  analogWrite(bila, w);
  analogWrite(crvena, r);
  analogWrite(zelena, g);
            if (r < 0) {r = 0;}
            if (r > 255) {r = 255;}
            if (g < 0) {g = 0;}
            if (g > 255) {g = 255;}
            if (w < 0) {w = 0;}
            if (w > 255) {w = 255;}
            if (b < 0) {b = 0;}
            if (b > 255) {b = 255;}
 if (IrReceiver.decodedIRData.command == 0x1) {
            w = w - 1;
           
        } else if (IrReceiver.decodedIRData.command == 0x3) {
             w = w + 1;
           
        }else if (IrReceiver.decodedIRData.command == 0x2) {
            w = 30;
        }else if (IrReceiver.decodedIRData.command == 0x4) {
             r = r - 1;
           
        } else if (IrReceiver.decodedIRData.command == 0x6) {
             r = r + 1;
         
        }else if (IrReceiver.decodedIRData.command == 0x5) {
            r = 30;
        }else if (IrReceiver.decodedIRData.command == 0x7) {
          g = g - 1;
            
        } else if (IrReceiver.decodedIRData.command == 0x9) {
             g = g + 1;
            
        }else if (IrReceiver.decodedIRData.command == 0x8) {
            g = 30;
        
        }else if (IrReceiver.decodedIRData.command == 0x26) {
           b = b - 1;
           
        } else if (IrReceiver.decodedIRData.command == 0x22) {
             b = b + 1;
            
        }else if (IrReceiver.decodedIRData.command == 0x11) { // volume down  button
         
           r = r - 1;
           g = g - 1;
           w = w - 1;
           b = b -1
         
        } else if (IrReceiver.decodedIRData.command == 0x10) {  // volume up button
             
           r = r + 1;
           g = g + 1;
          w = w + 1;
           b = b + 1; 
          
        }
       else if (IrReceiver.decodedIRData.command == 0xC) {
        
        b = 0;
        r = 0;
        g = 0;
        w = 0;
       }
       else if (IrReceiver.decodedIRData.command == 0x35) {
        fade();  // this function is empty for now
       }
   IrReceiver.resume(); 
}

???
p.s. ir receiver is connected to pin 2.

What MOSFETs are you using? Are they the logic level type you need to work with a microcontroller?

Also, you loop() never checks to see if you have actually received a valid command before processing. Look at the IRremote examples.

You can probably more easily use a switch statement vs. all those if/elseif/ clauses. It is a matter of taste/style.

The PWM of pin 10 might use the same timer as the IRRemote library. Can you make a small test-sketch without the IRRemote library to check if the blue led can change gradually ?

1 Like

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