AlexShu

Get Raw IR Codes for IRemote "Binary"

Apr 23rd, 2015
364
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. // the maximum pulse we'll listen for - 65 milliseconds is a long time
  6. #define MAXPULSE 65000
  7.  
  8.  
  9. // what our timing resolution should be, larger is better
  10. // as its more 'precise' - but too large and you wont get
  11. // accurate timing
  12. #define RESOLUTION 20
  13.  
  14.  
  15. // we will store up to 300 pulse pairs (this is -a lot-)
  16. uint16_t pulses[300][2]; // pair is high and low pulse
  17. uint8_t currentpulse = 0; // index for pulses we're storing
  18.  
  19.  
  20. void setup(void) {
  21.   Serial.begin(9600);
  22.   Serial.println("Ready to decode IR!");
  23. }
  24.  
  25.  
  26. void loop(void) {
  27.   uint16_t highpulse, lowpulse; // temporary storage timing
  28.   highpulse = lowpulse = 0; // start out with no pulse length
  29.  
  30.  
  31. // while (digitalRead(IRpin)) { // this is too slow!
  32.     while (IRpin_PIN & (1 << IRpin)) {
  33.      // pin is still HIGH
  34.  
  35.  
  36.      // count off another few microseconds
  37.      highpulse++;
  38.      delayMicroseconds(RESOLUTION);
  39.  
  40.  
  41.      // If the pulse is too long, we 'timed out' - either nothing
  42.      // was received or the code is finished, so print what
  43.      // we've grabbed so far, and then reset
  44.      if ((highpulse >= MAXPULSE) && (currentpulse != 0)) {
  45.        printpulses();
  46.        currentpulse=0;
  47.        return;
  48.      }
  49.   }
  50.   // we didn't time out so lets stash the reading
  51.   pulses[currentpulse][0] = highpulse;
  52.  
  53.   // same as above
  54.   while (! (IRpin_PIN & _BV(IRpin))) {
  55.      // pin is still LOW
  56.      lowpulse++;
  57.      delayMicroseconds(RESOLUTION);
  58.      if ((lowpulse >= MAXPULSE) && (currentpulse != 0)) {
  59.        printpulses();
  60.        currentpulse=0;
  61.        return;
  62.      }
  63.   }
  64.   pulses[currentpulse][1] = lowpulse;
  65.  
  66.  
  67.   // we read one high-low pulse successfully, continue!
  68.   currentpulse++;
  69. }
  70.  
  71.  
  72. void printpulses(void) {
  73.  
  74.  
  75.   for (uint8_t i = 0; i < currentpulse; i++) {
  76.     if(i!=0){
  77.  
  78. //################### First Array Code Pulse Division ########################
  79.    
  80. if((int)(pulses[i][0] * RESOLUTION)>7000.0){
  81.       Serial.print("3");
  82. }
  83. if((pulses[i][0] * RESOLUTION>4000)&&(pulses[i][0] * RESOLUTION<5000)){
  84.       Serial.print("2");
  85. }
  86. if((pulses[i][0] * RESOLUTION>1000)&&(pulses[i][0] * RESOLUTION<2000)){
  87.       Serial.print("1");
  88. }
  89. if((pulses[i][0] * RESOLUTION>0)&&(pulses[i][0] * RESOLUTION<1000)){
  90.       Serial.print("0");
  91. }
  92. //########################### End of First Array ##############################
  93.     Serial.print(", ");
  94. }
  95.  
  96. //################### Second Array Code Pulse Division ########################
  97.  
  98. if((int)(pulses[i][1] * RESOLUTION)>7000.0){
  99.       Serial.print("3");
  100. }
  101. if((pulses[i][1] * RESOLUTION>4000)&&(pulses[i][1] * RESOLUTION<5000)){
  102.       Serial.print("2");
  103. }
  104. if((pulses[i][1] * RESOLUTION>1000)&&(pulses[i][1] * RESOLUTION<2000)){
  105.       Serial.print("1");
  106. }
  107. if((pulses[i][1] * RESOLUTION>0)&&(pulses[i][1] * RESOLUTION<1000)){
  108.       Serial.print("0");
  109. }  
  110. //########################### End of Second Array ##############################
  111.  
  112.     Serial.print(", ");
  113.   }
  114. Serial.println("");
  115. }
RAW Paste Data