ArduinoGeneral

IR Kit For Arduino Model: DFR0107

userHead anonymous 2010-12-01 01:21:58 23616 Views19 Replies
Is there any sketch for decoding the button presses on the IR Kit For Arduino Model: DFR0107? It works great, I've been able to get the timing from this program http://www.arduino.cc/playground/Code/InfraredReceivers
and from that I got this idea of what the buttons POWER, 0, 1,2,3,4 do and what the difference in signal is between them [img]
has anyone developed a code to read this?
2011-01-11 17:52:37 Correct me if I'm wrong, but your code uses the PulseIn function which will require a constant polling of the IR receiver, while the IRemote library uses interrupts and keeps the chip free to do other tasks in the mean time.

I wrote some routines for the Attiny2313 chip for this kit using interrupts and they worked really well, though they would conflict with PWM and other timer-based libraries for Arduino. I think the IRemote library code can be adapted / tuned to read the NEC protocol more reliably.

peterstrobl, should we try to join our efforts?
userHeadPic fj604
2011-01-11 17:52:37 Correct me if I'm wrong, but your code uses the PulseIn function which will require a constant polling of the IR receiver, while the IRemote library uses interrupts and keeps the chip free to do other tasks in the mean time.

I wrote some routines for the Attiny2313 chip for this kit using interrupts and they worked really well, though they would conflict with PWM and other timer-based libraries for Arduino. I think the IRemote library code can be adapted / tuned to read the NEC protocol more reliably.

peterstrobl, should we try to join our efforts?
userHeadPic fj604
2011-01-09 17:44:30 Thanks fj604 your code with the library worked, but like you said, the Sony remote works much better with it. I modified this program now to use the full 32 bits using unsigned longs. This gives you the control you need to make this remote work good from a distance. This program allows the user to set different bit thresholds needed for this remote. The bit start threshold is extra long for some reason. But now you can use it from across the room and it also gives less errors. Try it and you'll notice a big increase in distance and less errors. Also, if a key is undefined, the program returns the number of the key so you don't need to look up the number.

Code: Select all

/* Peter Strobl Test Electronics Jan 9th 2011
   changed the original program to use the full 32 Bits from the
   IR Kit For Arduino Model: DFR0107 32 bit controller. 
   All keypresses are sent to the serial monitor at 9600 baud.
   pulseIn is always HIGH. The phototransistor in the kit does not invert the signal.
   uses pin 13 for heartbeat debug
   32 bits requires an unsigned long variable.  
*/

#define IR_BIT_LENGTH 32    // number of bits sent by IR remote
#define BIT_1 1300          // Binary 1 threshold (Microseconds)
#define BIT_0 300           // Binary 0 threshold (Microseconds)
#define BIT_START 3000      // Start bit threshold (Microseconds)

#define IR_PIN 10            // IR Sensor pin
#define LED_PIN 13          // LED goes off when signal is received

int debug = 0;              // flag as 1 to output raw IR pulse data stream length in microseconds
int output_verify = 0;      // flag as 1 to print decoded verification integers. same number for all buttons
int output_key = 0;         // flag as 1 to print decoded key integers

void setup() {
  pinMode(LED_PIN, OUTPUT);	//This shows when ready to recieve
  pinMode(IR_PIN, INPUT);
  digitalWrite(LED_PIN, LOW);
  Serial.begin(9600);
}

void loop() {
  digitalWrite(LED_PIN, HIGH);
  unsigned long key = get_ir_key();
  
  digitalWrite(LED_PIN, LOW);  // turn LED off while processing response
  do_response(key);
  delay(130);                  // 2 cycle delay to cancel duplicate keypresses
}

/*
  wait for a keypress from the IR remote, and return the
  integer mapping of that key (e.g. power button on remote returns 
  the unsigned long 4278238976)
*/

unsigned long get_ir_key() 
{
  int pulse[IR_BIT_LENGTH];
  int bits[IR_BIT_LENGTH];

  do {} //Wait for a start bit
  while(pulseIn(IR_PIN, HIGH) < BIT_START);

  read_pulse(pulse);
  pulse_to_bits(pulse, bits);
  return bits_to_int(bits);
}


/*
  use pulseIn to receive IR pulses from the remote.
  Record the length of these pulses (in ms) in an array
*/

void read_pulse(int pulse[])
{
  for (int i = 0; i < IR_BIT_LENGTH; i++)
  {
    pulse[i] = pulseIn(IR_PIN, HIGH, 10000);
  }
}

