analogWrite not working

just messing around with pwm and ir remote and fading leds, the code runs fine, checked with serial.print before and after every single command on the function fadding but the led it self doesnt blink, if i put just the fadding code it runs fine and fades, but using it on this piece of code it wont run… any ideas?

the led does occasionally flashes at full brightness, but no visible pattern or fadding.

also, im using an uno.

sketch_jul19a.ino (2.04 KB)

#include <IRremote.h>
unsigned long currentm=0;
unsigned long  previousm=0;
bool state=0;
int interval=100;
int fade=5;
int brightness=0;
void toggle_relay1();
void toggle_relay2();
void up_b();
void down_b();
long botton1=3238126971;
long botton2=2538093563;
long botton3=4039382595;
long botton4=2534850111;
long botton5=1033561079;
long botton6=1635910171;
long botton7=2351064443;
long botton8=1217346747;
long botton9=71952287;
long botton0=465573243;
long bottonok=3622325019;
long bootonup=5316027;
byte steps=0;
int on = 0;
bool fade2=false;
byte receiver = A0;
byte RELAY1=4;
byte RELAY2=5;
int led=3;
IRrecv irrecv(receiver);     // create instance of 'irrecv'
decode_results results;      // create instance of 'decode_results'
void setup()
{
  Serial.begin(9600); 
  irrecv.enableIRIn(); // Start the receiver
  pinMode(RELAY1, OUTPUT);
  pinMode(RELAY2, OUTPUT);
  pinMode(led, OUTPUT);
  digitalWrite(RELAY1, HIGH);
  digitalWrite(RELAY2, HIGH);
}

void loop()
{
  currentm=millis();
  if (irrecv.decode(&results)) // have we received an IR signal?

  {
    translateIR(); 
    Serial.println(results.value);
    irrecv.resume(); // receive the next value
  }
  if(fade2==true){
    fadding();
    }  
}
void translateIR()

{

  switch(results.value)

  {
  case 3238126971: toggle_relay1();break;
  case 2538093563: toggle_relay2();break;
  case 16756815: fadeornot();break;
  case 5316027: up_b(); break;
  case 515905009: down_b(); break;
  case 0xFFFFFFFF: break;  

  default: 
    Serial.println();

  }// End Case


} 
void fadeornot(){ 
  if(fade2==true){
    fade2=false;}else{
      fade2=true;}
  }
  

void fadding(){
   if(currentm-previousm>interval){
  previousm=currentm;
  analogWrite(led,brightness);
  Serial.println(led);
  brightness=brightness+fade;
  if(brightness<=0||brightness>=255){
      fade=-fade;
      }  
   }
  }
void up_b(){
  
  }
void down_b(){
  
  }
void toggle_relay1 (){
  Serial.println("botton 1");
  on = !on;
  digitalWrite(RELAY1, on ? HIGH : LOW);
}

void toggle_relay2 (){
  on = !on;
  digitalWrite(RELAY2, on ? HIGH : LOW);
}

Learn to read the instructions and to follow them! this will avoid you looking like a complete idiot!

Mark

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

It is not too clear from your post what the problem is.
Is it just ramping the brightness up and down too fast for you to see?
Try sticking a small delay in the fadding() function

Hey Arklamovi,

Edit your second post, and put
** **[code]** **
as the first line and
** **[/code]** **
as the last line.

there is just no output on the digital pin 3, has if the analogWrite function wasnt being processed, i tried putting a delay there but no luck.

here’s a cad of the circuit, where the led is actually a led strip, if i put just the function fadding() and it’s respective variables in a separate sketch, it runs fine and the led strip turns on and fades no problem or what so ever, but on this code, the led wont turn on, but i do get a serial print of the led pin,

previousm=currentm;
  analogWrite(led,brightness);
  Serial.println(led);
  brightness=brightness+fade;

stating that the code is running whenever i press the controller button, and stops when i press it again.
but the led it self never turn on.

Hi,
Can you check and see what adding the delay does in your code.

