Skip to content

Commit fd34db7

Browse files
authored
Merge pull request #2795 from adafruit/ir_demod
Adding IR demodulator examples
2 parents eaa77c8 + 9eca690 commit fd34db7

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
/*
6+
* Based on the ReceiverDump.cpp from the
7+
* Arduino-IRremote https://github.com/Arduino-IRremote/Arduino-IRremote.
8+
* by Armin Joachimsmeyer
9+
************************************************************************************
10+
* MIT License
11+
*
12+
* Copyright (c) 2020-2023 Armin Joachimsmeyer
13+
*
14+
*/
15+
16+
#include <Arduino.h>
17+
#include <IRremote.hpp>
18+
19+
#define IR_RECEIVE_PIN 5
20+
#define MARK_EXCESS_MICROS 20 // Adapt it to your IR receiver module. 20 is recommended for the cheap VS1838 modules.
21+
22+
int ir_count = 0;
23+
bool ir_state = false;
24+
25+
void setup() {
26+
pinMode(LED_BUILTIN, OUTPUT);
27+
28+
Serial.begin(115200);
29+
// Start the receiver and if not 3. parameter specified, take LED_BUILTIN pin from the internal boards definition as default feedback LED
30+
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
31+
Serial.print(F("Ready to receive IR signals "));
32+
Serial.print("at pin ");
33+
Serial.println(IR_RECEIVE_PIN);
34+
35+
}
36+
37+
void loop() {
38+
// put your main code here, to run repeatedly:
39+
if (IrReceiver.decode()) { // Grab an IR code
40+
// At 115200 baud, printing takes 200 ms for NEC protocol and 70 ms for NEC repeat
41+
ir_state = true;
42+
Serial.println(); // blank line between entries
43+
Serial.println(); // 2 blank lines between entries
44+
IrReceiver.printIRResultShort(&Serial);
45+
if (IrReceiver.decodedIRData.flags & IRDATA_FLAGS_WAS_OVERFLOW) {
46+
Serial.print("Try to increase the \"RAW_BUFFER_LENGTH\" value of ");
47+
Serial.println(RAW_BUFFER_LENGTH);
48+
// see also https://github.com/Arduino-IRremote/Arduino-IRremote#compile-options--macros-for-this-library
49+
} else {
50+
Serial.println();
51+
Serial.println("IR signal received!");
52+
IrReceiver.printIRResultRawFormatted(&Serial, true); // Output the results in RAW format
53+
ir_count += 1;
54+
Serial.print("Signal count: ");
55+
Serial.println(ir_count);
56+
Serial.println();
57+
}
58+
IrReceiver.resume();
59+
}
60+
else {
61+
ir_state = false;
62+
}
63+
64+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2024 Liz Clark for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import pulseio
6+
import board
7+
8+
pulses = pulseio.PulseIn(board.D5)
9+
pulse = False
10+
pulse_count = 0
11+
12+
while True:
13+
14+
# Wait for an active pulse
15+
while len(pulses) == 0:
16+
if pulse:
17+
pulse = False
18+
19+
if len(pulses) != 0 and not pulse:
20+
pulse_count += 1
21+
print(f"pulses detected {pulse_count} times")
22+
pulse = True
23+
# Pause while we do something with the pulses
24+
pulses.pause()
25+
# Print the pulses. pulses[0] is an active pulse unless the length
26+
# reached max length and idle pulses are recorded.
27+
print(pulses[0])
28+
# Clear the rest
29+
pulses.clear()
30+
# Resume with an 80 microsecond active pulse
31+
pulses.resume(80)

0 commit comments

Comments
 (0)