Hi all, I'm working on a strobe light (just one LED to begin with) and I would like to tune the frequency to precise values, which I thought would be a piece of cake with Arduino. However, I couldn't use a loop()/delay(period) method:
int T = 1;
void loop() {
digitalWrite(12, HIGH);
delayMicroseconds(500);
digitalWrite(12, LOW);
delay(T);
}
a max frequency of only 48 Hz can be reached with this method, still far from the 16 MHz of the Atmega. Even when my period T was less than 1 ms, which should yield f = 1/T = 1000 Hz.... moreover it was not very stable, the frequency would vary by 10 to 20%.
So I have searched and found this (among other interestting websites/blogs):
the problem is I found myself unable to understand what is going on in this source code. I really don't understand how the LED is turned on and off. Nothing else than changing the frequency seems to happen in the loop() function, so the function making the LED blink must be set in the setup() (or before? :o ), but I am really unable to see where. A detail: although it is not indicated, it seems like the guy is using the Timer1.h library. Here is the setup() function, where everything seems to happen (especially lines 9 to 14):
void setup() {
pinMode(PIN_SYNC,OUTPUT);
digitalWrite(PIN_SYNC,LOW); // show we arrived
analogWrite(PIN_PWM10,0); // turn off other PWM outputs
analogWrite(PIN_PWM11,0);
analogWrite(PIN_STROBE,1); // let Arduino set up default Timer1 PWM
TCCR1B = 0; // turn off Timer1 for strobe setup
TCCR1A = 0x82; // clear OCR1A on match, Fast PWM, lower WGM1x = 14
ICR1 = FlashPdCt;
OCR1A = FlashLengthCt;
TCNT1 = FlashLengthCt - 1;
TCCR1B = 0x18 | TCCRxB_CS; // upper WGM1x = 14, Prescale 1:64, start Timer1
pinMode(PIN_KNOB_B,INPUT_PULLUP);
pinMode(PIN_KNOB_A,INPUT_PULLUP);
KnobState = digitalRead(PIN_KNOB_A);
Button = PrevButton = ReadButtons(PIN_BUTTONS);
attachInterrupt((PIN_KNOB_A - 2),KnobHandler,CHANGE);
Serial.begin(9600);
fdevopen(&s_putc,0); // set up serial output for printf()
printf("Stroboscope Tachometer\r\nEd Nisley - KE4ZNU - December 2012\r\n");
printf("Frequency: %d.%02d\nPulse duration: %d us\n",
(int)FlashFreq,(int)(100.0 * (FlashFreq - trunc(FlashFreq))),
(int)(1e6 * FlashLength));
MillisThen = millis();
}
Some guidance, or links to other (easier) methods would be welcome.
Thanks