AlexShu

Get Raw IR Codes for IRemote

Apr 23rd, 2015
553
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define IRpin_PIN PIND
  2. #define IRpin 2
  3.  
  4.  
  5. // for MEGA use these!
  6. //#define IRpin_PIN PINE
  7. //#define IRpin 4
  8.  
  9.  
  10. // the maximum pulse we'll listen for - 65 milliseconds is a long time
  11. #define MAXPULSE 65000
  12.  
  13.  
  14. // what our timing resolution should be, larger is better
  15. // as its more 'precise' - but too large and you wont get
  16. // accurate timing
  17. #define RESOLUTION 20
  18.  
  19.  
  20. // we will store up to 300 pulse pairs (this is -a lot-)
  21. uint16_t pulses[300][2]; // pair is high and low pulse
  22. uint8_t currentpulse = 0; // index for pulses we're storing
  23.  
  24.  
  25. void setup(void) {
  26.   Serial.begin(9600);
  27.   Serial.println("Ready to decode IR!");
  28. }
  29.  
  30.  
  31. void loop(void) {
  32.   uint16_t highpulse, lowpulse; // temporary storage timing
  33.   highpulse = lowpulse = 0; // start out with no pulse length
  34.  
  35.  
  36. // while (digitalRead(IRpin)) { // this is too slow!
  37.     while (IRpin_PIN & (1 << IRpin)) {
  38.      // pin is still HIGH
  39.  
  40.  
  41.      // count off another few microseconds
  42.      highpulse++;
  43.      delayMicroseconds(RESOLUTION);
  44.  
  45.  
  46.      // If the pulse is too long, we 'timed out' - either nothing
  47.      // was received or the code is finished, so print what
  48.      // we've grabbed so far, and then reset
  49.      if ((highpulse >= MAXPULSE) && (currentpulse != 0)) {
  50.        printpulses();
  51.        currentpulse=0;
  52.        return;
  53.      }
  54.   }
  55.   // we didn't time out so lets stash the reading
  56.   pulses[currentpulse][0] = highpulse;
  57.  
  58.   // same as above
  59.   while (! (IRpin_PIN & _BV(IRpin))) {
  60.      // pin is still LOW
  61.      lowpulse++;
  62.      delayMicroseconds(RESOLUTION);
  63.      if ((lowpulse >= MAXPULSE) && (currentpulse != 0)) {
  64.        printpulses();
  65.        currentpulse=0;
  66.        return;
  67.      }
  68.   }
  69.   pulses[currentpulse][1] = lowpulse;
  70.  
  71.  
  72.   // we read one high-low pulse successfully, continue!
  73.   currentpulse++;
  74. }
  75.  
  76.  
  77. void printpulses(void) {
  78.   // IR Remote Results
  79.   Serial.println("For IR Remote Arduino sketch: ");
  80.   Serial.print("unsigned int raw[");
  81.   Serial.print(currentpulse*2, DEC);
  82.   Serial.print("] = {");
  83.   for (uint8_t i = 0; i < currentpulse; i++) {
  84.     if(i!=0){
  85.     Serial.print(pulses[i][0] * RESOLUTION, DEC);
  86.     Serial.print(", ");
  87.     }
  88.     Serial.print(pulses[i][1] * RESOLUTION, DEC);
  89.     Serial.print(", ");
  90.   }
  91.   Serial.print("};");
  92.   Serial.println("");
  93.   Serial.print("irsend.sendRaw(raw,");
  94.   Serial.print(currentpulse*2, DEC);
  95.   Serial.print(",38);");
  96.   Serial.println("");
  97.   Serial.println("");
  98. }
RAW Paste Data