/*
  IR pulses encode binary "0" as a short pulse, and binary "1"
  as a long pulse.  Given an array containing pulse lengths,
  convert this to an array containing binary values
*/

void pulse_to_bits(int pulse[], int bits[])
{
  if (debug) { Serial.println("-----"); }
  for(int i = 0; i < IR_BIT_LENGTH; i++) 
  {
    if (debug) { Serial.println(pulse[i]); }
    if(pulse[i] > BIT_1) //is it a 1?
    {
      bits[i] = 1;
    }  
    else if(pulse[i] > BIT_0) //is it a 0?
    {
      bits[i] = 0;
    } 
    else //data is invalid...
    {
      Serial.println("Error");
    }
  }
}

/*
  convert an array of binary values to a single base-10 integer
*/

unsigned long bits_to_int(int bits[])
{
  unsigned long result = 0;
  unsigned long seed = 1;
  
  //Convert bits to integer
  for(int i = 0 ; i < IR_BIT_LENGTH ; i++) 
  {		  
    if(bits[i] == 1) 
    {
	result += seed;
    }   
    seed *= 2;
  }
  return result;
}


/* 
  respond to specific remote-control keys with different behaviors
*/

void do_response(unsigned long key)
{  
  
  if (output_key)
   {
      Serial.print("Key ");
      Serial.println(key);
   }
  
  switch (key)
  {
    case 4278238976:  // turns on UUT power
      Serial.println("POWER");
      break;

    case 4244815616:  // FUNC/STOP turns off UUT power
      Serial.println("FUNC/STOP");
      break;

    case 4211392256:  // |<< ReTest failed Test
      Serial.println("|<<");
      break;

    case 4194680576:  // >|| Test
      Serial.println(">||");
      break;

    case 4177968896:  // >>| perform selected test number
      Serial.println(">>|");
      break;

    case 4261527296:  // VOL+ turns on individual test beeper
      Serial.println("VOL+");
      break;

    case 4127833856:  // VOL- turns off individual test beeper
      Serial.println("VOL-");
      break;

    case 4144545536:  // v scroll down tests
      Serial.println("v");
      break;

    case 4111122176:  // ^ scroll up tests
      Serial.println("^");
      break;

    case 4060987136:  // EQ negative tests internal setup
      Serial.println("EQ");
      break;

    case 4044275456:  // ST/REPT Positive tests Select Test and Repeat Test
    Serial.println("ST/REPT");
      break;

    case 4077698816:  // 0
      Serial.println("0");
      break;

    case 4010852096:  // 1
      Serial.println("1");
      break;

    case 3994140416:  // 2
      Serial.println("2");
      break;

    case 3977428736:  // 3
      Serial.println("3");
      break;

    case 3944005376:  // 4
      Serial.println("4");
      break;

    case 3927293696:  // 5
      Serial.println("5");
      break;

    case 3910582016:  // 6
      Serial.println("6");
      break;

    case 3877158656:  // 7
      Serial.println("7");
      break;

    case 3860446976:  // 8
      Serial.println("8");
      break;

    case 3843735296:  // 9
      Serial.println("9");
      break;
     
    default:
      {
        Serial.print("Key ");
        Serial.print(key);
        Serial.println(" not programmed");
      }
    break;
  }
}



userHeadPic anonymous
2011-01-09 17:44:30 Thanks fj604 your code with the library worked, but like you said, the Sony remote works much better with it. I modified this program now to use the full 32 bits using unsigned longs. This gives you the control you need to make this remote work good from a distance. This program allows the user to set different bit thresholds needed for this remote. The bit start threshold is extra long for some reason. But now you can use it from across the room and it also gives less errors. Try it and you'll notice a big increase in distance and less errors. Also, if a key is undefined, the program returns the number of the key so you don't need to look up the number.

Code: Select all

/* Peter Strobl Test Electronics Jan 9th 2011
   changed the original program to use the full 32 Bits from the
   IR Kit For Arduino Model: DFR0107 32 bit controller. 
   All keypresses are sent to the serial monitor at 9600 baud.
   pulseIn is always HIGH. The phototransistor in the kit does not invert the signal.
   uses pin 13 for heartbeat debug
   32 bits requires an unsigned long variable.  
*/

#define IR_BIT_LENGTH 32    // number of bits sent by IR remote
#define BIT_1 1300          // Binary 1 threshold (Microseconds)
#define BIT_0 300           // Binary 0 threshold (Microseconds)
#define BIT_START 3000      // Start bit threshold (Microseconds)

