Continuing the discussion from Press and Hold IR Remote Control Button:
#include "IRremote.h"
//LarryD
//Sketch to demonstrate using an IR hand remote to control Arduino outputs.
//The remote used here uses the NEC protocol.
//This remote sends the button code (example 0xFF629D) then a repeat code 0xFFFFFFFF
//The repeat code is re-sent as long as an IR remote button is pressed.
//The IR receiver used is the TSOP4838
/* 17 IR button remote layout http://i.imgur.com/X1KIdqI.png
^
< OK >
v
1 2 3
4 5 6
7 8 9
- 0 #
*/
const byte RECV_PIN = 7; //IR receive pin
IRrecv irrecv(RECV_PIN); //create instance of 'IRrecv'
decode_results results; //create instance of 'decode_results'
unsigned long TimerUp; //UP arrow on the remote
boolean TimerUpFlag = false;
unsigned long TimerDown; //DOWN arrow on the remote
boolean TimerDownFlag = false;
unsigned long TimerIntensity;
unsigned long TimerIncDec;
boolean intesityUpFlag = false;
boolean intesityDownFlag = false;
int brightness;
unsigned long dummy;
unsigned long * TimerPtr = &dummy; //pointer to the current timer
const byte upLED = 13; //turns on as long as the UP button is pressed
const byte downLED = 12; //turns on as long as the DOWN button is pressed
const byte leftLED = 11; //toggles on/off
const byte rightLED = 10; //toggles on/off
const byte intensityLED = 9; //a LED which can have its intensity adjusted
// s e t u p ( )
//**********************************************************************
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); //start receive
pinMode(upLED, OUTPUT);
pinMode(downLED, OUTPUT);
pinMode(leftLED, OUTPUT);
pinMode(rightLED, OUTPUT);
pinMode(intensityLED, OUTPUT); //this is a PWM output pin
} // E N D O F s e t u p ( )
// l o o p ( )
//**********************************************************************
void loop()
{
if (irrecv.decode(&results)) //is there IR remote button code
{
//Serial.println(results.value);
processButton(); //process button press
irrecv.resume(); //restart for next button press
}
//********************** //Turn off upLED
//if timing is enabled, is it time to stop
if (TimerUpFlag && millis() - TimerUp >= 250ul)
{
TimerUpFlag = false; //disable timing
TimerPtr = &dummy; //pointer to dummy timer
digitalWrite(upLED, LOW);
}
//********************** //Turn off downLED
//if timing is enabled, is it time to stop
if (TimerDownFlag && millis() - TimerDown >= 250ul)
{
TimerDownFlag = false; //disable timing
TimerPtr = &dummy; //pointer to dummy timer
digitalWrite(downLED, LOW);
}
//********************** //LED intensity
//are we still within the adjustment time
if (millis() - TimerIntensity <= 300ul)
{
//is it time to increase/decrease the intensity
if (millis() - TimerIncDec >= 200ul)
{
TimerIncDec = millis();
if (intesityUpFlag == true) //Increase
{
brightness += 5;
if (brightness > 255)
{
brightness = 255;
}
}
else if (intesityDownFlag == true) //Decrease
{
brightness -= 5;
if (brightness < 0)
{
brightness = 0;
}
}
analogWrite(intensityLED, brightness);
//Serial.println(brightness); //debug
}
}
//stop increasing/decreasing intensity
else
{
intesityUpFlag = false;
intesityDownFlag = false;
}
//************************************
//Other non blocking code goes here
//************************************
} // E N D O F l o o p ( )
//======================================================================
// F U N C T I O N S
//======================================================================
// p r o c e s s B u t t o n ( )
//**********************************************************************
//process IR remote button presses
void processButton()
{
switch (results.value)
{
//**********************
case 0xFF629D: //UP Arrow
{
Serial.println("UP");
TimerPtr = &TimerUp; //point to this timer
TimerUpFlag = true; //enable timing
digitalWrite(upLED, HIGH);
TimerUp = millis();
}
break;
//**********************
case 0xFFA857: //DOWN Arrow
{
Serial.println("DOWN");
TimerPtr = &TimerDown; //point to this timer
TimerDownFlag = true; //enable timing
digitalWrite(downLED, HIGH);
TimerDown = millis();
}
break;
//**********************
case 0xFF22DD: //LEFT Arrow
{
Serial.println("LEFT");
digitalWrite(leftLED, !digitalRead(leftLED)); //Toggle LED
}
break;
//**********************
case 0xFFC23D: //RIGHT Arrow
{
Serial.println("RIGHT");
digitalWrite(rightLED, !digitalRead(rightLED)); //Toggle LED
}
break;
//**********************
case 0xFF42BD: // * button
{
Serial.println("*");
TimerPtr = &TimerIntensity; //point to this timer
intesityUpFlag = true; //enable intensity up adjustment
intesityDownFlag = false;
TimerIncDec = millis();
TimerIntensity = millis();
}
break;
//**********************
case 0xFF52AD: // # button
{
Serial.println("#");
TimerPtr = &TimerIntensity; //point to this timer
intesityDownFlag = true; //enable intensity down adjustment
intesityUpFlag = false;
TimerIncDec = millis();
TimerIntensity = millis();
}
break;
//**********************
case 0xFF02FD:
Serial.println("OK");
break;
//**********************
case 0xFF6897:
Serial.println("1");
break;
//**********************
case 0xFF9867:
Serial.println("2");
break;
//**********************
case 0xFFB04F:
Serial.println("3");
break;
//**********************
case 0xFF30CF:
Serial.println("4");
break;
//**********************
case 0xFF18E7:
Serial.println("5");
break;
//**********************
case 0xFF7A85:
Serial.println("6");
break;
//**********************
case 0xFF10EF:
Serial.println("7");
break;
//**********************
case 0xFF38C7:
Serial.println("8");
break;
//**********************
case 0xFF5AA5:
Serial.println("9");
break;
//**********************
case 0xFF4AB5:
Serial.println("0");
break;
//**********************
case 0xFFFFFFFF: //Repeat code
{
Serial.println("REPEAT");
*TimerPtr = millis(); //reset the current timer
}
break;
} // END switch case
} // E N D o f p r o c e s s B u t t o n ( )
//**********************************************************************
//======================================================================
// E N D O F C O D E
//======================================================================
I'm new at this, and want to modify the code to suite my needs. I'm removing the dimmer code, and need to add additional buttons for source selection and mute. I only need momentary pulses, not steady state.
List all the receive codes for your particular NEC remote.
What exactly needs to happen when a receive code is received ?
I'm currently using a remote that came with the Arduino starter kit. Eventually, I'll be designing a custom remote with only the features/buttons I desire. I'll need to learn how to alter the code to match a purchased remote (future), or how to create code for a custom remote.
I'm in the process of gathering the button codes for the starter kit remote.
Thanks for your help, LarryD. ![]()
Oh, and I did succeed in removing the PWM code, and not disturb the volume up/down and steady state features.
Most people here do not have the Arduino Starter Kit.
At least take a picture if it and show us your image of it.
What exactly needs to happen when a receive code is received ?
Button wise, I need momentary pulse per button. I require on/off, volume +/-, mute, and six source buttons. The power button is on the remote, as are the volume +/-. The numbers 1 through 6 are available, so I'd probably use Func/Stop as my mute.
Here are the button codes listed left to right from the top of the remote:
FFA25D--on/off
FF629D--Vol+
FFE21D--Func/Stop
FF22DD--Left arrows
FF02FD--Play/Pause
FFC23D--Right arrows
FFE01F--Down arrow
FFA857--Vol-
FF906F--Up arrow
FF6897--0
FF9867--EQ
FFB04F--St/Rept
FF30CF--1
FF18E7--2
FF7A85--3
FF10EF--4
FF38C7--5
FF5AA5--6
FF42BD--7
FF4AB5--8
FF52AD--9
What exactly needs to happen when a receive code FFA25D is received ?
I can use a 555 timer as a latching switch to turn the sound system on/off: one pulse on, and another pulse off. So, a simple pulse out when I press the button will do.
Why not an Arduino output that goes LOW on 1st press then HIGH on 2nd press ?
Here's a pic of the remote:

