Skip to content

Commit 17e96f6

Browse files
authored
Add files via upload
1 parent a1eb7d4 commit 17e96f6

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// SPDX-FileCopyrightText: 2018 Limor Fried/ladyada for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: CC-BY-SA-3.0
4+
5+
/* Trinket/Gemma compatible Raw IR decoder sketch
6+
This sketch/program uses an Adafruit Trinket or Gemma
7+
ATTiny85 based mini microcontroller and a PNA4602 to
8+
decode IR received. This can be used to make a IR receiver
9+
(by looking for a particular code) or transmitter
10+
(by pulsing an IR LED at ~38KHz for the durations pulse_index
11+
12+
Based on Adafruit tutorial http://learn.adafruit.com/ir-sensor/using-an-ir-sensor
13+
14+
and ATTiny program by TinyPCRemote Nathan Chantrell http://nathan.chantrell.net
15+
under Creative Commons Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0) license
16+
17+
SendSoftwareSerial Lirary modification by Nick Gammon from NewSoftwareSerial code
18+
GNU Lesser General Public License as published by the Free Software Foundation version 2.1
19+
at http://gammon.com.au/Arduino/SendOnlySoftwareSerial.zip
20+
*/
21+
#include <SoftwareSerial.h> // use if you do not wish to use the lightweight library
22+
23+
SoftwareSerial Serial(0,1); // Receive, Transmit (Receive not used)
24+
25+
// We need to use the 'raw' pin reading methods because timing is very important here
26+
// and the digitalRead() procedure is slower!
27+
#define IRpin_PIN PINB // ATTiny85 had Port B pins
28+
#define IRpin 2
29+
30+
#define MAXPULSE 12000 // the maximum pulse we'll listen for - 5 milliseconds
31+
#define NUMPULSES 34 // max IR pulse pairs to sample
32+
#define RESOLUTION 2 // time between IR measurements
33+
#define STORED_BUTTON_CODES 4 // remote control codes stored
34+
35+
// we will store up to 100 pulse pairs (this is -a lot-)
36+
uint16_t pulses[NUMPULSES]; // high and low pulses
37+
uint16_t pulse_index = 0; // index for pulses we're storing
38+
uint32_t irCode = 0;
39+
40+
void setup(void) {
41+
Serial.begin(9600);
42+
Serial.println();
43+
Serial.println("Ready to decode IR!");
44+
pinMode(IRpin, INPUT); // Listen to IR receiver on Trinket/Gemma pin D2
45+
}
46+
47+
void loop(void) {
48+
// Wait for an IR Code
49+
uint16_t numpulse=listenForIR();
50+
51+
// Process the pulses to get a single number representing code
52+
for (int i = 0; i < NUMPULSES; i++) {
53+
Serial.print(pulses[i]);
54+
Serial.print(", ");
55+
}
56+
Serial.println("\n");
57+
}
58+
59+
uint16_t listenForIR() { // IR receive code
60+
pulse_index = 0;
61+
while (1) {
62+
unsigned int highpulse, lowpulse; // temporary storage timing
63+
highpulse = lowpulse = 0; // start out with no pulse length
64+
65+
while (IRpin_PIN & _BV(IRpin)) { // got a high pulse
66+
highpulse++;
67+
delayMicroseconds(RESOLUTION);
68+
if (((highpulse >= MAXPULSE) && (pulse_index != 0))|| pulse_index == NUMPULSES) {
69+
return pulse_index;
70+
}
71+
}
72+
pulses[pulse_index] = highpulse;
73+
74+
while (! (IRpin_PIN & _BV(IRpin))) { // got a low pulse
75+
lowpulse++;
76+
delayMicroseconds(RESOLUTION);
77+
if (((lowpulse >= MAXPULSE) && (pulse_index != 0))|| pulse_index == NUMPULSES) {
78+
return pulse_index;
79+
}
80+
}
81+
pulses[pulse_index] = lowpulse;
82+
pulse_index++;
83+
84+
}
85+
86+
}
87+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# SPDX-FileCopyrightText: 2018 Mikey Sklar for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import time
6+
7+
import adafruit_irremote
8+
import board
9+
import pulseio
10+
11+
IR_PIN = board.D2 # Pin connected to IR receiver.
12+
13+
print('IR listener')
14+
print()
15+
# Create pulse input and IR decoder.
16+
pulses = pulseio.PulseIn(IR_PIN, maxlen=200, idle_state=True)
17+
decoder = adafruit_irremote.GenericDecode()
18+
19+
# Loop waiting to receive pulses.
20+
while True:
21+
# make sure pulses is empty
22+
# small delay for cleaner results
23+
pulses.clear()
24+
pulses.resume()
25+
time.sleep(.1)
26+
27+
# Wait for a pulse to be detected.
28+
detected = decoder.read_pulses(pulses)
29+
30+
# print the number of pulses detected
31+
# note: pulse count is an excellent indicator as to the quality of IR code
32+
# received.
33+
#
34+
# If you are expecting 67 each time (Adafruit Mini Remote Control #389)
35+
# and only receive 57 this will result in a incomplete listener
36+
37+
print("pulse count: ", len(detected))
38+
39+
# print in list form of the pulse duration in microseconds
40+
# typically starts with ~9,000 microseconds followed by a ~4,000
41+
# microseconds which is standard IR preamble
42+
43+
print(detected)
44+
print()

0 commit comments

Comments
 (0)