I’ve been trying to make a custom IR Code reader just for learning. I know that I can do this the Arduino library but I want to know how to make the library my self and learn more Ir signals while doing it.
I made a simple function that works sometimes. It records IR signals each time but will I think there is a problem somewhere.Maybe a timing problem. When I send the recorded signal, sometimes it works, sometimes it doesn’t. I looked through it and realized that the problem is my recording function which is posted below. There is something wrong with it. Can anyone here figure out what the problem is? The transmit Ir code function is fine. Just the recording function…
For example when I record the IR value, it will only work 1 out of 10 tries. It will generate a valid value once out of about 10 tries. Sometimes it will generate 3 valid values out of 10 times. When I say valid values, I mean pair of values that works if sent through IR led.
void recordIRPattern() {
#define IRpin_PIN PIND
int IrReceiverPin = 2;
bool keepReading = true;
long RESPONSE_TIMEOUT = 5000; //Seconds second wait
long tempTimeOut = RESPONSE_TIMEOUT + millis();
bool pinFirstTimeHigh = true;
unsigned long pinHighTime = 0;
unsigned long pinLowTime = 0;
int pulseAmount = 0;
unsigned long pauseWaitTime[5000];
Serial.println("Started recoring ");
bool oldPinStatus = false;
while ((keepReading) && (millis() < tempTimeOut)) {
//Check if pin is high
if (IRpin_PIN & (1 << IrReceiverPin) && !(oldPinStatus)) {
//Check if this is the first name pin is going high
if (pinFirstTimeHigh) {
//get time pin went high, increement pulseAmount then make pinFirstTimeHigh false
pinHighTime = micros();
pulseAmount++; //Because of this first increementation, we use pulseAmount-1 each time we want to update the value in the index
oldPinStatus = true;
pinFirstTimeHigh = false;
} else {
//Not first time High(get time pin went high)
//get time pin went high, calcualte & and save how long pin was low then increement pulseAmount
pinHighTime = micros();
pauseWaitTime[pulseAmount - 1] = pinHighTime - pinLowTime;
pulseAmount++;
oldPinStatus = true;
}
} else if (! (IRpin_PIN & _BV(IrReceiverPin)) && (oldPinStatus)) {
//Ignore low reading if pinFirstTimeHigh is true
if (pinFirstTimeHigh) {
} else {
//get time pin went low, calcualte & and save how long pin was high then increement pulseAmount
pinLowTime = micros();
pauseWaitTime[pulseAmount - 1] = pinLowTime - pinHighTime;
pulseAmount++;
oldPinStatus = false;
}
}
}
//The amount of pulse is pulseAmount-1;
pulseAmount--;
//Make sure we dont have a nagative number
if (pulseAmount < 0) {
pulseAmount = 0;
}
Serial.print("We got: ");
Serial.print(pulseAmount);
Serial.println(" bytes");
Serial.print("They are: ");
for (int i = 0; i < pulseAmount; i++) {
Serial.print(pauseWaitTime[i]);
Serial.print(" ");
}
}