Skip to content
This repository was archived by the owner on Apr 6, 2019. It is now read-only.

Commit ccf89c5

Browse files
committed
First working version
0 parents  commit ccf89c5

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

rawirdecode.pde

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/* Raw IR decoder sketch!
2+
3+
This sketch/program uses the Arduno and a PNA4602 to
4+
decode IR received. This can be used to make a IR receiver
5+
(by looking for a particular code)
6+
or transmitter (by pulsing an IR LED at ~38KHz for the
7+
durations detected
8+
9+
Code is public domain, check out www.ladyada.net and adafruit.com
10+
for more tutorials!
11+
*/
12+
13+
// We need to use the 'raw' pin reading methods
14+
// because timing is very important here and the digitalRead()
15+
// procedure is slower!
16+
//uint8_t IRpin = 2;
17+
// Digital pin #2 is the same as Pin D2 see
18+
// http://arduino.cc/en/Hacking/PinMapping168 for the 'raw' pin mapping
19+
#define IRpin_PIN PIND
20+
#define IRpin 2
21+
22+
// the maximum pulse we'll listen for - 65 milliseconds is a long time
23+
#define MAXPULSE 65000
24+
25+
// what our timing resolution should be, larger is better
26+
// as its more 'precise' - but too large and you wont get
27+
// accurate timing
28+
#define RESOLUTION 20
29+
30+
// we will store up to 100 pulse pairs (this is -a lot-)
31+
uint16_t pulses[100][2]; // pair is high and low pulse
32+
uint8_t currentpulse = 0; // index for pulses we're storing
33+
34+
void setup(void) {
35+
Serial.begin(9600);
36+
Serial.println("Ready to decode IR!");
37+
}
38+
39+
void loop(void) {
40+
uint16_t highpulse, lowpulse; // temporary storage timing
41+
highpulse = lowpulse = 0; // start out with no pulse length
42+
43+
44+
// while (digitalRead(IRpin)) { // this is too slow!
45+
while (IRpin_PIN & (1 << IRpin)) {
46+
// pin is still HIGH
47+
48+
// count off another few microseconds
49+
highpulse++;
50+
delayMicroseconds(RESOLUTION);
51+
52+
// If the pulse is too long, we 'timed out' - either nothing
53+
// was received or the code is finished, so print what
54+
// we've grabbed so far, and then reset
55+
if ((highpulse >= MAXPULSE) && (currentpulse != 0)) {
56+
printpulses();
57+
currentpulse=0;
58+
return;
59+
}
60+
}
61+
// we didn't time out so lets stash the reading
62+
pulses[currentpulse][0] = highpulse;
63+
64+
// same as above
65+
while (! (IRpin_PIN & _BV(IRpin))) {
66+
// pin is still LOW
67+
lowpulse++;
68+
delayMicroseconds(RESOLUTION);
69+
if ((lowpulse >= MAXPULSE) && (currentpulse != 0)) {
70+
printpulses();
71+
currentpulse=0;
72+
return;
73+
}
74+
}
75+
pulses[currentpulse][1] = lowpulse;
76+
77+
// we read one high-low pulse successfully, continue!
78+
currentpulse++;
79+
}
80+
81+
void printpulses(void) {
82+
Serial.println("\n\r\n\rReceived: \n\rOFF \tON");
83+
for (uint8_t i = 0; i < currentpulse; i++) {
84+
Serial.print(pulses[i][0] * RESOLUTION, DEC);
85+
Serial.print(" usec, ");
86+
Serial.print(pulses[i][1] * RESOLUTION, DEC);
87+
Serial.println(" usec");
88+
}
89+
}

0 commit comments

Comments
 (0)