That would be simpler, wouldn't it? Great suggestion, LarryD.
Here is a start.
Do you understand what needs to be done ?
//https://forum.arduino.cc/t/expanding-on-larryds-ir-remote-control-code/1014754/15
//********************************************^************************************************
// XXXXXXXXXX.ino
//
//
// Version YY/MM/DD Comments
// ======= ======== ========================================================
// 1.00 22/07/21 Running code
//
//
//********************************************^************************************************
#include "IRremote.h"
//Sketch to demonstrate using an IR hand remote to control Arduino outputs.
//The remote used here uses the NEC protocol.
//This remote sends the button code (example 0xFF629D) then a repeat code 0xFFFFFFFF
//The repeat code is re-sent as long as an IR remote button is pressed.
//The IR receiver used is the TSOP4838
//********************************************^************************************************
#define LEDon HIGH
#define LEDoff LOW
#define RELAYon HIGH
#define RELAYoff LOW
#define ENABLED true
#define DISABLED false
#define PUSHED LOW
#define RELEASED HIGH
//********************************************^************************************************
const byte heartbeatLED = 13;
const byte onOffLED = 12;
//IR receive pin
const byte RECV_PIN = 7;
//timing stuff
unsigned long heartbeatMillis;
//create instance of 'IRrecv'
IRrecv irrecv(RECV_PIN);
//create instance of 'decode_results'
decode_results results;
// s e t u p ( )
//********************************************^************************************************
void setup()
{
Serial.begin(115200);
irrecv.enableIRIn(); //start receive
pinMode(heartbeatLED, OUTPUT);
pinMode(onOffLED, OUTPUT);
} //END of setup()
// l o o p ( )
//********************************************^************************************************
void loop()
{
//*********************************
//is it time to toggle the heartbeatLED ?
if (millis() - heartbeatMillis >= 500)
{
//restart this TIMER
heartbeatMillis = millis();
//toggle the heartbeatLED
digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
}
//*********************************
//have we received a IR code ?
if (irrecv.decode(&results)) //is there IR remote button code
{
//Serial.println(results.value, HEX);
processButton();
irrecv.resume(); //restart for next button press
}
//************************************
//Other non blocking code goes here
//************************************
} //END of loop()
// p r o c e s s B u t t o n ( )
//********************************************^************************************************
//process IR remote button presses
void processButton()
{
//do what is required for this IR code
switch (results.value)
{
//**********************
case 0xFFA25D: //ON/OFF
{
Serial.println("ON/OFF");
digitalWrite(onOffLED, !digitalRead(onOffLED));
}
break;
//**********************
case 0xFF629D: //Vol +
{
Serial.println("Vol +");
}
break;
//**********************
case 0xFFE21D: //Func Stop
{
Serial.println("Func Stop");
}
break;
//**********************
case 0xFF22DD: //Left Arrow
{
Serial.println("Left Arrow");
}
break;
//**********************
case 0xFF02FD: //Play Pause
{
Serial.println("Play Pause");
}
break;
//**********************
case 0xFFC23D: //Right Arrow
{
Serial.println("Right Arrow");
}
break;
//**********************
case 0xFFE01F: //Down Arrow
{
Serial.println("Down Arrow");
}
break;
//**********************
case 0xFFA857: //Vol -
{
Serial.println("Vol -");
}
break;
//**********************
case 0xFF906F: //Up Arrow
{
Serial.println("Up Arrow");
}
break;
//**********************
case 0xFF6897: //0
{
Serial.println("0");
}
break;
//**********************
case 0xFF9867: //EQ
{
Serial.println("EQ");
}
break;
//**********************
case 0xFFB04F: //St Rept
{
Serial.println("St Rept");
}
break;
//**********************
case 0xFF30CF: //1
{
Serial.println("1");
}
break;
//**********************
case 0xFF18E7: //2
{
Serial.println("2");
}
break;
//**********************
case 0xFF7A85: //3
{
Serial.println("3");
}
break;
//**********************
case 0xFF10EF: //4
{
Serial.println("4");
}
break;
//**********************
case 0xFF38C7: //5
{
Serial.println("5");
}
break;
//**********************
case 0xFF5AA5: //6
{
Serial.println("6");
}
break;
//**********************
case 0xFF42BD: //7
{
Serial.println("7");
}
break;
//**********************
case 0xFF4AB5: //8
{
Serial.println("8");
}
break;
//**********************
case 0xFF52AD: //9
{
Serial.println("9");
}
break;
} // END switch case
} //END of processButton()
//********************************************^************************************************
Edit
Added some comments.
This code is different from the original code you shared. It seems simpler. I'll explore further. Thanks, LarryD.
Is the original code still up to snuff, or is this newer code your preference?
John
One more thing: I only require a momentary pulse from the Arduino to activate my circuit. I'm adding physical N.O. momentary switches to the circuit control mix as well as the Arduino.
This is just a different version.
This version adds momentary output; as an example the Vol + (case 0xFF629D: ) button makes Arduino pin D11 go HIGH for 1 second.
You should be able to see how the momentary operation is accomplished.
//https://forum.arduino.cc/t/expanding-on-larryds-ir-remote-control-code/1014754/15
//********************************************^************************************************
// XXXXXXXXXX.ino
//
//
// Version YY/MM/DD Comments
// ======= ======== ========================================================
// 1.00 22/07/21 Running code
//
//
//********************************************^************************************************
#include "IRremote.h"
//Sketch to demonstrate using an IR hand remote to control Arduino outputs.
//The remote used here uses the NEC protocol.
//This remote sends the button code (example 0xFF629D) then a repeat code 0xFFFFFFFF
//The repeat code is re-sent as long as an IR remote button is pressed.
//The IR receiver used is the TSOP4838
//********************************************^************************************************
#define LEDon HIGH
#define LEDoff LOW
#define RELAYon LOW
#define RELAYoff HIGH
#define ENABLED true
#define DISABLED false
#define PUSHED LOW
#define RELEASED HIGH
//********************************************^************************************************
const byte heartbeatLED = 13;
const byte onOffLED = 12;
const byte pin11LED = 11;
//IR receive pin
const byte RECV_PIN = 7;
boolean pin11_TimerFlag = DISABLED;
//timing stuff
unsigned long heartbeatMillis;
unsigned long pin11_Millis;
const unsigned long pin11_OnInterval = 1000ul;
//*********************************
//create instance of 'IRrecv'
IRrecv irrecv(RECV_PIN);
//create instance of 'decode_results'
decode_results results;
// s e t u p ( )
//********************************************^************************************************
//
void setup()
{
Serial.begin(115200);
//start IR receiver
irrecv.enableIRIn();
pinMode(heartbeatLED, OUTPUT);
pinMode(onOffLED, OUTPUT);
digitalWrite(onOffLED, LEDoff);
pinMode(pin11LED, OUTPUT);
digitalWrite(pin11LED, LEDoff);
} //END of setup()
// l o o p ( )
//********************************************^************************************************
//
void loop()
{
//*********************************
//is it time to toggle the heartbeatLED ?
if (millis() - heartbeatMillis >= 500)
{
//restart this TIMER
heartbeatMillis = millis();
//toggle the heartbeatLED
digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
}
//*********************************
//have we received an IR code ?
if (irrecv.decode(&results))
{
//Serial.println(results.value, HEX);
processButton();
//restart for next button press
irrecv.resume();
}
//*********************************
//when this TIMER is enabled, is it time to turn OFF the LED on pin 11?
if (pin11_TimerFlag == ENABLED && millis() - pin11_Millis >= pin11_OnInterval)
{
//this TIMER has expired, disable it for now
pin11_TimerFlag = DISABLED;
digitalWrite(pin11LED, LEDoff);
}
//************************************
//Other non blocking code goes here
//************************************
} //END of loop()
// p r o c e s s B u t t o n ( )
//********************************************^************************************************
//
void processButton()
{
//do what is required for this IR code
switch (results.value)
{
//**********************
case 0xFFA25D: //ON/OFF
{
Serial.println("ON/OFF");
digitalWrite(onOffLED, !digitalRead(onOffLED));
}
break;
//**********************
case 0xFF629D: //Vol +
{
Serial.println("Vol +");
//when we 'are not' currently timing
if (pin11_TimerFlag == DISABLED)
{
//enable the TIMER
pin11_TimerFlag = ENABLED;
//reset the TIMER
pin11_Millis = millis();
digitalWrite(pin11LED, LEDon);
}
}
break;
//**********************
case 0xFFE21D: //Func Stop
{
Serial.println("Func Stop");
}
break;
//**********************
case 0xFF22DD: //Left Arrow
{
Serial.println("Left Arrow");
}
break;
//**********************
case 0xFF02FD: //Play Pause
{
Serial.println("Play Pause");
}
break;
//**********************
case 0xFFC23D: //Right Arrow
{
Serial.println("Right Arrow");
}
break;
//**********************
case 0xFFE01F: //Down Arrow
{
Serial.println("Down Arrow");
}
break;
//**********************
case 0xFFA857: //Vol -
{
Serial.println("Vol -");
}
break;
//**********************
case 0xFF906F: //Up Arrow
{
Serial.println("Up Arrow");
}
break;
//**********************
case 0xFF6897: //0
{
Serial.println("0");
}
break;
//**********************
case 0xFF9867: //EQ
{
Serial.println("EQ");
}
break;
//**********************
case 0xFFB04F: //St Rept
{
Serial.println("St Rept");
}
break;
//**********************
case 0xFF30CF: //1
{
Serial.println("1");
}
break;
//**********************
case 0xFF18E7: //2
{
Serial.println("2");
}
break;
//**********************
case 0xFF7A85: //3
{
Serial.println("3");
}
break;
//**********************
case 0xFF10EF: //4
{
Serial.println("4");
}
break;
//**********************
case 0xFF38C7: //5
{
Serial.println("5");
}
break;
//**********************
case 0xFF5AA5: //6
{
Serial.println("6");
}
break;
//**********************
case 0xFF42BD: //7
{
Serial.println("7");
}
break;
//**********************
case 0xFF4AB5: //8
{
Serial.println("8");
}
break;
//**********************
case 0xFF52AD: //9
{
Serial.println("9");
}
break;
} //END switch . . . case
} //END of processButton()
//********************************************^************************************************
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
