I try to write a sketch to receive the power button from my tv remote control, then I use the receiving signal to send to turn on and off my tv. My sketch is successfully receive and send to turn on and off my tv. however, when I add this function:
irrecv.enableIRIn();
to decode the different button.
then my tv can't turn on and off anymore even through I see the same signal from my oscilloscope with or without this function irrecv.enableIRIn();
only this line in my sketch cause trouble: irrecv.enableIRIn();
when I comment it out, then everything works normal.
anyone has any idea what's the problem? if you need to see my code, i'll post it later.
Here is my code. I tried to simplify my previous code a bit. Whole purpose of my sketch is to turn on and turn off tv every 10s. And I also want to decode and print any buttons being pressed. I noticed that every time I enable " irrecv.enableIRIn(); " line, then my sketch stop execute. It does not matter where I place this function " irrecv.enableIRIn(); " it always stop execute when I enable it.
any ideas would be great appreciated !!!
#include <IRremote.h>
#define IRpin_PIN PIND
#define IRpin 2
#define MAXPULSE 65000
#define RESOLUTION 20
IRrecv irrecv(IRpin);
decode_results results;
uint32_t PowerKeyCode = 0;
uint16_t pulses[100][2];
uint8_t currentpulse = 0;
int IRledPin = 13;
int LedPin = 3;
void setup()
{
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);
irrecv.enableIRIn();
}
//************************************************************************************************************************
// start main loop
//************************************************************************************************************************
void loop()
{
/* if (irrecv.decode(&results))
{
Serial.println(results.value, DEC); // Print the Serial 'results.value'
irrecv.resume(); // Receive the next value
}*/
printpulses;
SendChannelUpCode(); // turn on tv
delay (10000);
SendChannelUpCode(); // turn off tv
}
//******************************************************************************************************************
// 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++;
}
//******************************************************************************************************************
// start printing function
//******************************************************************************************************************
void printpulses(void)
{
Serial.println("\n\r\n\rReceived: \n\rOFF \tON");
for (uint8_t i = 0; i < currentpulse; i++)
{
Serial.print(pulses[i][0] * RESOLUTION, DEC);
Serial.print(" usec, ");
Serial.print(pulses[i][1] * RESOLUTION, DEC);
Serial.println(" usec");
}
}
//******************************************************************************************************************
// end printing function
//******************************************************************************************************************
I am using UNO with Atmega32. When I printpulsed through serial port and I see they all print out fine with 100 pairs. These 100 pairs turn on and off my tv successful all the time without adding this line: irrecv.enableIRIn();
whenever I enable: irrecv.enableIRIn(); it stops transmitting.
no, I did not fix the printpulses; because I see it print out through serial without problem. I am using UNO with atmega32. By the way, the printpulses just the information for me to see. I can disable this function anytime I want. My real problem with the irrecv.enableIRIn(); function. Every time I enable this function, my sketch stop running. I can tell it stops because no print data through serial port anymore.
when I disable this irrecv.enableIRIn(); function, everything works normal. it print out 100 pairs and turn on and off tv in 10 seconds apart.
here is the message I got when I compile my sketch:
Sketch uses 3,574 bytes (11%) of program storage space. Maximum is 32,256 bytes.
Global variables use 891 bytes (43%) of dynamic memory, leaving 1,157 bytes for local variables. Maximum is 2,048 bytes.
based on the compiler message, memory still available, am i right?
can you help to point out where the problem is? Thanks so much !!!