|
| 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 | + |
0 commit comments