Hi all,
(Edit: posted it here because the IR sensor is a sensor.... might not be the best part of the forum)
Got a strange one here...
Running the IR library of Ken Shirriff... the sketches work a treat, to show what key on my remote is being pressed.
So then I thought I'd make a key actually do something, and decided to use the volume keys to control an LED's brightness. Added a few lines to Ken's code (see below) to do an analog write to pin 3 with a value which is increased or decreased when the volume up or down key is pressed.
The LED on pin 3 turned out to be either on or off though, and nothing in between. When I upped or downed the volume the LED was off unless I pressed the key to get up to the max of 255. I changed my code to use PWM on 5, moved the LED, and Lo! my LED dims and brightens as expected.
Thinking my pin 3 might be faulty and have lost its PWMability, I wrote a simple sketch to send arbitrary values between 0 and 255 to pin 3 and the LED brightness changes as expected.
So, I'm wondering if something in the IR library has disabled PWMing on pin 3?
Here's the code, but bear in mind with led_pin set to 5 it works as expected; with led_pin = 3, the led is on or off. (Edit: The serial print shows the correct brightness between 10 and 255, and that we're going up or down)
/*
* IRremote: IRrecvDemo - demonstrates receiving IR codes with IRrecv
* An IR detector/demodulator must be connected to the input RECV_PIN.
* Version 0.1 July, 2009
* Copyright 2009 Ken Shirriff
* http://arcfn.com
*/
/* well now it controls the brightness of an LED
LED is PWM'd on pin5
******* PWM on pin3 seems disabled when using IR?
uses the volume key... up to brighten, down to dim
under rc5, the keys have two toggled values
down: 411 or c11 hex; 1041 or 3089 dec
up: 410 or c10 hex; 1040 or 3088 dec
*/
#include <IRremote.h>
int RECV_PIN = 11;
int led_pin = 5; // seems PWM not work on 3 with IR library
int led_bright = 130; //half way to start
int led_step = 10; // and change in pwm steps of 10
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
pinMode(led_pin, OUTPUT);
analogWrite(led_pin, led_bright);
}
void loop() {
if (irrecv.decode(&results)) {
Serial.println(results.value, HEX);
irrecv.resume(); // Receive the next value
//now check if it's correct key and act...
//down
if (results.value == 1041 || results.value == 3089) {
led_bright = led_bright - led_step;
if (led_bright < 10) {
led_bright = 10;
}
Serial.print("Going down to ");
Serial.println(led_bright);
analogWrite(led_pin, led_bright);
}
//up
if (results.value == 1040 || results.value == 3088) {
led_bright = led_bright + led_step;
if (led_bright > 255) {
led_bright = 255;
}
Serial.print("Going up to ");
Serial.println(led_bright);
analogWrite(led_pin, led_bright);
}
}
}