How to know if remote control is available?

hi all
i have this code (IR remote for sonny)

int getSircs() { // function for decode the remote control button
  int duration;  
  int irCode;
  int mask;

  // wait for start bit
  do {
    duration = pulseIn(irPin, LOW);
  } while (duration < 2160 || duration > 2640);

  // get 12-bit SIRCS code
  irCode = 0;							  // clear ir code
  mask = 1;							    // set mask to bit 0
  for (int idx = 0; idx < 12; idx++) {			 // get all 12 bits
    duration = pulseIn(irPin, LOW);			    // measure the bit pulse
    if (duration >= 1080)					  // 1 bit?
	irCode |= mask;						// yes, update ir code
    mask <<= 1;							// shift mask to next bit
  }
  return irCode;
}

how i want to know if getSircs is available…
bcos i want to the loop keep running while the same time it check the remote button. if remote available it will process the other code, and when it not available it will closed…

dut: hi all i have this code (IR remote for sonny)

You could try the following:

Instead of the do{}while() loop, call pulsiIn once with a timeout value of 2640 (or whatever upper limit you want to put). If the duration is out of the range you will accept, return from getSircs with a value of -1 (or 0 or something else that will never be a valid code) so that the calling function can know that there is nothing to decode.

Now...

This means that every time you call the function there will be a 2.4 millisecond delay before it decides to give up and lets you get back to whatever else you need to be doing. Also, if you are doing something else that keeps you from calling getSircs in timely manner you could miss the pulse that marks the starts of the code and the timing will be screwed up until the next code is sent from the remote control. Seems pretty shaky to me if you intend to do something meaningful instead of just waiting for the remote code.

However...

If you use Ken Sharriff's excellent IRremote library, the receive stuff is interrupt-driven and stores transition times in an array while your program can be doing something else. You can call the IRremote decodeSony function at any time to see whether the proper number of bits has been received (it returns zero if a complete code is not in its buffer). That way you are less likely to miss anything while your program is doing other stuff.

Here's the link for information about and download of the IRremote library (and a wealth of other really important information): Ken Shirriff's Blog

Regards,

Dave

davekw7x:

dut: hi all i have this code (IR remote for sonny)

You could try the following:

Instead of the do{}while() loop, call pulsiIn once with a timeout value of 2640 (or whatever upper limit you want to put). If the duration is out of the range you will accept, return from getSircs with a value of -1 (or 0 or something else that will never be a valid code) so that the calling function can know that there is nothing to decode.

Now...

This means that every time you call the function there will be a 2.4 millisecond delay before it decides to give up and lets you get back to whatever else you need to be doing. Also, if you are doing something else that keeps you from calling getSircs in timely manner you could miss the pulse that marks the starts of the code and the timing will be screwed up until the next code is sent from the remote control. Seems pretty shaky to me if you intend to do something meaningful instead of just waiting for the remote code.

how to do that? i dont understand

here the code that i modify, but the led not light automatic, it need button on remote pressed ,
how can i repair the code,
i donno what wrong.

#define NUMVALUES 4 //set array in 4

int Position;
int position1;
int position2;
int position3;
int position4;
int get;
int irPin = 2;
int key;
int key2;
int set;
int counter = 0;
//====================position LDR=============
void setposition(){
  position1= digitalRead(9);
  position2=digitalRead(10);
  position3=digitalRead(11);
  position4= digitalRead(12);
  
  if (position1==1){
   if (position2 ==0){
       if (position3 ==0){
         if (position4 ==0){
              Position = 1;           //position on LDR 1
    Serial.print("Position");
    Serial.println(Position);
         }
       }
    }
  }
 if (position1==0){
   if (position2 ==1){
       if (position3 ==0){
         if (position4 ==0){
        Position = 2;              //position on LDR 2
    Serial.print("Position");
    Serial.println(Position);
         }
       }
    }
    
  } 
  if (position1==0){
    if(position2 ==0){
       if (position3 ==1){
         if (position4 ==0){
            Position = 3;            //position on LDR 3
    Serial.print("Position");
    Serial.println(Position);
         }
       }
    }
  }
   if (position1==0){
    if(position2 ==0){
       if (position3 ==0){
         if (position4 ==1){
            Position = 4;            //position on LDR 4
    Serial.print("Position");
    Serial.println(Position);
         }
       }
    }
  }
     
     
   if (position1 ==1){
       if (position2 ==1){
         if (position3 ==1){
          if (position4==1){
             Position = 0;     //no light detected 
    Serial.print("Position");
    Serial.println("No light detected");
          }
         }
       }
   }


   }
  //====================end position======================
  
  //===============goto remote func===========================

void gotoremote(){ //on the remote control
  key2 = getSircs(); //get for the next button press
    delay(100);
  while (key2 != 151){ //if the button '151' is not pressed yet do
    if (key2 == 146){
    set = 100;
     Serial.print("Open= ");
      Serial.println(set);
      delay(500);
   
  }
  if (key2 == 147){
    set = -100;
      Serial.print("closed= ");
      Serial.println(set);
  delay(500);
  }
   key2 = getSircs();
  }
}

//=============== End goto remote func===========================


  void setup()
{
   for (int i = 9; i<= 12; i++) 
    pinMode(i, OUTPUT);
  pinMode(13,OUTPUT);
  pinMode(irPin, INPUT); //select digital 2 as input reciever
  Serial.begin(9600);
  Serial.println("BEGIN");
  delay(1000);  // wait for 25ms
  
}

void loop()
{
  

digitalWrite(13,LOW);
       delay(500);
       int values [NUMVALUES];
       int maxVal = -1;
  
  // READ THE VALUES
  for (int i=0; i< NUMVALUES; i++)
    {
    int val = analogRead(i);
    values[i] = val;
    if (val > maxVal)
      maxVal = val;
    
    }  // end of taking readings

  // SET ALL MAX PINS TO HIGH
  for (int i = 0; i < NUMVALUES; i++)
    digitalWrite(9+i, values[i] == maxVal); 
 setposition();
 delay(1000);
 if (getSircs() == true){
   gotoremote();
 }
 
}
    

   



int getSircs() { // function for decode the remote control button
  int duration;  
  int irCode;
  int mask;

  // wait for start bit
  do {
    duration = pulseIn(irPin, LOW);
  } while (duration < 2160 || duration > 2640);

  // get 12-bit SIRCS code
  irCode = 0;							  // clear ir code
  mask = 1;							    // set mask to bit 0
  for (int idx = 0; idx < 12; idx++) {			 // get all 12 bits
    duration = pulseIn(irPin, LOW);			    // measure the bit pulse
    if (duration >= 1080)					  // 1 bit?
	irCode |= mask;						// yes, update ir code
    mask <<= 1;							// shift mask to next bit
  }
  if (irCode == 149){
    int k = 1;
    Serial.println("on");
     digitalWrite(13, HIGH);
     delay(500);
     gotoremote();
     get= getSircs();
  }
  return irCode;
}

i donno what wrong.

since you didn't post the (modified) getSircs() function, we donno either.