#define IR_PIN 10            // IR Sensor pin
#define LED_PIN 13          // LED goes off when signal is received

int debug = 0;              // flag as 1 to output raw IR pulse data stream length in microseconds
int output_verify = 0;      // flag as 1 to print decoded verification integers. same number for all buttons
int output_key = 0;         // flag as 1 to print decoded key integers

void setup() {
  pinMode(LED_PIN, OUTPUT);	//This shows when ready to recieve
  pinMode(IR_PIN, INPUT);
  digitalWrite(LED_PIN, LOW);
  Serial.begin(9600);
}

void loop() {
  digitalWrite(LED_PIN, HIGH);
  unsigned long key = get_ir_key();
  
  digitalWrite(LED_PIN, LOW);  // turn LED off while processing response
  do_response(key);
  delay(130);                  // 2 cycle delay to cancel duplicate keypresses
}

/*
  wait for a keypress from the IR remote, and return the
  integer mapping of that key (e.g. power button on remote returns 
  the unsigned long 4278238976)
*/

unsigned long get_ir_key() 
{
  int pulse[IR_BIT_LENGTH];
  int bits[IR_BIT_LENGTH];

  do {} //Wait for a start bit
  while(pulseIn(IR_PIN, HIGH) < BIT_START);

  read_pulse(pulse);
  pulse_to_bits(pulse, bits);
  return bits_to_int(bits);
}


/*
  use pulseIn to receive IR pulses from the remote.
  Record the length of these pulses (in ms) in an array
*/

void read_pulse(int pulse[])
{
  for (int i = 0; i < IR_BIT_LENGTH; i++)
  {
    pulse[i] = pulseIn(IR_PIN, HIGH, 10000);
  }
}

/*
  IR pulses encode binary "0" as a short pulse, and binary "1"
  as a long pulse.  Given an array containing pulse lengths,
  convert this to an array containing binary values
*/

void pulse_to_bits(int pulse[], int bits[])
{
  if (debug) { Serial.println("-----"); }
  for(int i = 0; i < IR_BIT_LENGTH; i++) 
  {
    if (debug) { Serial.println(pulse[i]); }
    if(pulse[i] > BIT_1) //is it a 1?
    {
      bits[i] = 1;
    }  
    else if(pulse[i] > BIT_0) //is it a 0?
    {
      bits[i] = 0;
    } 
    else //data is invalid...
    {
      Serial.println("Error");
    }
  }
}

/*
  convert an array of binary values to a single base-10 integer
*/

unsigned long bits_to_int(int bits[])
{
  unsigned long result = 0;
  unsigned long seed = 1;
  
  //Convert bits to integer
  for(int i = 0 ; i < IR_BIT_LENGTH ; i++) 
  {		  
    if(bits[i] == 1) 
    {
	result += seed;
    }   
    seed *= 2;
  }
  return result;
}


/* 
  respond to specific remote-control keys with different behaviors
*/

void do_response(unsigned long key)
{  
  
  if (output_key)
   {
      Serial.print("Key ");
      Serial.println(key);
   }
  
  switch (key)
  {
    case 4278238976:  // turns on UUT power
      Serial.println("POWER");
      break;

    case 4244815616:  // FUNC/STOP turns off UUT power
      Serial.println("FUNC/STOP");
      break;

    case 4211392256:  // |<< ReTest failed Test
      Serial.println("|<<");
      break;

    case 4194680576:  // >|| Test
      Serial.println(">||");
      break;

    case 4177968896:  // >>| perform selected test number
      Serial.println(">>|");
      break;

    case 4261527296:  // VOL+ turns on individual test beeper
      Serial.println("VOL+");
      break;

    case 4127833856:  // VOL- turns off individual test beeper
      Serial.println("VOL-");
      break;

    case 4144545536:  // v scroll down tests
      Serial.println("v");
      break;

    case 4111122176:  // ^ scroll up tests
      Serial.println("^");
      break;

    case 4060987136:  // EQ negative tests internal setup
      Serial.println("EQ");
      break;

    case 4044275456:  // ST/REPT Positive tests Select Test and Repeat Test
    Serial.println("ST/REPT");
      break;

    case 4077698816:  // 0
      Serial.println("0");
      break;

    case 4010852096:  // 1
      Serial.println("1");
      break;

    case 3994140416:  // 2
      Serial.println("2");
      break;

    case 3977428736:  // 3
      Serial.println("3");
      break;

    case 3944005376:  // 4
      Serial.println("4");
      break;

    case 3927293696:  // 5
      Serial.println("5");
      break;

    case 3910582016:  // 6
      Serial.println("6");
      break;

    case 3877158656:  // 7
      Serial.println("7");
      break;

    case 3860446976:  // 8
      Serial.println("8");
      break;

    case 3843735296:  // 9
      Serial.println("9");
      break;
     
    default:
      {
        Serial.print("Key ");
        Serial.print(key);
        Serial.println(" not programmed");
      }
    break;
  }
}



