2

I'm using the Arduino sketch for LED stroboscope found here: http://mehax.blogspot.co.uk/2011/02/arduino-stroboscope.html.

Also below, I've inserted the loop part where this calculations are made:

void loop() {
// this while is a workaround for timer limitations using micros vs millis
sensorValue = analogRead(analogInPin);   //take potentiometer reading
hold = sensorValue*55 + 400;             //add some time to scale and for code execution
microsCurrent = micros();                // collect current time
if (microsCurrent > microsPrev + hold){  //set up timing loop
frequency = 1000000/(microsCurrent - microsPrev);    
microsPrev = microsPrev + hold;            //set up millis for delay stuff

//set all 6 LEDs high (they were already plugged in)
digitalWrite(3,HIGH);
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(11,HIGH);
delayMicroseconds(.02*hold);  //leave LEDs on long enough to see

//set all LEDs low
digitalWrite(3,LOW);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,LOW);

if(millis() > millisSerial + 200){ 
  millisSerial=millis();    //reset timer
  holdFrequency = 1000000/hold;    
  //do some math to find values
  rpm = frequency * 60;

I want to use this code in my project, but I don't really understand how this code calculates the stobing frequency.

So, I have following questions:

  1. Why "hold" variable is calculated in the given way?
  2. Why "microsCurrent", "microsPrev" and all other timings are executed?
  3. How "frequency is calculated"?
  4. What is "hold frequency" variable?

Sorry, I'm very new to Arduino and don't know much coding. Will appreciate very much your help!

1
  • About that program you are using: you may want to take a look at the comment I just posted in the author's blog. Commented Aug 26, 2016 at 13:07

1 Answer 1

1

Why "hold" variable is calculated in the given way?

hold stores the intended delay between the strobe pulses, i.e. the stroboscope period. It is computed as an affine function of the potentiometer cursor position because... well, that's the simplest way to make it user-adjustable. 400 µs is the shortest period that (according to the program author) one may reasonably want to set.

Why "microsCurrent", "microsPrev" and all other timings are executed?

See the Blink Without Delay Arduino tutorial.

How "frequency is calculated"?

From the code:

frequency = 1000000/(microsCurrent - microsPrev);

The frequency is the inverse of the period. The period is the time elapsed between this strobe and the previous one. The numerator is a conversion factor to get the frequency in hertz from a period in microseconds.

What is "hold frequency" variable?

It's the intended frequency (inverse of the intended period), disregarding the timing jitter that makes the actual instantaneous frequency slightly different.

2
  • Thanks for your prompt reply! This made almost everything clear in my head. So, the "frequency" variable shows the real strobe frequency (taking into consideration the possible delays), while "hold frequency" is the intended freq., but it's not used to display anything in this code, right? and what kind of delays there might be? Thank you.
    – George
    Commented Aug 26, 2016 at 10:34
  • @George: I am still examining the code, and I believe there is a bug. holdFrequency seems to have the correct average frequency, which is probably the most relevant datum. frequency is computed from the difference between the actual time of the current pulse and the intended time of the previous one. It can therefore carry a slight bias. Commented Aug 26, 2016 at 10:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.