Skip to content

Commit 6af60f2

Browse files
authored
Merge pull request #606 from adafruit/TheKitty-patch-64
Add files via upload
2 parents 3f5bfb1 + 4b13865 commit 6af60f2

3 files changed

Lines changed: 308 additions & 1 deletion

File tree

IR_Sensor/Arduino/IR_Commander/.uno.test

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/******************* our codes ************/
2+
3+
int ApplePlaySignal[] = {
4+
// ON, OFF (in 10's of microseconds)
5+
912, 438,
6+
68, 48,
7+
68, 158,
8+
68, 158,
9+
68, 158,
10+
68, 48,
11+
68, 158,
12+
68, 158,
13+
68, 158,
14+
70, 156,
15+
70, 158,
16+
68, 158,
17+
68, 48,
18+
68, 46,
19+
70, 46,
20+
68, 46,
21+
68, 160,
22+
68, 158,
23+
70, 46,
24+
68, 158,
25+
68, 46,
26+
70, 46,
27+
68, 48,
28+
68, 46,
29+
68, 48,
30+
66, 48,
31+
68, 48,
32+
66, 160,
33+
66, 50,
34+
66, 160,
35+
66, 50,
36+
64, 160,
37+
66, 50,
38+
66, 3950,
39+
908, 214,
40+
66, 3012,
41+
908, 212,
42+
68, 0};
43+
44+
int AppleForwardSignal[] = {
45+
// ON, OFF (in 10's of microseconds)
46+
908, 444,
47+
64, 50,
48+
66, 162,
49+
64, 162,
50+
64, 162,
51+
64, 52,
52+
64, 162,
53+
64, 162,
54+
64, 162,
55+
64, 164,
56+
62, 164,
57+
64, 162,
58+
64, 52,
59+
62, 52,
60+
64, 52,
61+
64, 50,
62+
64, 164,
63+
64, 50,
64+
64, 164,
65+
64, 162,
66+
64, 50,
67+
66, 50,
68+
66, 50,
69+
64, 50,
70+
66, 50,
71+
64, 52,
72+
64, 50,
73+
66, 160,
74+
66, 50,
75+
64, 162,
76+
66, 50,
77+
64, 162,
78+
64, 50,
79+
66, 3938,
80+
906, 214,
81+
66, 3014,
82+
906, 214,
83+
64, 0};
84+
85+
int AppleRewindSignal[] = {
86+
// ON, OFF (in 10's of microseconds)
87+
908, 442,
88+
66, 48,
89+
66, 162,
90+
66, 160,
91+
66, 160,
92+
66, 50,
93+
66, 160,
94+
66, 160,
95+
66, 160,
96+
68, 158,
97+
68, 160,
98+
66, 160,
99+
66, 50,
100+
66, 48,
101+
66, 50,
102+
66, 48,
103+
66, 162,
104+
66, 160,
105+
66, 48,
106+
68, 48,
107+
66, 160,
108+
66, 50,
109+
66, 50,
110+
66, 48,
111+
66, 50,
112+
66, 48,
113+
68, 48,
114+
66, 160,
115+
66, 50,
116+
66, 160,
117+
66, 50,
118+
66, 160,
119+
66, 48,
120+
68, 3936,
121+
906, 214,
122+
66, 0};
123+
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
/* Raw IR commander
2+
3+
This sketch/program uses the Arduno and a PNA4602 to
4+
decode IR received. It then attempts to match it to a previously
5+
recorded IR signal. Limor Fried, Adafruit Industries
6+
7+
MIT License, please attribute
8+
check out learn.adafruit.com for more tutorials!
9+
*/
10+
11+
// We need to use the 'raw' pin reading methods
12+
// because timing is very important here and the digitalRead()
13+
// procedure is slower!
14+
//uint8_t IRpin = 2;
15+
// Digital pin #2 is the same as Pin D2 see
16+
// http://arduino.cc/en/Hacking/PinMapping168 for the 'raw' pin mapping
17+
#define IRpin_PIN PIND
18+
#define IRpin 2
19+
20+
// the maximum pulse we'll listen for - 65 milliseconds is a long time
21+
#define MAXPULSE 65000
22+
#define NUMPULSES 50
23+
24+
// what our timing resolution should be, larger is better
25+
// as its more 'precise' - but too large and you wont get
26+
// accurate timing
27+
#define RESOLUTION 20
28+
29+
// What percent we will allow in variation to match the same code
30+
#define FUZZINESS 20
31+
32+
// we will store up to 100 pulse pairs (this is -a lot-)
33+
uint16_t pulses[NUMPULSES][2]; // pair is high and low pulse
34+
uint8_t currentpulse = 0; // index for pulses we're storing
35+
36+
#include "ircommander.h"
37+
38+
void setup(void) {
39+
Serial.begin(9600);
40+
Serial.println("Ready to decode IR!");
41+
}
42+
43+
void loop(void) {
44+
int numberpulses;
45+
46+
numberpulses = listenForIR();
47+
48+
Serial.print("Heard ");
49+
Serial.print(numberpulses);
50+
Serial.println("-pulse long IR signal");
51+
if (IRcompare(numberpulses, ApplePlaySignal,sizeof(ApplePlaySignal)/4)) {
52+
Serial.println("PLAY");
53+
}
54+
if (IRcompare(numberpulses, AppleRewindSignal,sizeof(AppleRewindSignal)/4)) {
55+
Serial.println("REWIND");
56+
}
57+
if (IRcompare(numberpulses, AppleForwardSignal,sizeof(AppleForwardSignal)/4)) {
58+
Serial.println("FORWARD");
59+
}
60+
delay(500);
61+
}
62+
63+
//KGO: added size of compare sample. Only compare the minimum of the two
64+
boolean IRcompare(int numpulses, int Signal[], int refsize) {
65+
int count = min(numpulses,refsize);
66+
Serial.print("count set to: ");
67+
Serial.println(count);
68+
for (int i=0; i< count-1; i++) {
69+
int oncode = pulses[i][1] * RESOLUTION / 10;
70+
int offcode = pulses[i+1][0] * RESOLUTION / 10;
71+
72+
#ifdef DEBUG
73+
Serial.print(oncode); // the ON signal we heard
74+
Serial.print(" - ");
75+
Serial.print(Signal[i*2 + 0]); // the ON signal we want
76+
#endif
77+
78+
// check to make sure the error is less than FUZZINESS percent
79+
if ( abs(oncode - Signal[i*2 + 0]) <= (Signal[i*2 + 0] * FUZZINESS / 100)) {
80+
#ifdef DEBUG
81+
Serial.print(" (ok)");
82+
#endif
83+
} else {
84+
#ifdef DEBUG
85+
Serial.print(" (x)");
86+
#endif
87+
// we didn't match perfectly, return a false match
88+
return false;
89+
}
90+
91+
92+
#ifdef DEBUG
93+
Serial.print(" \t"); // tab
94+
Serial.print(offcode); // the OFF signal we heard
95+
Serial.print(" - ");
96+
Serial.print(Signal[i*2 + 1]); // the OFF signal we want
97+
#endif
98+
99+
if ( abs(offcode - Signal[i*2 + 1]) <= (Signal[i*2 + 1] * FUZZINESS / 100)) {
100+
#ifdef DEBUG
101+
Serial.print(" (ok)");
102+
#endif
103+
} else {
104+
#ifdef DEBUG
105+
Serial.print(" (x)");
106+
#endif
107+
// we didn't match perfectly, return a false match
108+
return false;
109+
}
110+
111+
#ifdef DEBUG
112+
Serial.println();
113+
#endif
114+
}
115+
// Everything matched!
116+
return true;
117+
}
118+
119+
int listenForIR(void) {
120+
currentpulse = 0;
121+
122+
while (1) {
123+
uint16_t highpulse, lowpulse; // temporary storage timing
124+
highpulse = lowpulse = 0; // start out with no pulse length
125+
126+
// while (digitalRead(IRpin)) { // this is too slow!
127+
while (IRpin_PIN & (1 << IRpin)) {
128+
// pin is still HIGH
129+
130+
// count off another few microseconds
131+
highpulse++;
132+
delayMicroseconds(RESOLUTION);
133+
134+
// If the pulse is too long, we 'timed out' - either nothing
135+
// was received or the code is finished, so print what
136+
// we've grabbed so far, and then reset
137+
138+
// KGO: Added check for end of receive buffer
139+
if (((highpulse >= MAXPULSE) && (currentpulse != 0))|| currentpulse == NUMPULSES) {
140+
return currentpulse;
141+
}
142+
}
143+
// we didn't time out so lets stash the reading
144+
pulses[currentpulse][0] = highpulse;
145+
146+
// same as above
147+
while (! (IRpin_PIN & _BV(IRpin))) {
148+
// pin is still LOW
149+
lowpulse++;
150+
delayMicroseconds(RESOLUTION);
151+
// KGO: Added check for end of receive buffer
152+
if (((lowpulse >= MAXPULSE) && (currentpulse != 0))|| currentpulse == NUMPULSES) {
153+
return currentpulse;
154+
}
155+
}
156+
pulses[currentpulse][1] = lowpulse;
157+
158+
// we read one high-low pulse successfully, continue!
159+
currentpulse++;
160+
}
161+
}
162+
void printpulses(void) {
163+
Serial.println("\n\r\n\rReceived: \n\rOFF \tON");
164+
for (uint8_t i = 0; i < currentpulse; i++) {
165+
Serial.print(pulses[i][0] * RESOLUTION, DEC);
166+
Serial.print(" usec, ");
167+
Serial.print(pulses[i][1] * RESOLUTION, DEC);
168+
Serial.println(" usec");
169+
}
170+
171+
// print it in a 'array' format
172+
Serial.println("int IRsignal[] = {");
173+
Serial.println("// ON, OFF (in 10's of microseconds)");
174+
for (uint8_t i = 0; i < currentpulse-1; i++) {
175+
Serial.print("\t"); // tab
176+
Serial.print(pulses[i][1] * RESOLUTION / 10, DEC);
177+
Serial.print(", ");
178+
Serial.print(pulses[i+1][0] * RESOLUTION / 10, DEC);
179+
Serial.println(",");
180+
}
181+
Serial.print("\t"); // tab
182+
Serial.print(pulses[currentpulse-1][1] * RESOLUTION / 10, DEC);
183+
Serial.print(", 0};");
184+
}
185+

0 commit comments

Comments
 (0)