what i am trying to do is:
read sth from IR interface and set PWM signal to an LED.
But i dont understand the behavior i am getting...
Hardware that i use: ESP8266
#include <IRremote.h>
#define IRPIN 13
int pwmvalue=0;
const int ledPin = 4;
int counter = 0;
IRrecv receiver(IRPIN);
void setup()
{
Serial.begin(9600);
Serial.println("Enabling IRin");
receiver.begin(IRPIN, ENABLE_LED_FEEDBACK);
Serial.println("Enabled IRin");
}
void loop()
{
if (receiver.decode())
{
int value = receiver.decodedIRData.decodedRawData;
if (-350945536 == value) {
//Serial.println("up");
pwmvalue = pwmvalue + 50;
} else if (-284098816 == value) {
//Serial.println("down");
pwmvalue = pwmvalue - 50;
}
if (0 > pwmvalue)
pwmvalue = 0;
if (1024 < pwmvalue)
pwmvalue = 1024;
receiver.resume();
Serial.println(pwmvalue);
//analogWrite(ledPin, pwmvalue);
Serial.println(pwmvalue);
}
Serial.println(counter);
counter ++;
delay(500);
}
The code works completely as expected without " //analogWrite(ledPin, pwmvalue);".
If i use it with the analog write, it will handle the "down" button ok.
If i use the "up" button, it will do it once. Both println() get printed out, and the LED will light up. But from then on the receiver.decode never delivers any data anymore. The println() of the counter keeps on working.
I don't get how they interfere with each other.
I already read through a lot of articles. Even the interrupt method... but i guess with that not working, i should not continue.
Does nobody have any idea where to point me?
Basically:
Writing an "analog" Value as PWM to an LED works.
Reading an IR Remote works as well. The sketch detects the right buttons and so on.
Just putting the two together does not work. And i have no clue why.
As far as i understood: that is the led built into the ir receiver board.
The led i am trying to control via analogwrite() ia a separate one connected to a different pin (here pin 4) which works fine if i just use the analogwrite() in a separate program with sth loke a for loop.
thank you for your answer!!
i guess my description is not good enough. Sorry for that. i will work on my wording.
if i use this code as "main loop":
void loop()
{
if (receiver.decode())
{
int value = receiver.decodedIRData.decodedRawData;
if (-350945536 == value) {
pwmvalue = pwmvalue + 50;
} else if (-284098816 == value) {
pwmvalue = pwmvalue - 50;
}
if (0 > pwmvalue)
pwmvalue = 0;
if (1024 < pwmvalue)
pwmvalue = 1024;
receiver.resume();
Serial.println(pwmvalue);
}
Serial.println(counter);
counter ++;
delay(500);
}
the IR signals are recognized correctly and the serial output is also as expected.
BUT ... with this code (only added the analogWrite() ) the IR signal "down" button is read... but as soon as i hit the "up" button, the signal is read once and then never again. (receiver.decode() will never get true anymore)
the output "Serial.println(counter);" is still written on the serial port. So the "normal" work is still handled.
void loop()
{
if (receiver.decode())
{
int value = receiver.decodedIRData.decodedRawData;
if (-350945536 == value) {
pwmvalue = pwmvalue + 50;
} else if (-284098816 == value) {
pwmvalue = pwmvalue - 50;
}
if (0 > pwmvalue)
pwmvalue = 0;
if (1024 < pwmvalue)
pwmvalue = 1024;
receiver.resume();
analogWrite(ledPin, pwmvalue);
Serial.println(pwmvalue);
}
Serial.println(counter);
counter ++;
delay(500);
}
i also have a different sketch with which i tested the PWM signal output. This works also completely as expected. like the LED gets brighter and less bright. so the "wiring" seems to be ok.
So my assumption is that the PWM in combination with the serial read has some interference... but if that is in the library... it might be a little hard to find for me.
maybe the pin i chose is not the greatest?! but i dont know which pins else to chose. at least not from the documentation.
if i disconnect the LED (connected to D2 - GPIO4) the LED goes off. if i plug it back in, it will go on. The other LED cannot be disconnected without desoldering.
but it does not change anything in the serial output or anything.
the ir read does not return.
if i disconnect the LED, it is like a light bulb... the circuit does not recognize it. if i replace/put it back in it is like it has never been removed.
so no, the ir decode still does not return with true.
no the code works with or without LED connected.
just the difference is to write to PWM output.
seems like the writing of a value greater then 0 blocks the ir read mechanism.
i played around a little... i can confirm that if i write a value greater than 0, the ir read mechanism does not recognize any command anymore.
a value equal to 0 does not block the ir reading. that means the serial output shows that the ir is read and that it can be read more than once.