userHeadPic anonymous
2010-12-12 01:03:24 I do not see any problem with the IRemote library when used with this kit, though I found much more sensitive to a Sony remote than to the included one that appears to use the NEC protocol.

This sketch detects buttons 0-9 and writes them to Serial, and also beeps the optional buzzer attached to pin 10.

See the code[] array for 32-bit button HEX codes from 0 to 9, you can find out the rest with the IRemote library.
Code: Select all
#include <IRremote.h>

int RECV_PIN = 2;

IRrecv irrecv(RECV_PIN);

long code[]=
{
  0xFD30CF,
  0xFD08F7,
  0xFD8877,
  0xFD48B7,
  0xFD28D7,
  0xFDA857,
  0xFD6897,
  0xFD18E7,
  0xFD9867,
  0xFD58A7,
  0x0
};

char buttoncode[]=
{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

decode_results results;

char button(long keycode)
{
  int i=0;
  do
  {
    if (code[i]==keycode)
      return buttoncode[i];
  }
  while (code[i++]);
  return 0;
}


void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);
}

void loop()
{
  char bpressed;
  if (irrecv.decode(&results))
  {
    if (bpressed=button(results.value))
      {
        Serial.write(bpressed);
        digitalWrite(10,LOW);
        delay(100);
        digitalWrite(10,HIGH);
      } 
    irrecv.resume();
  }
}

userHeadPic fj604
2010-12-12 01:03:24 I do not see any problem with the IRemote library when used with this kit, though I found much more sensitive to a Sony remote than to the included one that appears to use the NEC protocol.

This sketch detects buttons 0-9 and writes them to Serial, and also beeps the optional buzzer attached to pin 10.

See the code[] array for 32-bit button HEX codes from 0 to 9, you can find out the rest with the IRemote library.
Code: Select all
#include <IRremote.h>

int RECV_PIN = 2;

IRrecv irrecv(RECV_PIN);

long code[]=
{
  0xFD30CF,
  0xFD08F7,
  0xFD8877,
  0xFD48B7,
  0xFD28D7,
  0xFDA857,
  0xFD6897,
  0xFD18E7,
  0xFD9867,
  0xFD58A7,
  0x0
};

char buttoncode[]=
{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

decode_results results;

char button(long keycode)
{
  int i=0;
  do
  {
    if (code[i]==keycode)
      return buttoncode[i];
  }
  while (code[i++]);
  return 0;
}


void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);
}

void loop()
{
  char bpressed;
  if (irrecv.decode(&results))
  {
    if (bpressed=button(results.value))
      {
        Serial.write(bpressed);
        digitalWrite(10,LOW);
        delay(100);
        digitalWrite(10,HIGH);
      } 
    irrecv.resume();
  }
}

userHeadPic fj604
2010-12-09 13:59:48 The remote control really works well, there is no problem with the long 32 bit chain. it's actually very impressive that this little remote puts all that out. As for needing the bigger processor, you don't. I simply ignore the first 17 bits and take the last 15. That is plenty of information to know if I received a valid code or not. I just developed some code to make it work with the keyboard on your LCD Keypad Shield or the ADK Keyboard. It basically scans the remote each time it scans through the keys if no key is depressed. I will post it as soon as I clean it up. Please build more of these remote control kits. They work great, I ordered 20 of them for my work. userHeadPic anonymous
2010-12-09 13:59:48 The remote control really works well, there is no problem with the long 32 bit chain. it's actually very impressive that this little remote puts all that out. As for needing the bigger processor, you don't. I simply ignore the first 17 bits and take the last 15. That is plenty of information to know if I received a valid code or not. I just developed some code to make it work with the keyboard on your LCD Keypad Shield or the ADK Keyboard. It basically scans the remote each time it scans through the keys if no key is depressed. I will post it as soon as I clean it up. Please build more of these remote control kits. They work great, I ordered 20 of them for my work. userHeadPic anonymous
2010-12-02 20:53:08 First of all, thanks for the code and your effect to make it so simple.

