Hey,
I'm trying to get an IR tester to work. Principle is easy, I'll leave it running and if he recieves a specific IR signal, an LED turns on and stays on. When pressing an reset switch, the LED turns of again.
Problem is:
When the tester recieves the signel, the LED turns on.
When I press the switch after the LED turned on, it doesn't turn it off.
Only then pressing the switch, while the signal is recieved, the LED turns off.
It looks like, the second If is only tested, as long the first If is true.
The part in question:
void loop() {
// Wait for an IR Code
numberpulses = listenForIR();
Reset = digitalRead(ResetPin);
if (IRcompare(numberpulses, RedButton, sizeof(RedButton) / 4)) {
digitalWrite(SwitchPin, HIGH);
delay(100);
}
if ((Reset == HIGH) && (SwitchPin == HIGH)) {
digitalWrite(SwitchPin, LOW);
Serial.print("Reset");
delay(100);
}
}
Whole Code:
// SPDX-FileCopyrightText: 2018 Limor Fried/ladyada for Adafruit Industries
//
// SPDX-License-Identifier: CC-BY-SA-3.0
/* Trinket/Gemma compatible Raw IR decoder sketch
This sketch/program uses an Adafruit Trinket or Gemma
ATTiny85 based mini microcontroller and a PNA4602 to
decode IR received. This can be used to make a IR receiver
(by looking for a particular code) or transmitter
(by pulsing an IR LED at ~38KHz for the durations pulse_index
Based on Adafruit tutorial http://learn.adafruit.com/ir-sensor/using-an-ir-sensor
and ATTiny program by TinyPCRemote Nathan Chantrell http://nathan.chantrell.net
under Creative Commons Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0) license
SendSoftwareSerial Lirary modification by Nick Gammon from NewSoftwareSerial code
GNU Lesser General Public License as published by the Free Software Foundation version 2.1
at http://gammon.com.au/Arduino/SendOnlySoftwareSerial.zip
*/
// We need to use the 'raw' pin reading methods because timing is very important here
// and the digitalRead() procedure is slower!
#define IRpin_PIN PIND // ATTiny85 had Port B pins
#define IRpin 2 //IR Reciever Pin
#define SwitchPin 3 //Pin switched by Signal
#define ResetPin 4 //Pin to Reset SwitchPin
#define MAXPULSE 65000 // the maximum pulse we'll listen for - 5 milliseconds
#define NUMPULSES 50 // max IR pulse pairs to sample
#define RESOLUTION 20 // time between IR measurements
#define FUZZINESS 25 // What percent we will allow in variation to match the same code
#define HIGH 0x1
#define LOW 0x0
// we will store up to 100 pulse pairs (this is -a lot-)
uint16_t pulses[NUMPULSES][2]; // high and low pulses
uint16_t pulse_index = 0; // index for pulses we're storing
uint16_t numberpulses = 0;
int Reset = 0;
#include "signalsample.h"
void setup() {
pinMode(IRpin, INPUT);
pinMode(SwitchPin, OUTPUT);
pinMode(ResetPin, INPUT);
Serial.begin(9600);
}
void loop() {
// Wait for an IR Code
numberpulses = listenForIR();
Reset = digitalRead(ResetPin);
if (IRcompare(numberpulses, RedButton, sizeof(RedButton) / 4)) {
digitalWrite(SwitchPin, HIGH);
delay(100);
}
if ((Reset == HIGH) && (SwitchPin == HIGH)) {
digitalWrite(SwitchPin, LOW);
Serial.print("Reset");
delay(100);
}
}
//KGO: added size of compare sample. Only compare the minimum of the two
boolean IRcompare(int numbpulses, int Signal[], int refsize) {
for (int i = 0; i < numbpulses - 1; i++) {
int oncode = pulses[i][1] * RESOLUTION / 10;
int offcode = pulses[i + 1][0] * RESOLUTION / 10;
// check to make sure the error is less than FUZZINESS percent
if (abs(oncode - Signal[i * 2 + 0]) <= (Signal[i * 2 + 0] * FUZZINESS / 100)) {
} else {
return false;
}
if (abs(offcode - Signal[i * 2 + 1]) <= (Signal[i * 2 + 1] * FUZZINESS / 100)) {
} else {
return false;
}
}
return true;
}
uint16_t listenForIR() { // IR receive code
pulse_index = 0;
while (1) {
unsigned int highpulse, lowpulse; // temporary storage timing
highpulse = lowpulse = 0; // start out with no pulse length
while (IRpin_PIN & _BV(IRpin)) { // got a high pulse
highpulse++;
delayMicroseconds(RESOLUTION);
if (((highpulse >= MAXPULSE) && (pulse_index != 0)) || pulse_index == NUMPULSES) {
return pulse_index;
}
}
pulses[pulse_index][0] = highpulse;
while (!(IRpin_PIN & _BV(IRpin))) { // got a low pulse
lowpulse++;
delayMicroseconds(RESOLUTION);
if (((lowpulse >= MAXPULSE) && (pulse_index != 0)) || pulse_index == NUMPULSES) {
return pulse_index;
}
}
pulses[pulse_index][1] = lowpulse;
pulse_index++;
}
}
The code is an abdomination from multiple examples and my beginner coding and is normally for an Adafruit Trinket but I have a ported Version for my Uno clone for debugging.
The signalsample.h is what the name says, the sample of the signal which triggers the LED.
