| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • Stop wasting time looking for files and revisions. Connect your Gmail, DriveDropbox, and Slack accounts and in less than 2 minutes, Dokkio will automatically organize all your file attachments. Learn more and claim your free account.

View
 

Sony IR Receive not blocking

Page history last edited by camerons 10 years, 4 months ago

Here is code to receive IR for sony's 12 bit tv remote protocol without blocking.

Run this code and press buttons on the remote and it should print the integer key code

of the button you press when it receives it.

 

int ir_pin = 6;                //IR Receiver attached through a 220 ohm resistor to this pin
int led_pin = 11;           
int debug = 0;               
int start_bit = 2000;            //Start bit threshold (Microseconds)
int bin_1 = 1000;                //Binary 1 threshold (Microseconds)
int bin_0 = 400;                 //Binary 0 threshold (Microseconds)


volatile int data[12];
volatile int dataReceived=false;

void setup() {
  for(int i=0;i<12;i++)
    data[i]=0;
  pinMode(led_pin, OUTPUT);        //This shows when we're ready to recieve
  pinMode(ir_pin, INPUT);
  digitalWrite(led_pin, LOW);        //not ready yet
  Serial.begin(9600);
  attachInterrupt(ir_pin-5, rising, CHANGE);

}
long lastRise=0;
long lastFall=0;

void loop() {
 /* int key = getIRKey();            //Fetch the key
  if(key!=-2)
  {
    Serial.print("Key Recieved: ");
    Serial.println(key);
  }*/
  if(dataReceived)
  {
    int key= getIRKey();
    dataReceived=false;
    Serial.print("key:");
    Serial.println(key);
    if(debug)
    for(int i=0;i<12;i++)
      Serial.println(data[i]);
  }

 // delay(1000);
}

long IRwaitTime=3000; //microseconds
int getIRKey() {

  for(int i=0;i<11;i++) {          //Parse them
    if(data[i] > bin_1) {          //is it a 1?
    data[i] = 1;
    }  else {
    if(data[i] > bin_0) {        //is it a 0?
      data[i] = 0;
    } else {
     data[i] = 2;              //Flag the data as invalid; I don't know what it is!
    }
    }
  }

  for(int i=0;i<11;i++) {          //Pre-check data for errors
    if(data[i] > 1) {
    return -1;                 //Return -1 on invalid data
    }
  }

  int result = 0;
  int seed = 1;
  for(int i=0;i<11;i++) {          //Convert bits to integer
    if(data[i] == 1) {
    result += seed;
    }
    seed = seed * 2;
  }
  return result;                 //Return key number
}


long d=0;
int dataPos=0;

void rising()
{
  if(digitalRead(ir_pin)==LOW) //falling
    lastFall=micros();
  else //rising
  {
     d=micros()-lastFall;
    if(d>2200) //mark received
    {
      dataPos=0;
      dataReceived=false;
    }
    else if(dataPos<12) //found some data!
    {
      data[dataPos++]=d;
      if(dataPos==12)
        dataReceived=true;
    }
    
  }
 
}

 

Comments (0)

You don't have permission to comment on this page.