Secondly, the remoter is not able to be customized, as all codes all burned in the chip which is not programmable which make it so cheap.  We can only hope that Atmel will make big memory to overcome this problem.  For example, Arduino Mega 2560 should have enough memory to store everything.
userHeadPic R2D2C3PO
2010-12-02 20:53:08 First of all, thanks for the code and your effect to make it so simple.

Secondly, the remoter is not able to be customized, as all codes all burned in the chip which is not programmable which make it so cheap.  We can only hope that Atmel will make big memory to overcome this problem.  For example, Arduino Mega 2560 should have enough memory to store everything.
userHeadPic R2D2C3PO
2010-12-02 10:03:59 Is it possible to customize the first few bits that each IR controller outputs? I know that is probably too much to ask for a $7 remote controller kit, but it seems like it may be possible because these controllers output 32 bits which is overkill for only 21 buttons. 32 bits could control 4 billion buttons, so I wonder if the first few bits aren't there for identifying the remote. Of course only the last few bits of the 32 bit string change with each button press. I'm thinking there may be some secret technique, like pressing two buttons and holding them for a few seconds then pressing a new code.

I have an application where two people are sitting side by side on a long bench controlling Arduino based circuit board test fixtures with this remote control. I need to make it so that one person’s remote control doesn’t unintentionally operate the other persons tester.
userHeadPic anonymous
2010-12-02 10:03:59 Is it possible to customize the first few bits that each IR controller outputs? I know that is probably too much to ask for a $7 remote controller kit, but it seems like it may be possible because these controllers output 32 bits which is overkill for only 21 buttons. 32 bits could control 4 billion buttons, so I wonder if the first few bits aren't there for identifying the remote. Of course only the last few bits of the 32 bit string change with each button press. I'm thinking there may be some secret technique, like pressing two buttons and holding them for a few seconds then pressing a new code.

I have an application where two people are sitting side by side on a long bench controlling Arduino based circuit board test fixtures with this remote control. I need to make it so that one person’s remote control doesn’t unintentionally operate the other persons tester.
userHeadPic anonymous
2010-12-02 09:45:58 Thanks for the nice big library, but it is not tailored for the DFR0107.
I tailored this code for the DFR0107. The code was originally based on a very well written remote controller code I found for a sony controller. I did my best to keep that nice simple format. The main change was to divide up the 32 bits and instead of using memory consuming long integers, I divided down the bitlength of 32 bits to two 15 bit codes. I just ignored the middle two bits. This code all runs from one sketch. no need for header files etc. All it does is identify the key press and output the keypress to the serial monitor.  This keeps the 21 switch case routines as a simple foundation to build on so you can insert the program you need for each button.

Code: Select all
// 0.1 by pmalmsten http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1176098434
// 0.2 by farkinga
// 0.3 by farkinga - adds cool behaviors
/* 0.4 by pstrobl 
   changed the original program to use on IR Kit For Arduino Model: DFR0107 32 bit controller. 
   All keypresses are sent to the serial monitor at 9600 baud.
   pulseIn is always HIGH. The phototransistor in the kit does not invert the signal.
   uses pin 13 for heartbeat debug
   32 bits requires a long variable, so divided up into two 15 bit so can use integer variables
   use the first 15 bits of the 32 bits for remote and data stream verification. This code is always the same for every button press
   use the last 15 of the 32 bits for button press selection. This code changes for each button.
   ignore the middle 2 bits, it never changes.  
*/

#define IR_BIT_LENGTH 32    // number of bits sent by IR remote
#define FirstLastBit 15     // divide 32 bits into two 15 bit chunks for integer variables. Ignore center two bits. they are all the same.
#define BIT_1 1500          // Binary 1 threshold (Microseconds)
#define BIT_0 450           // Binary 0 threshold (Microseconds)
#define BIT_START 4000      // Start bit threshold (Microseconds)

#define IR_PIN 2            // IR Sensor pin
#define LED_PIN 13          // LED goes off when signal is received

int debug = 0;              // flag as 1 to output raw IR pulse data stream length in microseconds
int output_verify = 0;      // flag as 1 to print decoded verification integers. same number for all buttons
int output_key = 0;         // flag as 1 to print decoded key integers
int remote_verify = 16128;  // verifies first bits are 11111100000000 different remotes may have different start codes

void setup() {
  pinMode(LED_PIN, OUTPUT);	//This shows when ready to recieve
  pinMode(IR_PIN, INPUT);
  digitalWrite(LED_PIN, LOW);
  Serial.begin(9600);
}

