I wrote some code with the IRremote library a few months ago, and it worked as intended then. but after not using it or making any changes to it , just sitting , unused for a few weeks , it just does not work. The code is supposed to turn on and off an LED with two buttons , and to change the brightness of the LED with two other buttons. But now instead of subtracting 51 from 255 until it reaches 51, it just turns the LED off and it does not recieve any other codes unless i reset the board.
I hope i was descriptive enough.
Please help me figure it out.
`
#include <IRremote.h>
#define RECV_PIN 5 // Pin du capteur IR
#define LED 3 // LED pin
void setup() {
Serial.begin(9600);
// turn on reciever
IrReceiver.begin(RECV_PIN , ENABLE_LED_FEEDBACK);
Serial.println("IR Receiver Ready");
}
int i = 0;
int const x = 51;
int g = 1;
void loop() {
if (IrReceiver.decode()) {
// Print the hex code
Serial.print("HEX Code : 0x");
Serial.println(IrReceiver.decodedIRData.decodedRawData, HEX);
IrReceiver.resume();
// Turn ON
if (IrReceiver.decodedIRData.decodedRawData == 0xFC03EF00 || IrReceiver.decodedIRData.decodedRawData == 0xB3E2CB06) {
analogWrite(LED, 255);
g = 1;
i = 255;
}
IrReceiver.resume();
// Turn OFF
if (IrReceiver.decodedIRData.decodedRawData == 0xFD02EF00) {
analogWrite(LED, 0);
g = 0;
}
IrReceiver.resume();
// Brightness UP
if (IrReceiver.decodedIRData.decodedRawData == 0xFF00EF00 && i < 255 && g == 1) {
i = i + x;
g = 1;
analogWrite(LED, i);
Serial.print("i=");
Serial.println(i);
}
IrReceiver.resume();
// Brightness DOWN
if (IrReceiver.decodedIRData.decodedRawData == 0xFE01EF00 && i > 51 && g == 1) {
i =i - x;
g = 1;
analogWrite(LED, i);
Serial.print("i=");
Serial.println(i);
}
IrReceiver.resume();
}
IrReceiver.resume();
}
`
You posted your question in the Uploading category of the forum but it does not seem to be a problem with uploading the code so it has been moved to a more relevant forum category
The (assuming) solderless breadboard and the pins can form a small layer of rust (oxidation) with time and current flow. Pull-and-push each pin several times. We used to call this magic "clean and reseat"... when inspecting the connector pins and sockets "fixed" the problem.
[edit]
When the hardware is working again...
I wondered if using resume() so often was part of the problem, so I added some Serial.print() to your sketch to see how the numbers were moving, especially g and i, which enable or disable the if() conditions that add and subtract 51. If i is at or above 255, only DOWN will seem to work. If i is at or below 51, only "UP" will seem to work. Here is the sketch...
// https://forum.arduino.cc/t/my-ir-project-code-does-not-work-the-same-after-not-using-it-after-a-while/1458033/4
#include <IRremote.h>
#define RECV_PIN 5 // Pin du capteur IR
#define LED 3 // LED pin
void setup() {
Serial.begin(9600);
// turn on reciever
IrReceiver.begin(RECV_PIN, ENABLE_LED_FEEDBACK);
Serial.println("IR Receiver Ready");
}
int i = 0;
int const x = 51;
int g = 1;
void loop() {
if (IrReceiver.decode()) {
// Print the hex code
Serial.print("HEX Code : 0x");
Serial.print(IrReceiver.decodedIRData.decodedRawData, HEX);
Serial.print(" g=");
Serial.print(g);
analogWrite(LED, i);
Serial.print(" i=");
Serial.print(i);
IrReceiver.resume(); // restart IrReceive
// Turn ON
if (IrReceiver.decodedIRData.decodedRawData == 0x5DA2FF00) {
analogWrite(LED, 255);
g = 1;
i = 255;
Serial.print(" ON ");
Serial.print(" g=");
Serial.print(g);
analogWrite(LED, i);
Serial.print(" i=");
Serial.print(i);
}
// IrReceiver.resume();
// Turn OFF
if (IrReceiver.decodedIRData.decodedRawData == 0x1DE2FF00) {
analogWrite(LED, 0);
g = 0;
Serial.print(" OFF ");
Serial.print(" g=");
Serial.print(g);
analogWrite(LED, i);
Serial.print(" i=");
Serial.print(i);
}
// IrReceiver.resume();
// Brightness UP
if (IrReceiver.decodedIRData.decodedRawData == 0x6F90FF00 && i < 255 && g == 1) {
i = i + x;
g = 1;
Serial.print(" g=");
Serial.print(g);
analogWrite(LED, i);
Serial.print(" i=");
Serial.print(i);
Serial.print(" UP ");
}
// IrReceiver.resume();
// Brightness DOWN
if (IrReceiver.decodedIRData.decodedRawData == 0x1FE0FF00 && i > 51 && g == 1) {
i = i - x;
g = 1;
Serial.print(" g=");
Serial.print(g);
analogWrite(LED, i);
Serial.print(" i=");
Serial.print(i);
Serial.print(" DOWN ");
}
IrReceiver.resume(); // restart
Serial.println();
}
}
Your LED is connected to pin 3. On an Uno/Nano, analogWrite() on pin 3 uses Timer2, which can conflict with the timer used by IRremote. Try moving the LED to PWM pin 5 or 6 and change #define LED 3 accordingly.
I copied the code and placed it on a new sketch, but every time i try to upload it to the arduino nano , gives me this error :
Error: cannot open port \\.\COM8: Access is denied.
Error: unable to open port COM8 for programmer arduino
Failed uploading: uploading error: exit status 1
You probably need to re-select your board and port. Windows will change the port. I have one physical port that Windows re-names with three different port numbers... here's how to correct it:
IDE >> TOOLS >> BOARD >> AVR BOARDS >> your board
IDE >> TOOLS >> PORT >> your port
IRremote.h uses TIMER2 which makes Arduino PWM Pin 3 and PWM Pin 11 unusable for PWM. Change #define LED 3 to any other available PWM pin, for example, #define LED 6
I move the power indicator LED to its own LED so you can still see when PWM is adjustable (only during power ON state).
// https://forum.arduino.cc/t/my-ir-project-code-does-not-work-the-same-after-not-using-it-after-a-while/1458033/
#include <IRremote.h>
#define IRXPIN 5 // IR receive pin
#define PWMPIN 6 // PWM LED
#define POWER 2 // power indicator
#define POWERBTN 0x5DA2FF00
#define INCREASE 0x6F90FF00
#define DECREASE 0x1FE0FF00
int const step = 51;
int pwmValue = 0;
bool powerState = 0;
void setup() {
Serial.begin(115200);
pinMode(POWER, OUTPUT);
pinMode(PWMPIN, OUTPUT);
pinMode(IRXPIN, INPUT);
IrReceiver.begin(IRXPIN, ENABLE_LED_FEEDBACK); // initialize IRremote object
Serial.println("IR Receiver Ready");
}
void loop() {
if (IrReceiver.decode()) {
long rawData = IrReceiver.decodedIRData.decodedRawData;
printRawData(rawData);
if (rawData == POWERBTN) {
powerState = !powerState; // toggle LED state which controls PWM adjust
digitalWrite(POWER, powerState); // write powerState to LED
if (powerState == 0) {
pwmValue = 0;
analogWrite(PWMPIN, pwmValue); // turn off PWM pin
}
}
printpowerState(); // needs to be after POWER button check
if (powerState) { // allow PWM adjust
if (rawData == INCREASE) { // right arrow
pwmAdjust(1); // increase
}
if (rawData == DECREASE) { // left arrow
pwmAdjust(-1); // decrease
}
}
IrReceiver.resume(); // re-enable decoding
Serial.println(); // finish output line
}
}
void pwmAdjust(int direction) {
Serial.print(direction > 0 ? " UP" : " DN");
pwmValue = pwmValue + (step * direction); // adjust PWM up or down
if (pwmValue < 51) pwmValue = 51; // lower boundry
if (pwmValue > 255) pwmValue = 255; // upper boundry
Serial.print(" pwmValue=");
Serial.print(pwmValue);
analogWrite(PWMPIN, pwmValue);
}
void printRawData(long data) {
Serial.print("HEX Code=0x");
Serial.print(data, HEX); // Print hex code
}
void printpowerState() {
Serial.print(" powerState=");
Serial.print(powerState);
}