void fadding(){
   if(currentm-previousm>interval){
  previousm=currentm;
  analogWrite(led,brightness);
  Serial.println(led);
  brightness=brightness+fade;
  delay(100);
  if(brightness<=0||brightness>=255){
      fade=-fade;
      }  
   }
  }

Thanks… Tom… :slight_smile:

same has before tom, it occasionally blinks the led but just that, a flash, no fadding or constant light…

You can make your code easier to read by formatting it.

Use in the IDE to format the sketch.

cleaned up the code a little.

#include <IRremote.h>
unsigned long currentm = 0;
unsigned long  previousm = 0;
int interval = 10;
int fade = 5;
int brightness = 0;
bool fade2 = false;
byte receiver = A0;
int led = 3;
IRrecv irrecv(receiver);     // create instance of 'irrecv'
decode_results results;      // create instance of 'decode_results'


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



void loop()
{
  currentm = millis();
  if (irrecv.decode(&results)) // have we received an IR signal?

  {
    translateIR();
    Serial.println(results.value);
    irrecv.resume(); // receive the next value
  }
  if (fade2 == true) {
    fadding();
  }
}
void translateIR()

{

  switch (results.value)

  {

    case 16756815: fadeornot(); break;

    default:Serial.println();

  }

}
void fadeornot() {
  if (fade2 == true) {
    fade2 = false;
  } else {
    fade2 = true;
  }
}


void fadding() {
  if (currentm - previousm > interval) {
    previousm = currentm;
    analogWrite(led, brightness);
    Serial.println(led);
    brightness = brightness + fade;
    if (brightness <= 0 || brightness >= 255) {
      fade = -fade;
    }
  }
}

Put a few print statements in your code at various points so you can see what is happening to variables and hence find the error.
If you think your board is faulty try the blink example.

hammy:
Put a few print statements in your code at various points so you can see what is happening to variables and hence find the error.
If you think your board is faulty try the blink example.

i did and everything seens normal, the brightness value increases and decreases every 10 milliseconds by a factor of 5 and the led pin is correct, the currentm and previousm is always correct has well, it should work but it doesnt.
and it isnt the board, i tested all of the ports and voltages, everything is running normally, only on that sketch
and on that port of the code, the analogWrite is not working. there is no output at all on the pin 3, other than flashing rapidly every 5 seconds or so.

IRRemote library uses timer2.

Timer2 is responsible for PWM on pin 3 and 11, so when IRRemote is in use, PWM is not available on those pins.

DrAzzy:
IRRemote library uses timer2.

Timer2 is responsible for PWM on pin 3 and 11, so when IRRemote is in use, PWM is not available on those pins.

Good catch...

This should help, use another PWM pin except pins 3 and 11.

Tom... :slight_smile:

DrAzzy:
IRRemote library uses timer2.

Timer2 is responsible for PWM on pin 3 and 11, so when IRRemote is in use, PWM is not available on those pins.

and that just fixed it, i'm going to study a little more about the atmega328, since it does have some secrets behind up its sleeves. thank you all for the answers and excuse my bad ingles, i'm not a native speaker.

now another question, how can i attach an interrupt to the ir receiver, where it will stop whatever it is doing when i press a button and go do what the respective pressed button is programmed to do in the switch case function ?

Hi,
I think you will find the library already uses an interrupt to do what you think you need.

So you do not need to use an extra interrupt.

Tom… :slight_smile:

Not sure if this will work with your IR system.
Here is a library I wrote for NEC protocol.
Does not use timers, does use interrupts.

larryd:
Not sure if this will work with your IR system.
Here is a library I wrote for NEC protocol.
Does not use timers, does use interrupts.

New Library for IR Remotes - Exhibition / Gallery - Arduino Forum

awesome, i'm going to check it out tomorrow and see what i can do with it.. i have plans to drive a 16x2 lcd with temperature values in it and a few other things, while the arduino also controls my lights(including some rgb strips)and the ac power for my sound system.

hope it works without too much of a headache, thank you all.