void loop() {
  digitalWrite(LED_PIN, HIGH);
  int key = get_ir_key();
  
  digitalWrite(LED_PIN, LOW);  // turn LED off while processing response
  do_response(key);
  delay(130);                  // 2 cycle delay to cancel duplicate keypresses
}

/*
  wait for a keypress from the IR remote, and return the
  integer mapping of that key (e.g. power button on remote returns 
  the integer 1429)
*/

int get_ir_key() 
{
  int pulse[IR_BIT_LENGTH];
  int bits[IR_BIT_LENGTH];

  do {} //Wait for a start bit
  while(pulseIn(IR_PIN, HIGH) < BIT_START);

  read_pulse(pulse);
  pulse_to_bits(pulse, bits);
  RemoteVerify(bits);
  return bits_to_int(bits);
}


/*
  use pulseIn to receive IR pulses from the remote.
  Record the length of these pulses (in ms) in an array
*/

void read_pulse(int pulse[])
{
  for (int i = 0; i < IR_BIT_LENGTH; i++)
  {
    pulse[i] = pulseIn(IR_PIN, HIGH);
  }
}

/*
  IR pulses encode binary "0" as a short pulse, and binary "1"
  as a long pulse.  Given an array containing pulse lengths,
  convert this to an array containing binary values
*/

void pulse_to_bits(int pulse[], int bits[])
{
  if (debug) { Serial.println("-----"); }
  for(int i = 0; i < IR_BIT_LENGTH; i++) 
  {
    if (debug) { Serial.println(pulse[i]); }
    if(pulse[i] > BIT_1) //is it a 1?
    {
      bits[i] = 1;
    }  
    else if(pulse[i] > BIT_0) //is it a 0?
    {
      bits[i] = 0;
    } 
    else //data is invalid...
    {
      Serial.println("Error");
    }
  }
}

/*
  check returns proper first 14 check bits
*/

void RemoteVerify(int bits[])
{
  int result = 0;
  int seed = 1;
  
  //Convert bits to integer
  for(int i = 0 ; i < (FirstLastBit) ; i++) 
  {		  
    if(bits[i] == 1) 
    {
	result += seed;
    }
    
    seed *= 2;
  }
        if (output_verify)
      {
        Serial.print("Remote ");
        Serial.print(result);
        Serial.println(" verification code");
      }
 if (remote_verify != result) {delay (60); get_ir_key();} //verify first group of bits. delay for data stream to end, then try again.
}


/*
  convert an array of binary values to a single base-10 integer
*/

int bits_to_int(int bits[])
{
  int result = 0;
  int seed = 1;
  
  //Convert bits to integer
  for(int i = (IR_BIT_LENGTH-FirstLastBit) ; i < IR_BIT_LENGTH ; i++) 
  {		  
    if(bits[i] == 1) 
    {
	result += seed;
    }   
    seed *= 2;
  }
  return result;
}


/* 
  respond to specific remote-control keys with different behaviors
*/

void do_response(int key)
{  
  
  if (output_key)
   {
      Serial.print("Key ");
      Serial.println(key);
   }
  
  switch (key)
  {
    case 32640:  // turns on UUT power
      Serial.println("POWER");
      break;

    case 32385:  // FUNC/STOP turns off UUT power
      Serial.println("FUNC/STOP");
      break;

    case 32130:  // |<< ReTest failed Test
      Serial.println("|<<");
      break;

    case 32002:  // >|| Test
      Serial.println(">||");
      break;

    case 31875:  // >>| perform selected test number
      Serial.println(">>|");
      break;

    case 32512:  // VOL+ turns on individual test beeper
      Serial.println("VOL+");
      break;

    case 31492:  // VOL- turns off individual test beeper
      Serial.println("VOL-");
      break;

    case 31620:  // v scroll down tests
      Serial.println("v");
      break;

    case 31365:  // ^ scroll up tests
      Serial.println("^");
      break;

    case 30982:  // EQ negative tests internal setup
      Serial.println("EQ");
      break;

    case 30855:  // ST/REPT Positive tests Select Test and Repeat Test
    Serial.println("ST/REPT");
      break;

    case 31110:  // 0
      Serial.println("0");
      break;

    case 30600:  // 1
      Serial.println("1");
      break;

    case 30472:  // 2
      Serial.println("2");
      break;

    case 30345:  // 3
      Serial.println("3");
      break;

    case 30090:  // 4
      Serial.println("4");
      break;

    case 29962:  // 5
      Serial.println("5");
      break;

    case 29835:  // 6
      Serial.println("6");
      break;

    case 29580:  // 7
      Serial.println("7");
      break;

    case 29452:  // 8
      Serial.println("8");
      break;

    case 29325:  // 9
      Serial.println("9");
      break;
     
    default:
      {
        Serial.print("Key ");
        Serial.print(key);
        Serial.println(" not programmed");
      }
    break;
  }
}


