I'm trying to test out a IR receiver and determine what frequency works best. I tried this code and determined that it was an IR transistor. But something was making me think... I was seeing the LED flash (I was testing it using a regular red LED) up to 37kHz. As I understand it, our eyes are not capable of seeing things flash beyond 60Hz or 100Hz due to persistence of vision. This makes me think that I am doing something wrong in my code. Here it is:
#include<TimerOne.h>
template<class T> inline Print &operator <<(Print &obj, T arg) { obj.print(arg); return obj; }
static char const endl = '\n';
static int const inputPin = 0;
static int const outputPin = 10;
long inc = 500;
unsigned long freq = 30e3; // 30kHz
void setup()
{
analogReference(EXTERNAL);
pinMode(outputPin, OUTPUT);
Timer1.initialize(1e9/freq/2); // /2 because each interrupt is to toggle LED, doubling the frequency to call interrupt.
Timer1.attachInterrupt(callback);
Serial.begin(9600);
Serial << "Freq = " << freq << "Hz - Period = " << 1e9/freq << "us" << endl;
}
int ledStatus = 0;
void callback()
{
digitalWrite(outputPin, ledStatus = !ledStatus);
}
void loop()
{
//delay(1000);
if (freq > 56e3)
{
inc = -500;
}
else if (freq < 30e3)
{
inc = +500;
}
freq += inc; // increase by 0.5kHz
Timer1.setPeriod(1e9/freq/2);
analogRead(inputPin); // this extra is because I found elsewhere that the first read may not work properly
long x = 0;
for (int i = 0; i < 10000; ++i)
{
x += analogRead(inputPin);
}
Serial << "Freq = " << freq << "Hz - Period = " << 1e9/freq << "us" << " - " << x/10000 << endl;
}
It uses the TimerOne library I got here: Arduino Playground - HomePage
Can someone confirm that I'm doing this right or tell me if a flicker can be seen up to 37kHz?
Thanks,
Adrian