ir remote problem

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++;                                            

 }

Didn't you start a thread on this topic 5 days ago? Perhaps you should have continued in the same thread, where people were already aware of what was going on.
The original thread:- ir receiving and sending to turn on and off tv

Since you call this in setup():-

irrecv.enableIRIn(); // Start the receiver

you don't need to call it again in the loop.

In setup(), this caught my attention:-

Serial.println("Ready to decode remote control power key!");
    digitalWrite(LedPin, HIGH);
    for (int i = 0; i < 100; i++)
        PowerKey();

Are you possibly trying to send codes with your Arduino, then receive them with the same Arduino?
(Or am I misunderstanding your comment?)

hi OldSteve,

Yes, I did. but, I got only one feedback and waiting that's why I thought nobody read my old post anymore. Anyway, thanks for your advise.

Are you possibly trying to send codes with your Arduino, then receive them with the same Arduino?
(Or am I misunderstanding your comment?)

Serial.println("Ready to decode remote control power key!");
    digitalWrite(LedPin, HIGH);
    for (int i = 0; i < 100; i++)
        PowerKey();

yes, this code is used to read the remote control power key and store the raw data into memory and I use that raw data to send to turn on and off tv in the main loop. but, inside the main loop i want to monitor and reading any keys pressed by user.

irrecv.enableIRIn(); // Start the receiver

you don't need to call it again in the loop.

in fact, I only use this function in the set up (), but if I don't call it again in the main loop, I receive nothing when I press the remote control keys. When I call it again in the main loop right before the irrecv.resume();, then it keeps print out only one value every 10 seconds.

thanks again for your input and hope you can give me further suggestion !!!

I don't have much time right now, but rather than using your 'PowerKey()' function, have you tried using the method used in the 'IRrecord' example: 'void storeCode(decode_results *results)'?

Also, very noticeably, although you make calls to it, I see no definition of the 'printpulses()' function in the code you posted, so it can't be compiled.
It might help if you post all of your code, not just some of it.

I don't have much time right now, but rather than using your 'PowerKey()' function, have you tried using the method used in the 'IRrecord' example: 'void storeCode(decode_results *results)'?

No, I have not tried it yet. I will try and let you know. Thanks for your suggestion.

Also, very noticeably, although you make calls to it, I see no definition of the 'printpulses()' function in the code you posted, so it can't be compiled.
It might help if you post all of your code, not just some of it.

sorry I took the print function out because I forgot that I call the print function in my sketch. Here is the complete sketch:

#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()
                     
 {

   
    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     
    }
    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
//******************************************************************************************************************

anymore inputs would be appreciated !!!

sonh:
sorry I took the print function out because I forgot that I call the print function in my sketch.

No problem. I only asked because if you post the complete code, helpers can copy and paste it into their own IDE, then compile. That can't be done if the code is incomplete.

And let us know how this 'void storeCode(decode_results *results)' goes - it appears that it does exactly what you want to do.
Fingers crossed. :slight_smile:

sorry for slow response due to holiday.

I did try the IRrecord from (* Copyright 2009 Ken Shirriff), but it did not work out for me. Anyway, I'll try to do more study to find the bug in my code later.

if you have any other ideas, please let me know.

Thanks and Happy early Christmas Season !!!

sonh:
I did try the IRrecord from (* Copyright 2009 Ken Shirriff), but it did not work out for me.

Do you still have the code that you used with it?
Maybe it was an error in your code, rather than the library not actually working. Most of us have had no trouble with that library.

Another possibility is that the transmitting remote uses 40kHz, and your receiver is 38kHz, or vice versa.
(Just a thought.)

Happy early Christmas Season

You too.