userHeadPic anonymous
2010-12-02 09:45:58 Thanks for the nice big library, but it is not tailored for the DFR0107.
I tailored this code for the DFR0107. The code was originally based on a very well written remote controller code I found for a sony controller. I did my best to keep that nice simple format. The main change was to divide up the 32 bits and instead of using memory consuming long integers, I divided down the bitlength of 32 bits to two 15 bit codes. I just ignored the middle two bits. This code all runs from one sketch. no need for header files etc. All it does is identify the key press and output the keypress to the serial monitor.  This keeps the 21 switch case routines as a simple foundation to build on so you can insert the program you need for each button.

Code: Select all
// 0.1 by pmalmsten http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1176098434
// 0.2 by farkinga
// 0.3 by farkinga - adds cool behaviors
/* 0.4 by pstrobl 
   changed the original program to use on IR Kit For Arduino Model: DFR0107 32 bit controller. 
   All keypresses are sent to the serial monitor at 9600 baud.
   pulseIn is always HIGH. The phototransistor in the kit does not invert the signal.
   uses pin 13 for heartbeat debug
   32 bits requires a long variable, so divided up into two 15 bit so can use integer variables
   use the first 15 bits of the 32 bits for remote and data stream verification. This code is always the same for every button press
   use the last 15 of the 32 bits for button press selection. This code changes for each button.
   ignore the middle 2 bits, it never changes.  
*/

#define IR_BIT_LENGTH 32    // number of bits sent by IR remote
#define FirstLastBit 15     // divide 32 bits into two 15 bit chunks for integer variables. Ignore center two bits. they are all the same.
#define BIT_1 1500          // Binary 1 threshold (Microseconds)
#define BIT_0 450           // Binary 0 threshold (Microseconds)
#define BIT_START 4000      // Start bit threshold (Microseconds)

#define IR_PIN 2            // IR Sensor pin
#define LED_PIN 13          // LED goes off when signal is received

int debug = 0;              // flag as 1 to output raw IR pulse data stream length in microseconds
int output_verify = 0;      // flag as 1 to print decoded verification integers. same number for all buttons
int output_key = 0;         // flag as 1 to print decoded key integers
int remote_verify = 16128;  // verifies first bits are 11111100000000 different remotes may have different start codes

void setup() {
  pinMode(LED_PIN, OUTPUT);	//This shows when ready to recieve
  pinMode(IR_PIN, INPUT);
  digitalWrite(LED_PIN, LOW);
  Serial.begin(9600);
}

void loop() {
  digitalWrite(LED_PIN, HIGH);
  int key = get_ir_key();
  
  digitalWrite(LED_PIN, LOW);  // turn LED off while processing response
  do_response(key);
  delay(130);                  // 2 cycle delay to cancel duplicate keypresses
}

/*
  wait for a keypress from the IR remote, and return the
  integer mapping of that key (e.g. power button on remote returns 
  the integer 1429)
*/

int get_ir_key() 
{
  int pulse[IR_BIT_LENGTH];
  int bits[IR_BIT_LENGTH];

  do {} //Wait for a start bit
  while(pulseIn(IR_PIN, HIGH) < BIT_START);

  read_pulse(pulse);
  pulse_to_bits(pulse, bits);
  RemoteVerify(bits);
  return bits_to_int(bits);
}


/*
  use pulseIn to receive IR pulses from the remote.
  Record the length of these pulses (in ms) in an array
*/

void read_pulse(int pulse[])
{
  for (int i = 0; i < IR_BIT_LENGTH; i++)
  {
    pulse[i] = pulseIn(IR_PIN, HIGH);
  }
}

/*
  IR pulses encode binary "0" as a short pulse, and binary "1"
  as a long pulse.  Given an array containing pulse lengths,
  convert this to an array containing binary values
*/

