hi all,
i am writing a sketch to read my tv remote control power key and use arduino to turn on and turn off my tv instead of using the remote. during my arduino turn on and off tv in every 10 seconds, i also want my arduino read the signal from remote control if any keys been pressed. so far the sketch be able to turn on and off tv for every 10 seconds, but the arduino can't receive the signal from remote control even though i press many different keys. Arduino only print out one same number every 10 seconds. if i disable the SendChannelUpCode(); function, it prints out the right number every time I press a key. i read some other people forum and found one useful tip to enable the irrecv.enableIRIn(); again before irrecv.resume(); function, but it still does not work for me. please see my entire code below:
any help would be appreciated !!!
#include <IRremote.h>
#define IRpin_PIN PIND
#define IRpin 2
#define MAXPULSE 65000
#define RESOLUTION 20
IRrecv irrecv(IRpin);
decode_results results;
uint16_t pulses[100][2];
uint8_t currentpulse = 0;
int IRledPin = 13;
int LedPin = 3;
void setup()
{
irrecv.enableIRIn(); // Start the receiver
pinMode(IRledPin, OUTPUT);
pinMode(LedPin, OUTPUT);
Serial.begin(9600);
Serial.println("Ready to decode remote control power key!");
digitalWrite (LedPin, HIGH);
for (int i=0; i < 100; i++) PowerKey();
delay(1000);
digitalWrite (LedPin, LOW);
}
//************************************************************************************************************************
// start main loop
//************************************************************************************************************************
void loop()
{
SendChannelUpCode(); // turn on tv
delay (10000);
SendChannelUpCode(); // turn off tv
if (irrecv.decode(&results))
{
Serial.println(results.value, DEC); // Print the Serial 'results.value'
irrecv.enableIRIn(); // Start the receiver
irrecv.resume(); // Receive the next value
}
}
//******************************************************************************************************************
// start sending 38khz signal inside the IR pulsewidth envelope function
//******************************************************************************************************************
void pulseIR(long microsecs)
{
cli();
while (microsecs > 0)
{
digitalWrite(IRledPin, HIGH);
delayMicroseconds(10);
digitalWrite(IRledPin, LOW);
delayMicroseconds(10);
microsecs -= 26;
}
sei();
}
//******************************************************************************************************************
// start IR transmitting function
//******************************************************************************************************************
void SendChannelUpCode()
{
for (int i=0; i <100; i++)
{ delayMicroseconds(pulses [i][0] * RESOLUTION);
pulseIR(pulses [i][1] * RESOLUTION);
}
}
//*****************************************************
// start reading remote control function for power key.
//*****************************************************
void PowerKey()
{
uint16_t highpulse, lowpulse;
highpulse = lowpulse = 0;
while (IRpin_PIN & (1 << IRpin))
{
highpulse++;
delayMicroseconds(RESOLUTION);
if ((highpulse >= MAXPULSE) && (currentpulse != 0))
{
printpulses();
currentpulse=0;
return;
}
}
pulses[currentpulse][0] = highpulse;
while (! (IRpin_PIN & _BV(IRpin)))
{
lowpulse++;
delayMicroseconds(RESOLUTION);
if ((lowpulse >= MAXPULSE) && (currentpulse != 0))
{
printpulses();
currentpulse=0;
return;
}
}
pulses[currentpulse][1] = lowpulse;
currentpulse++;
}