Reading ir codes from a remote, I need to execute an analogWrite when a match occurs. I've tested analogWrite separately from an IF statement and that seems to work. But when I put it in an ir reader comparison, analogWrite doesn't write anything. The ir read loop works fine, it just ignores the analogWrite command.
A digitalWrite however, does just fine. In the code below, I have included a digitlWrite and analogWrite. The digitalWrite works, but the analogWrite doesn't write anything - i.e., always zero. The delay functions are working as ar the Serial.println statements. What am I missing?
#include <IRremote.h>
//int RECV_PIN = 13;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup() {
// initialize the digital pin as an input.
pinMode(13, INPUT);
// Initialize pin 3 as OUTPUT for LED)
pinMode (3, OUTPUT);
irrecv.enableIRIn(); // Start the receiver
Serial.begin(9600);
}
void loop() {
int i=0;
if (irrecv.decode(&results)) {
translateIR();
// unknownRemoter();
irrecv.resume(); // Receive the next value
}
}
void translateIR() // takes action based on IR code received describing Car MP3 IR codes
{
//
if (results.value == 0xC){
Serial.println ("results was 0xC");
digitalWrite (3, HIGH);
delay (2000);
digitalWrite (3,0);
delay (2000);
Serial.println ("start analogWrite");
analogWrite(3, 25);
delay (200);
analogWrite(3, 50);
delay (200);
analogWrite (3, 75);
delay (200);
analogWrite (3, 50);
delay (200);
analogWrite (3, 25);
delay (200);
analogWrite (3, 0);
delay (1000);
Serial.println ("END");
}
}
What board are you using? There were some old boards where pin 3 wasn't a PWM pin.
What is connected to pin 3, i.e. what are you using to test that the analogWrite() is doing anything? Since you're only rapidly flipping up and down to a max of 75 (out of 255) it will need to be something pretty responsive.
Try doing analogWrite(3, 255) which is equivalent to a digitalWrite(3, HIGH) and see if that works.
I'm using an Elegoo UNO R3. I wasn't aware that pin 3 didn't support pwm on some models. Checking, pin 3 is marked as pwm capable, and it does accept an analogWrite in test code. It only fails when I put it inside the ir routine.
My test circuit is a single LED which in other tests, is actually at about full brilliance at analog 80 or so. However, I tried it at 255 and still see nothing. The digitalWrite works just fine, even inside the ir routine.
I will try other pins just to see what happens.
My final version will be using a Pro-mini which also indicates pin 3 is analog capable. As a last resort, I can use a Pro-mini for the ir loop and do a digitalWrite to a second Pro-mini to actually do the analog function. A little clumsy, but possible. I'm a little cramped for space though so I'm still hoping there a one-Arduino solution.
Some of this 'logic' just doesn't make sense sometimes!
I tried the other analog pins - 5,6,9,10 and 11.
Of those, pins 6, 9, and 10 respond as expected to an analogWrite within the ir routine. pins 5 and 11 do not..
Don't understand the 'why' of it, but I can adjust the rest of my circuits to work with this.
Don't know. Guess it could be. It's strange though that it seems to be related to the ir routine. Pin 3 works with a direct analogWrite outside the ir loop. I suspected maybe the fundamental pwm frequency might be interfering with the ir frequency somehow, but pin 3 and 10 use the same frequency. But while I'm into this, I will do some tests on another Arduino unit just so we can eliminate anything weird with this particular board.
Meanwhile I'm good with using pin 10 which is a functional solution, but doesn't provide a 'why'.