IR Remote Question

Hi All!

First post here and I would like to ask a few questions as I am a total noob in the language.

I recently experimented on IR remotes and tried this..

#include <IRremote.h>
#include <IRremoteInt.h> // This library does not need to be included at this point but it seems like it might be important?
long oldValue; // This value will save the last meaningful remote input and reuse it in the case of a repeat
int RECV_PIN = 12; // The pin that recieves the IR sensor Output. The leftmost pin when looking at the eye with pins down.
int volDelay = 100; //this value can be changed to move volume for longer increments

IRrecv irrecv(RECV_PIN); // The core IR parts of the program and the libraries come from Ken Shirriff's blog: A Multi-Protocol Infrared Remote Library for the Arduino
decode_results results;


void MotorOff()
{
digitalWrite(A0,LOW); // The core code for motor control came from http://www.hifivision.com/diy/44275-...g-arduino.html
digitalWrite(A1,LOW); // this sets the functions for each mode of motor movement
digitalWrite(A2,LOW);
digitalWrite(A3,LOW);
digitalWrite(A4,LOW);
digitalWrite(A5,LOW);

digitalWrite(2,LOW);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
}

void MotorUp()
{
digitalWrite(A0,HIGH);
digitalWrite(A1,HIGH);
digitalWrite(A2,HIGH);
digitalWrite(A3,HIGH);
digitalWrite(A4,HIGH);
digitalWrite(A5,HIGH);

digitalWrite(2,LOW);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
}


void MotorDown()
{
digitalWrite(A0,LOW);
digitalWrite(A1,LOW);
digitalWrite(A2,LOW);
digitalWrite(A3,LOW);
digitalWrite(A4,LOW);
digitalWrite(A5,LOW);

digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
digitalWrite(7,HIGH);
}

void setup()
{
pinMode(A0,OUTPUT);
pinMode(A1,OUTPUT); //setting power to motor pins for 5v output
pinMode(A2,OUTPUT);
pinMode(A3,OUTPUT);
pinMode(A4,OUTPUT);
pinMode(A5,OUTPUT);

pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);

MotorOff();
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
}


void loop()
{
if (irrecv.decode(&results))
{
Serial.print(results.bits, DEC);
Serial.print(" --results.value"); //printing newly recieved IR code to serial monitor
Serial.print(results.value, HEX);
irrecv.resume(); // Receive the next IR code. This code is an interrupt, so the arduino can go on with other tasks.

if ((results.value==0x18E740BF)||(results.value==0x18E700FF)||(results.value==0x18E730CF)) //if up button pushed move motor forward with a 175ms shot of 5V
{
oldValue=results.value; //setting the placeholders value
MotorUp();
delay(volDelay); //this value can be changed to move volume in bigger increments
MotorOff();
}

else if ((results.value==0x18E7C03F)||(results.value==0x18E7807F)||(results.value==0x18E7B04F)) // down button pushed
{
oldValue =results.value;
MotorDown();
delay(volDelay);
MotorOff();
}
else if (results.value==0xFFFFFFFF) // button is held down, do the last thing done, again
{
if ((oldValue==0x18E740BF)||(oldValue==0x18E700FF)||(oldValue==0x18E730CF))
{
MotorUp();
delay(volDelay);
MotorOff();
}

else if ((oldValue==0x18E7C03F)||(oldValue==0x18E7807F)||(oldValue==0x18E7B04F))
{
MotorDown();
delay(volDelay);
MotorOff();
}
}




Serial.print(" --oldValue");
Serial.println(oldValue, HEX); // this shows the placeholder at work and starts a new line in the serial monitor

}
}

It works fine, I used an LED (with limit resistor) to monitor the output for up/down. It even responds to the remote being held down using the repeat code (0xFFFFFFFF).

However, since I am using an old tv remote, I noticed that pressing and holding down other buttons also transmits the repeat signal (0xFFFFFFFF) and so the chip reads this and executes whatever the value of oldValue is.

My question is, how do I prevent this from happening? Can I flush the value of oldValue say after a second or two? How?

I'm open to any ideas, I just want the remote to only respond to designated buttons.

Thanks for reading!

Please do not direct us to a link and say I am running this code, just attach what you are currently using (use code tags).
You could change the sketch to delete the old command after a period of time (set up a timer based on millis() see the Blink without Delay example that comes with the IDE), you could ignore the repeat code entirely.

Hi LarryD,

Thanks for the heads up. I already edited my original post, hope I got it right. :slight_smile: