Hi, I want to make the numbers on my IR Remote add together to create any number I want between 0 and 999. The problem Im having is that when I press the number on my Remote, it appears a lot of times. Say I press 8, instead of getting just that, I get 888. Plus, I created an array to store each number in its own position, so that I can combine the numbers to make the final number. But the length of my array can only be 3 (I only want number 1000 and below), so when I press the numbers, it fills up my array instantly. I want to clarify im a beginner with Arduino so I don't really understand really advanced stuff. Thanks
When you press and hold a remote button, a repeat code is sent to the IR receiver.
This is why you are getting repeat numbers.
You need to not respond to the repeat code.
Always show us a good schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.
In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.
Use the < CODE / > icon from the ‘posting menu’ to attach the copied sketch.
You might consider a termination key to say the number is finished.
example:
123# for 123
56# for 056
I didn't really understand that. Here's my code so you get a good idea. It's all my code, its a bit long but I can't remove parts without it not working.
#include <IRremote.h>
#define IR_PIN 5
#define IR_BUTTON_NEXT 64
#define IR_BUTTON_PREV 68
#define IR_BUTTON_0 9
#define IR_BUTTON_1 29
#define IR_BUTTON_2 1
#define IR_BUTTON_3 2
#define IR_BUTTON_4 3
#define IR_BUTTON_5 4
#define IR_BUTTON_6 5
#define IR_BUTTON_7 6
#define IR_BUTTON_8 7
#define IR_BUTTON_9 8
String numbersPressedArray[3] = {};
int currentPositionInArray = 0;
unsigned long lastTime5SecondsPassed = millis();
unsigned long secondDelay = 5000;
unsigned long lastTimeNumberPressed = millis();
unsigned long numberPressDelay = 250;
int command;
int prevKey;
String actualNumberInString;
void getNumberInArray() {
if (command != prevKey) {
switch (command) {
case IR_BUTTON_0:
{
numbersPressedArray[currentPositionInArray] = "0";
break;
}
case IR_BUTTON_1:
{
numbersPressedArray[currentPositionInArray] = "1";
break;
}
case IR_BUTTON_2:
{
numbersPressedArray[currentPositionInArray] = "2";
break;
}
case IR_BUTTON_3:
{
numbersPressedArray[currentPositionInArray] = "3";
break;
}
case IR_BUTTON_4:
{
numbersPressedArray[currentPositionInArray] = "4";
break;
}
case IR_BUTTON_5:
{
numbersPressedArray[currentPositionInArray] = "5";
break;
}
case IR_BUTTON_6:
{
numbersPressedArray[currentPositionInArray] = "6";
break;
}
case IR_BUTTON_7:
{
numbersPressedArray[currentPositionInArray] = "7";
break;
}
case IR_BUTTON_8:
{
numbersPressedArray[currentPositionInArray] = "8";
break;
}
case IR_BUTTON_9:
{
numbersPressedArray[currentPositionInArray] = "9";
break;
}
default:
{
currentPositionInArray -= 1;
}
}
}
Serial.println(numbersPressedArray[currentPositionInArray]);
currentPositionInArray += 1;
prevKey = command;
}
int getActualNumber() {
for (int i = 0; i < 3; i++) {
actualNumberInString += numbersPressedArray[i];
}
int actualNumber = actualNumberInString.toInt();
return actualNumber;
}
void setup() {
Serial.begin(115200);
IrReceiver.begin(IR_PIN);
}
void loop() {
unsigned long timeNow = millis();
command = IrReceiver.decodedIRData.command;
if (timeNow - lastTimeNumberPressed > numberPressDelay) {
lastTimeNumberPressed = timeNow;
if (IrReceiver.decode()) {
getNumberInArray();
IrReceiver.resume();
}
}
if (timeNow - lastTime5SecondsPassed > secondDelay) {
lastTime5SecondsPassed += secondDelay;
Serial.println(getActualNumber());
Serial.println("---------------------");
for (int i = 0; i < 3; i++) {
numbersPressedArray[i] = " ";
currentPositionInArray = 0;
actualNumberInString = "";
}
}
}
Also im using Version 3 of the IR remote library.
#define IR_BUTTON_9 8
Are you saying when an 8 is received, the #9 remote button was pushed ?
What remote do you have ?
To enter 123 the user presses 1 then 2 then 3 then the # key on the remote.
Yes, I was using an Arduino starter kit remote but it was very cheap so the buttons wouldn't work very well, so I decided to just use my TV remote. The value for the 9 button is 8, the value of the 7 button is 6, the value for the 6 button is 5 and so on. I switched the value that gives 1 to the "." button because I was getting some weird bugs Here's a picture of my remote:
And I don't know If it makes a difference but im using this IR Receiver.
Also, I understood that # part. It will be useful thanks.
Do you know if this remote follows the NEC protocol ?
Not really sure what the is haha, sorry. Im just a newbie to Arduino and electronics in general.
Once you have this 3 digit number, what are you wanting to do with it ?
Please explain why you decided to use a millis( ) based TIMER ?
I actually want to integrate into a larger project. Setting and LEDS strip brightness with the IR remote. So say I want 255, I click 2 5 5 on my remote. The problem is that if I write "255" it ends up as 222 or 552 or some other weird combination. Im not quite sure if the problem is my remote or my program.
I was using millis() to make it so that every 5 seconds, a new number is put into the brightness, but with the # tip you gave me, im planning to remove that and add the # to enter the 3 digits.
Im gonna try that out, maybe it's the problem.
Try this simple code example; please confirm the button commands and digits match with your remote ?
#include <IRremote.h>
#define IR_PIN 5
#define IR_BUTTON_NEXT 64
#define IR_BUTTON_PREV 68
const byte myButtonCode[] = {82, 22, 25, 13, 12, 24, 94, 8, 28, 90};
const byte buttonCode[] = {9, 29, 1, 2, 3, 4, 5, 6, 7, 8};
const byte digit[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
// s e t u p ( )
//******************************************************************************************
void setup()
{
Serial.begin(115200);
IrReceiver.begin(IR_PIN);
}//END of setup()
// l o o p ( )
//******************************************************************************************
void loop()
{
//*******************************************************
if (IrReceiver.decode())
{
//Enable receiving of the next value
IrReceiver.resume();
}
//*******************************************************
//have we received a new IR code form the remote ?
if (IrReceiver.decodedIRData.command != 0)
{
checkIRcode();
}
} //END of loop()
// c h e c k I R c o d e ( )
//******************************************************************************************
void checkIRcode()
{
Serial.print("Command = ");
//command
Serial.print(IrReceiver.decodedIRData.command);
for (byte x = 0; x < 10; x++)
{
if (IrReceiver.decodedIRData.command == buttonCode[x])
{
Serial.print(" Button = ");
Serial.print(digit[x]);
break;
}
}
Serial.println("");
//needed to block repeat codes
IrReceiver.decodedIRData.command = 0;
} //END of checkIRcode()
After checking out the above sketch, try this version.
You will need to change the following line of code to meet your needs:
const byte hash = 74; // # <-----<<<<< use a button and it's code from your remote
#include <IRremote.h>
#define IR_PIN 5
#define IR_BUTTON_NEXT 64
#define IR_BUTTON_PREV 68
const byte myButtonCode[] = {82, 22, 25, 13, 12, 24, 94, 8, 28, 90};
const byte buttonCode[] = { 9, 29, 1, 2, 3, 4, 5, 6, 7, 8};
const byte digit[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
const byte hash = 74; // # <-----<<<<< use a button and it's code from your remote
unsigned int myNumber;
// s e t u p ( )
//******************************************************************************************
void setup()
{
Serial.begin(115200);
IrReceiver.begin(IR_PIN);
}//END of setup()
// l o o p ( )
//******************************************************************************************
void loop()
{
//*******************************************************
if (IrReceiver.decode())
{
//Enable receiving of the next value
IrReceiver.resume();
}
//*******************************************************
//have we received a new IR code form the remote ?
if (IrReceiver.decodedIRData.command != 0)
{
checkIRcode();
}
} //END of loop()
// c h e c k I R c o d e ( )
//******************************************************************************************
void checkIRcode()
{
//**************************************
for (byte x = 0; x < 10; x++)
{
//***************
if (IrReceiver.decodedIRData.command == buttonCode[x])
{
Serial.print("Button = ");
Serial.print(digit[x]);
myNumber = myNumber * 10 + digit[x];
break;
}
}
Serial.println("");
//***************
if (IrReceiver.decodedIRData.command == hash)
{
Serial.print("My number = ");
Serial.println(myNumber);
Serial.println("");
myNumber = 0;
}
//needed to block repeat codes
IrReceiver.decodedIRData.command = 0;
} //END of checkIRcode()
Edited the code
It is good to know the protocol because, for example, the SONY protocol every time a button is pressed sends its code 3 times.
Alright I tried it and used button 3 for hash. But It still repeats 2 or three times. Take a look.
clicked on these buttons only one time, yet it appears as though I clicked multiple times.
ohhh that makes sense, but still, sometimes it doesn't repeat 3 times, sometimes It repeats it twice, sometimes it doesn't repeat it at all. What I think I need is just like a simple delay after each click, so it forgets about the other repetitions. Im not quite sure how to to do it though.
Hey, do you mean change the 74 to one of my buttons?
Try using a button that is not from 0 to 9 as the terminator button.
Try the decimal “ . “
Always attach your modified code, not pictures.
Some remotes send a key more than once, if yours does, you will need to to ignore the extra received numbers.
okay so I finally undertstand. I just need to change the code, so instead of multiplying my digit by 10, I do whatever I want to do. Let me try it out and get back to you.
If you use “ . “ as the terminator, to enter 123 the user presses 1 then 2 then 3 then the “ . “ key on the remote.
Okay so I got the "." thing to work. But Im still getting repeated numbers. Take a look.
#include <IRremote.h>
#define IR_PIN 5
#define IR_BUTTON_NEXT 64
#define IR_BUTTON_PREV 68
const byte myButtonCode[] = { 82, 22, 25, 13, 12, 24, 94, 8, 28, 90 };
const byte buttonCode[] = { 9, 0, 1, 2, 3, 4, 5, 6, 7, 8 };
const byte digit[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
const String buttonCodeInString[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
const byte hash = 29; // # <-----<<<<< use a button and it's code from your remote
String myStringNumber;
unsigned int myNumber;
// s e t u p ( )
//******************************************************************************************
void setup() {
Serial.begin(115200);
IrReceiver.begin(IR_PIN);
} //END of setup()
// l o o p ( )
//******************************************************************************************
void loop() {
//*******************************************************
if (IrReceiver.decode()) {
//Enable receiving of the next value
IrReceiver.resume();
}
//*******************************************************
//have we received a new IR code form the remote ?
if (IrReceiver.decodedIRData.command != 0) {
checkIRcode();
}
} //END of loop()
// c h e c k I R c o d e ( )
//******************************************************************************************
void checkIRcode() {
//**************************************
for (byte x = 0; x < 10; x++) {
//***************
if (IrReceiver.decodedIRData.command == buttonCode[x]) {
Serial.print("Button = ");
Serial.print(digit[x]);
myStringNumber += buttonCodeInString[x];
break;
}
}
myNumber = myStringNumber.toInt();
Serial.println("");
//***************
if (IrReceiver.decodedIRData.command == hash) {
if (myNumber != 0) {
Serial.print("My number = ");
Serial.println(myNumber);
Serial.println("");
myNumber = 0;
myStringNumber = "";
}
}
//needed to block repeat codes
IrReceiver.decodedIRData.command = 0;
} //END of checkIRcode()
Here's a pick of my Results
Finally got it to Work. Changed my TV remote for my cheap remote and it's working great. Thank you so much. Here's my finished code in case anyone wants to use it. Thank you so much, really.
#include <IRremote.h>
#define IR_PIN 5
#define IR_BUTTON_NEXT 64
#define IR_BUTTON_PREV 68
const byte buttonCode[] = { 74, 22, 12, 24, 94, 8, 28, 90, 66, 82 };
const byte digit[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
const String buttonCodeInString[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
const byte hash = 64; // # <-----<<<<< use a button and it's code from your remote
String myStringNumber;
unsigned int myNumber;
// s e t u p ( )
//******************************************************************************************
void setup() {
Serial.begin(115200);
IrReceiver.begin(IR_PIN);
} //END of setup()
// l o o p ( )
//******************************************************************************************
void loop() {
//*******************************************************
if (IrReceiver.decode()) {
//Enable receiving of the next value
IrReceiver.resume();
}
//*******************************************************
//have we received a new IR code form the remote ?
if (IrReceiver.decodedIRData.command != 0) {
checkIRcode();
}
} //END of loop()
// c h e c k I R c o d e ( )
//******************************************************************************************
void checkIRcode() {
//**************************************
for (byte x = 0; x < 10; x++) {
//***************
if (IrReceiver.decodedIRData.command == buttonCode[x]) {
Serial.print("Button = ");
Serial.print(digit[x]);
myStringNumber += buttonCodeInString[x];
break;
}
}
myNumber = myStringNumber.toInt();
Serial.println("");
//***************
if (IrReceiver.decodedIRData.command == hash) {
if (myNumber != 0) {
Serial.print("My number = ");
Serial.println(myNumber);
Serial.println("");
myNumber = 0;
myStringNumber = "";
}
}
//needed to block repeat codes
IrReceiver.decodedIRData.command = 0;
} //END of checkIRcode()