void pulse_to_bits(int pulse[], int bits[])
{
  if (debug) { Serial.println("-----"); }
  for(int i = 0; i < IR_BIT_LENGTH; i++) 
  {
    if (debug) { Serial.println(pulse[i]); }
    if(pulse[i] > BIT_1) //is it a 1?
    {
      bits[i] = 1;
    }  
    else if(pulse[i] > BIT_0) //is it a 0?
    {
      bits[i] = 0;
    } 
    else //data is invalid...
    {
      Serial.println("Error");
    }
  }
}

/*
  check returns proper first 14 check bits
*/

void RemoteVerify(int bits[])
{
  int result = 0;
  int seed = 1;
  
  //Convert bits to integer
  for(int i = 0 ; i < (FirstLastBit) ; i++) 
  {		  
    if(bits[i] == 1) 
    {
	result += seed;
    }
    
    seed *= 2;
  }
        if (output_verify)
      {
        Serial.print("Remote ");
        Serial.print(result);
        Serial.println(" verification code");
      }
 if (remote_verify != result) {delay (60); get_ir_key();} //verify first group of bits. delay for data stream to end, then try again.
}


/*
  convert an array of binary values to a single base-10 integer
*/

int bits_to_int(int bits[])
{
  int result = 0;
  int seed = 1;
  
  //Convert bits to integer
  for(int i = (IR_BIT_LENGTH-FirstLastBit) ; i < IR_BIT_LENGTH ; i++) 
  {		  
    if(bits[i] == 1) 
    {
	result += seed;
    }   
    seed *= 2;
  }
  return result;
}


/* 
  respond to specific remote-control keys with different behaviors
*/

void do_response(int key)
{  
  
  if (output_key)
   {
      Serial.print("Key ");
      Serial.println(key);
   }
  
  switch (key)
  {
    case 32640:  // turns on UUT power
      Serial.println("POWER");
      break;

    case 32385:  // FUNC/STOP turns off UUT power
      Serial.println("FUNC/STOP");
      break;

    case 32130:  // |<< ReTest failed Test
      Serial.println("|<<");
      break;

    case 32002:  // >|| Test
      Serial.println(">||");
      break;

    case 31875:  // >>| perform selected test number
      Serial.println(">>|");
      break;

    case 32512:  // VOL+ turns on individual test beeper
      Serial.println("VOL+");
      break;

    case 31492:  // VOL- turns off individual test beeper
      Serial.println("VOL-");
      break;

    case 31620:  // v scroll down tests
      Serial.println("v");
      break;

    case 31365:  // ^ scroll up tests
      Serial.println("^");
      break;

    case 30982:  // EQ negative tests internal setup
      Serial.println("EQ");
      break;

    case 30855:  // ST/REPT Positive tests Select Test and Repeat Test
    Serial.println("ST/REPT");
      break;

    case 31110:  // 0
      Serial.println("0");
      break;

    case 30600:  // 1
      Serial.println("1");
      break;

    case 30472:  // 2
      Serial.println("2");
      break;

    case 30345:  // 3
      Serial.println("3");
      break;

    case 30090:  // 4
      Serial.println("4");
      break;

    case 29962:  // 5
      Serial.println("5");
      break;

    case 29835:  // 6
      Serial.println("6");
      break;

    case 29580:  // 7
      Serial.println("7");
      break;

    case 29452:  // 8
      Serial.println("8");
      break;

    case 29325:  // 9
      Serial.println("9");
      break;
     
    default:
      {
        Serial.print("Key ");
        Serial.print(key);
        Serial.println(" not programmed");
      }
    break;
  }
}


userHeadPic anonymous
2010-12-01 14:33:26 Please find the Arduino library for this kit.

http://www.dfrobot.com/image/data/DFR0107/IRremote.zip
userHeadPic R2D2C3PO
2010-12-01 14:33:26 Please find the Arduino library for this kit.

http://www.dfrobot.com/image/data/DFR0107/IRremote.zip
userHeadPic R2D2C3PO
2010-12-01 01:26:40 Here is my source for making the chart the microsoft excel version of the data
http://www.testelectronics.com/pic/pwr-4.xls
userHeadPic anonymous
2010-12-01 01:26:40 Here is my source for making the chart the microsoft excel version of the data
http://www.testelectronics.com/pic/pwr-4.xls
userHeadPic anonymous
2010-12-01 01:21:58 Is there any sketch for decoding the button presses on the IR Kit For Arduino Model: DFR0107? It works great, I've been able to get the timing from this program http://www.arduino.cc/playground/Code/InfraredReceivers
and from that I got this idea of what the buttons  POWER, 0, 1,2,3,4 do and what the difference in signal is between them [img]
has anyone developed a code to read this?
userHeadPic anonymous