Go Down

Topic: Controlling LED brightness, PWM not working! (solved) (Read 553 times) previous topic - next topic

tetigrijo

May 18, 2012, 07:06 pm Last Edit: May 18, 2012, 07:31 pm by tetigrijo Reason: 1
Hey guys I'm having some problems using the function analogWrite. I'm developing a program that get data from a remote control (infrared) to adjust the led brightness. But it seens that the PWM is not working, I tested other programs and they worked very well, so it's not a problem with the board. The just turn's on when I send 255 to the function analogWrite, values between 0-254 does nothing. I will post the code here, so if anyone sees the error, please tell me! Obs: pinoLed = ledpin


----------------------------------------------------
#include <IRremote.h>

#define Botplay 16761405
#define Botmais 16754775
#define Botmenos 16769055
#define Bot1 16724175
#define Bot2 16718055
#define Bot3 16743045
#define Bot4 16716015
#define Bot5 16726215
#define Bot6 16734885
#define Bot7 16728765
#define Bot8 16730805
#define Bot9 16732845
#define Bot0 16738455


int RECV_PIN = 2;
int pinoLed = 3;
int cont1 = 0;
int cont2 = 0;

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{
 Serial.begin(9600);
 Serial.flush();
 pinMode(pinoLed, OUTPUT);
 irrecv.enableIRIn(); // Start the receiver
}

void loop() {
 if (irrecv.decode(&results)) {

   if(results.value != Botplay){
         cont1 = cont1*10;
   }
   if(results.value == Botplay){
     Serial.print("cont1 ");
     Serial.println(cont1);
     cont1 = constrain(cont1,0,255);
     Serial.print("cont1 ");
     Serial.println(cont1);
     analogWrite(pinoLed, cont1);
     cont1 = 0;
     cont2 = 0;
   }
   Serial.print("Botao:  ");
   if (results.value == Bot1){
     Serial.println(1);
     cont1 = cont1 + 1;
     cont2 = cont2 + 1;
   }
   if (results.value == Bot2){
     Serial.println(2);
     cont1 = cont1 + 2;
     cont2 = cont2 + 1;
   }
   if (results.value == Bot3){
     Serial.println(3);
     cont1 = cont1 + 3;
     cont2 = cont2 + 1;
   }
   if (results.value == Bot4){
     Serial.println(4);
     cont1 = cont1 + 4;
     cont2 = cont2 + 1;
   }
   if (results.value == Bot5){
     Serial.println(5);
     cont1 = cont1 + 5;
     cont2 = cont2 + 1;
   }
   if (results.value == Bot6){
     Serial.println(6);
     cont1 = cont1 + 6;
     cont2 = cont2 + 1;
   }
   if (results.value == Bot7){
     Serial.println(7);
     cont1 = cont1 + 7;
     cont2 = cont2 + 1;
   }
   if (results.value == Bot8){
     Serial.println(;
     cont1 = cont1 + 8;
     cont2 = cont2 + 1;
   }
   if (results.value == Bot9){
     Serial.println(9);
     cont1 = cont1 + 9;
     cont2 = cont2 + 1;
   }
   if (results.value == Bot0){
     Serial.println(0);
     cont1 = cont1 + 0;
     cont2 = cont2 + 1;
   }
   Serial.print("contador1 :  ");
   Serial.println(cont1);
   Serial.print("contador2 :  ");
   Serial.println(cont2);
   
   if (cont2>=3){
     cont1 = constrain(cont1,0,255);
     analogWrite(pinoLed, cont1);
     cont1 = 0;
     cont2 = 0;
   }
   
   delay(1000);
   irrecv.resume(); // Receive the next value
 }
}
----------------------------------------------------------------------------------------

tetigrijo

Problem solved, changed the ledpin to pin 9. The IRremote lib uses timer2, that uses pins 3 and 11...

Go Up