Go Down

Topic: Infrared Programming (Read 105 times) previous topic - next topic

Shandy

Hi all i have a question regarding some code i'm modifying to record some infrared data, it determines the amount of time the infrared signal has been high or low and i am changing it to work without the use of loops and delays, the reason i am not using a library is due to the fact that the infrared protocol is not a standard protocol and i need it tailored to my use.

Code: [Select]
  while(IR_PIN & (1 << IRPin)){ //IR signal is off.
    highpulse++;
    delayMicroseconds(20); //Add some fuzzyness.
     
      return;
    }


The code above you will find on some of the tutorials from adafruit and various others online who do not use a library while i realize the loop is in this code i thought i would tackle the delay first as i'm sure i can measure the micros() in the standard tradition so that should not be a problem but this resolution and "Fuzzyness" what is it there for i notice if i remove it my timing is off and i think it just ensures that the varying code execution times from other things you add doesn't affect the timing in a drastic manor so how should i remove it without could i simply add a certain amount to the result and offset it that way or do i need to allow for something when i use the micros function in the code.

Can anyone help me on this, are there any experts out there?

Shandy

Thought i would post on last nights escapade, i have done some research and from what I've found the delayMicroseconds() function is there because otherwise we would have to know how long each code execution took and considering the controller is running at 16 Mhz if we were to count we would not be counting in microseconds.

I have solved the blocking code issue by rewriting the code to use Micros to measure the time between the infrared LED turning on and off if you hook up a receiver to pin 2 and run the code when you transmit to it you will see the individual pulse length times with surprisingly good accuracy, See code attached below.

Code: [Select]
#define IR_PIN PIND
#define IRPin 2

unsigned long PrevMicros = 0; //store time of last pulse.
int lowPulse = 0;

void setup() {
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  Serial.println("Reciever Started..."); //Let us know its ready.

}

void loop() {
  Get_IR(); //Process any IR.

}

int Get_IR(){
  unsigned long TMicros = micros(); //Get the time.
  if(IR_PIN & (1 << IRPin)){ //If IR is off.
    if(lowPulse){ //If we recorded a pulse print it.
      Serial.println(lowPulse); //print it.
      lowPulse = 0; //Reset for next pulse.
    }
    PrevMicros = TMicros; //The time before the signal.
  }

  if(! (IR_PIN & _BV(IRPin))){ //If IR is on.
    lowPulse = TMicros - PrevMicros; //Subtract current time from time we last checked.
    //I.e. 4 o clock we checked its now 5 o clock subtract equals an hour passed.
  }
}


Yes there are still things i could change such as variable types but this is my first version next i intend to have it return the pulse length and i am going to use a pin change interrupt so that i can expand this to incorporate multiple sensors.

Go Up