user27258: This is a segment of an Arduino sketch, that I am trying to understand. 
I know that the purpose of the entire code is to control 2 stepper motors linked to their respective drivers which are linked to a Mega 2560, but since I am new to Arduino, I am having problems with some functions. 
This is a segment of the code. I am having problems with some statements such as unsigned short FLG = 0; and I cannot make out at all the timer interrupts part. 
What does it all mean?
/* The “define” function is NOT A PROGRAMMING STATEMENT. Actually it sets up a macro which causes a text replace to occur before code is compiled. The pre-processor of the IDE will replace the variables such as “stp” to the integer value 22 in the following subsequent sections like the “void setup” function and the “loop” function if they are called. This function helps reduce RAM usage. */

#define stp 22
#define dir 23
#define stp2 26
#define dir2 27
#define EN  24
#define dir_ 25


int i, j, t, X = 0, Y = 0, Z = 0;
// here, the integer is creating variables such as i, j, t
//and also assigning values to some of them such as X, Y and Z.

unsigned short FLG = 0;

unsigned int Speed1 = 0, Speed2 = 0; // creating variables assigning initial values; due to unsinged only positive

int ser_1[3];

int po(int, int);

void setup() {     // You define how your Arduino will be used and set up any pins and communications you will be using. This will run first and only once.

  Serial.begin(9600); // begin the data transmission between Arduino and pc at speed of 9600 bits per sec
  delay(500); // stop for 500 milliseconds

  Serial.println(“hi”); // this will print the word “hi” on the serial monitor window on pc via serial port

  pinMode(stp, OUTPUT); // tells arduino to use digital Pin 22 to output electricity
  pinMode(dir, OUTPUT); // tells arduino to use digital Pin 23 to output electricity
  pinMode(stp2, OUTPUT); // tells arduino to use digital Pin 26 to output electricity
  pinMode(dir2, OUTPUT); // tells arduino to use digital Pin 27 to output electricity
  pinMode(EN, OUTPUT); // tells arduino to use digital Pin 24 to output electricity

  noInterrupts(); // to prevent disruption of timing of codes, disables some functions in the background

  /* To use timers, the built-in timer registers on the AVR chip that store timer settings need to be configured.  There are a number of registers per timer.  Two of these registers –the Timer/Counter Control Registers- hold setup values, and are called TCCRxA and TCCRxB, where x is the timer number (TCCR1A and TCCR1B, etc.).   Each register holds 8 bits, and each bit stores a configuration value. */

  TCCR1A = 0x80; //
  TCCR1B = 0x0A; // Enable reset on compare match, 1:8 clock. don’t understand
  OCR1AH = 0x9C;
  OCR1AL = 0x3F;
  TIMSK1 = (1 << OCIE1A); // enable timer compare interrupt
  interrupts();
}
ISR(TIMER1_COMPA_vect)   // timer compare interrupt service routine
{
  res();
}
void loop(){

if(Serial.available())
 {
  ser_1[i] = Serial.read();
  char ch = Serial.read();
if(ch==’S’)
   { i=0;
     X=0;
     while(1)
{ 
  if (Serial.available() > 0)
     {
      ser_1[i]=Serial.read();
      ser_1[i]=ser_1[i]-48;
      X=X+(ser_1[i]*(po(10,i)));
      i++;
     if(i==3)
       {
        FLG++;
        break;
        }
      }
    }
}

if (ch==’A’)
  { i=0;
    Y=0;
     while(1)
     {
        if (Serial.available() > 0)
      {
      ser_1[i]=Serial.read();
      ser_1[i]=ser_1[i]-48;
      Y=Y+(ser_1[i]*(po(10,i)));
       i++;
      if (i==3)
          {
           FLG++;
           break;
           }
          }
        }
      } 

     if(FLG==2)
    {
      Speed1=X;
      Speed2=Y;
      digitalWrite(EN,LOW);
        delay(100);
      StepForwardDefault();
        delay(1000);
      StepForwardDefault2();
         delay(1000);
      ReverseStepDefault2();
      ReverseStepDefault();
      delay(1000);
      digitalWrite(EN,HIGH);
      delay(1000); 
      }
      else
      Serial.println(“sequence is stop”);
}
 /*****************po******************/

int po(int num, int pow)
{ int result=1;
  if(pow==0)
  return 1;
  else
 {
  for(int f=1;f<=pow;f++)
    {
  result=result*num;
    }
 return result;
 }
}

/************ MOTOR 1 Function *****************/

void ReverseStepDefault()
{
  Serial.println(“motor a reverse”);
  Serial.println(“Moving in reverse at default step mode.”);
digitalWrite(dir_, HIGH); // Pull direction pin high to move in “reverse”
digitalWrite(dir, LOW);
delay(100);
for(int x=1; x<200; x++) // Loop the stepping enough times for motion to be visible
{
  digitalWrite(stp, HIGH); //Trigger one step
  delay(305-X);
  digitalWrite(stp, LOW);  // Pull step pin low so it can be triggered again3
  delay(305-X);
  }
}


/************ MOTOR 2 Function *****************/

void ReverseStepDefault2()
{
  Serial.println(“Moving b reverse”);
  digitalWrite(dir_, HIGH); // Pull direction pin high to move in “reverse”
  digitalWrite(dir2, LOW);
  delay(100);
  for(int x =1; x<200; x++) // Loop the stepping enough times for motion to be visible
  {
   digitalWrite(stp2,HIGH); // Trigger one step
   delay(305-Y);
   digitalWrite(stp2,LOW); // Pull step pin low so it can be triggered again3
   delay(305-Y);
   }
}

void StepForwardDefault2()
{
   Serial.println(“motor b forward”);
   digitalWrite(dir_,HIGH); // Pull direction pin high to move in “reverse”
   digitalWrite(dir2,HIGH);
   delay(100);
   for(int x=1; x<200; x++) // Loop the forward stepping enough times for motion to be visible

{
  digitalWrite(stp2,HIGH); // Trigger one step forward
  delay(305-Y);
  digitalWrite(stp2,LOW); // Pull step pin low so it can be triggered again
  delay(305-Y);
    }
}

void res()
 {
   if(Serial.available())
  {
   ser_1[i]=Serial.read();
   char ch = Serial.read();
   if(ch ==’T’)
      {
       FLG=0;
       Serial.println(“stop is pressed”);
      }
   }
}


user28033: Here is some explanation :
TCCR1A = 0x80; //OC1 will cleared on compare match
TCCR1B = 0x0A; //Turn on CTC mode --> it means the interrupt counter will reset to 0, and causing the interrupt happen again and again.
               //Divide clock 1:8 (prescaler)
OCR1AH = 0x9C;
OCR1AL = 0x3F; //compare value = 0x9C3F = 39999
TIMSK1 = (1<<OCIE1A); // the micro will jump to the Output Compare A Interrupt vetor upon compare match.

Overall, this setup will make your program do whatever you put in the Interrupt Service Routine ISR(TIMER1_COMPA_vect){...do whatever here...}
The interrupt interval is =

(OCR1A+1) × prescaler ÷ F_CPU
(39999+1) × 8 ÷ 16.000.000 = 0.02 sec

as for res(); and unsigned short FLG; I dont know what that is, since its probably your own creation (not from arduino libs)
You can found more detailed explanation here and here

user22924: I'm assuming you don't mean "all" when you ask "what does it all mean?"
I also assume that the comments are from you - in which case you've done reasonably well!
/* The “define” function is NOT A PROGRAMMING STATEMENT... */

True, #define isn't a programming statement that produces code: it's a way of defining a symbol that you can use in the rest of the code, both for explanation purposes and ease of changing: without having to search for all copies of 22 - and then decide if this particular 22 is for that pin or not.
unsigned short FLG=0;

int is 16 bits in the Arduino, but in some systems it's 32 (and in others 64). short is (practically) always 16 bits regardless of system, so many programmers use short when they know they definitely want a 16-bit integer. unsigned merely means that it can never go negative - which allows the variable to hold higher values (0 to 65,535 instead of -32,768 to 32,767).
int ser_1[3];

This is an array of 3 ints called ser_1, which can be accessed as ser_1[0], ser_1[1] and ser_1[2]. In C, all arrays start at 0. Why use an array? It makes it possible to use another variabe to say which ser_1 you want: e.g. ser_1[i]. If i is 2, you get the last one.
int po(int,int);

This says that there will be a function called po that takes two int values, processes them, and returns an int result.
noInterrupts(); // to prevent disruption of timing of codes, disables some functions in the background

Pretty much. An interrupt can happen at any time: the CPU will stop executing the main program, execute the interrupt code, then resume where it left off in the main program. Sometimes that's a bad thing! If the interrupt code is going to change something that this code needs to stay the same (or vice versa!), then Bad Things can happen. To prevent that, you can turn off interrupts temporarily with this function, and turn them on again later with interrupts() (which you'll see below).
/* To use timers, the built-in timer registers on the AVR chip that store timer settings need to be configured.  There are a number of registers per timer.  Two of these registers –the Timer/Counter Control Registers- hold setup values, and are called TCCRxA and TCCRxB, where x is the timer number (TCCR1A and TCCR1B, etc.).   Each register holds 8 bits, and each bit stores a configuration value. */

 TCCR1A = 0x80; // 
 TCCR1B = 0x0A; // Enable reset on compare match, 1:8 clock. don’t understand

OK, this is complicated:

By default, a timer simply counts from 0 to 65,535 (maximum 16 bit value), then resets and starts again.
How quickly? One 'tick' is the system clock, so very quickly! But you can "pre-scale" that with a divisor. In this case, they're using a divisor of 8, so the timer will "tick" an eighth as fast.
You can configure the timer to interrupt when it resets, so that special code will execute on a timed basis.
You can also tell it that if at any time it hits a certain number to also interrupt (Compare Match) - that's two different interrupts.
You can also tell it that when it hits that Compare number, to reset back to 0 and start counting again. That means that the timer will interrupt more often.  

OCR1AH = 0x9C;
OCR1AL = 0x3F;
These stand for "Output Compare Register 1A High" and "Output Compare Register 1A Low" - a 16 bit value that is 0x9C3F (37839 decimal). If you divide the system clock by that number, (and a further 8 for the pre-scaler), you'll find out how often it interrupts.
interrupts();

This turns on interrupts again.
ISR(TIMER1_COMPA_vect)   // timer compare interrupt service routine
{
res();
}

This is the code that executes every timer interrupt. I don't know what res() does.

user7508: 
I am having problems with some statements such as
  "unsigned short FLG = 0;"

This is the C and C++ syntax for declaring a variable. The statement
means “Let FLG be an unsigned short integer and let it's initial value
be zero”. An unsigned short can hold any integer value between 0 and
65535 inclusive.
The Arduino language reference documents the types
short and
unsigned int but,
for a mysterious reason, it does not document unsigned short.

I cannot make out at all the timer interrupts part.

Nobody can unless they know the internals of the AVR timers, which are
non trivial but are well covered in the
datasheet.
It's a thick document, but you only need to study chapters 14:
Interrupts and 17: 16-bit Timer/Counter (Timer/Counter 1, 3, 4, and
5).
I recommend you first read the Timers and counters
tutorial by Nick Gammon, then go to the
datasheet for the most authoritative information.
Edit: Just a comment... I find the program you have posted to be
written with poor programming style. More specifically, the section
setting the Timer 1 registers would be better written as follows:
TCCR1A = bit(COM1A1);  // set OC1A to LOW on compare match
TCCR1B = bit(WGM12)    // CTC mode, TOP = OCR1A
       | bit(CS11);    // clock at F_CPU / 8
OCR1A  = 40000 - 1;    // period = 40000 prescaled clock cycles
TIMSK1 = bit(OCIE1A);  // enable COMPA interrupt

It is still obscure if you don't know the timer, but using explicit bit
names instead of pure numbers makes things a lot easier when you refer
to the datasheet. Also, there is no point in setting each byte of
OCR1A in a different statement: it only makes things harder to read.

27258: <p>This is a segment of an Arduino sketch, that I am trying to understand. </p>

<p>I know that the purpose of the entire code is to control 2 stepper motors linked to their respective drivers which are linked to a Mega 2560, but since I am new to Arduino, I am having problems with some functions. </p>

<p>This is a segment of the code. I am having problems with some statements such as <code>unsigned short FLG = 0;</code> and I cannot make out at all the timer interrupts part. </p>

<p>What does it all mean?</p>



<pre class="lang-c prettyprint-override"><code>/* The “define” function is NOT A PROGRAMMING STATEMENT. Actually it sets up a macro which causes a text replace to occur before code is compiled. The pre-processor of the IDE will replace the variables such as “stp” to the integer value 22 in the following subsequent sections like the “void setup” function and the “loop” function if they are called. This function helps reduce RAM usage. */

#define stp 22
#define dir 23
#define stp2 26
#define dir2 27
#define EN  24
#define dir_ 25


int i, j, t, X = 0, Y = 0, Z = 0;
// here, the integer is creating variables such as i, j, t
//and also assigning values to some of them such as X, Y and Z.

unsigned short FLG = 0;

unsigned int Speed1 = 0, Speed2 = 0; // creating variables assigning initial values; due to unsinged only positive

int ser_1[3];

int po(int, int);

void setup() {     // You define how your Arduino will be used and set up any pins and communications you will be using. This will run first and only once.

  Serial.begin(9600); // begin the data transmission between Arduino and pc at speed of 9600 bits per sec
  delay(500); // stop for 500 milliseconds

  Serial.println(“hi”); // this will print the word “hi” on the serial monitor window on pc via serial port

  pinMode(stp, OUTPUT); // tells arduino to use digital Pin 22 to output electricity
  pinMode(dir, OUTPUT); // tells arduino to use digital Pin 23 to output electricity
  pinMode(stp2, OUTPUT); // tells arduino to use digital Pin 26 to output electricity
  pinMode(dir2, OUTPUT); // tells arduino to use digital Pin 27 to output electricity
  pinMode(EN, OUTPUT); // tells arduino to use digital Pin 24 to output electricity

  noInterrupts(); // to prevent disruption of timing of codes, disables some functions in the background

  /* To use timers, the built-in timer registers on the AVR chip that store timer settings need to be configured.  There are a number of registers per timer.  Two of these registers –the Timer/Counter Control Registers- hold setup values, and are called TCCRxA and TCCRxB, where x is the timer number (TCCR1A and TCCR1B, etc.).   Each register holds 8 bits, and each bit stores a configuration value. */

  TCCR1A = 0x80; //
  TCCR1B = 0x0A; // Enable reset on compare match, 1:8 clock. don’t understand
  OCR1AH = 0x9C;
  OCR1AL = 0x3F;
  TIMSK1 = (1 &lt;&lt; OCIE1A); // enable timer compare interrupt
  interrupts();
}
ISR(TIMER1_COMPA_vect)   // timer compare interrupt service routine
{
  res();
}
void loop(){

if(Serial.available())
 {
  ser_1[i] = Serial.read();
  char ch = Serial.read();
if(ch==’S’)
   { i=0;
     X=0;
     while(1)
{ 
  if (Serial.available() &gt; 0)
     {
      ser_1[i]=Serial.read();
      ser_1[i]=ser_1[i]-48;
      X=X+(ser_1[i]*(po(10,i)));
      i++;
     if(i==3)
       {
        FLG++;
        break;
        }
      }
    }
}

if (ch==’A’)
  { i=0;
    Y=0;
     while(1)
     {
        if (Serial.available() &gt; 0)
      {
      ser_1[i]=Serial.read();
      ser_1[i]=ser_1[i]-48;
      Y=Y+(ser_1[i]*(po(10,i)));
       i++;
      if (i==3)
          {
           FLG++;
           break;
           }
          }
        }
      } 

     if(FLG==2)
    {
      Speed1=X;
      Speed2=Y;
      digitalWrite(EN,LOW);
        delay(100);
      StepForwardDefault();
        delay(1000);
      StepForwardDefault2();
         delay(1000);
      ReverseStepDefault2();
      ReverseStepDefault();
      delay(1000);
      digitalWrite(EN,HIGH);
      delay(1000); 
      }
      else
      Serial.println(“sequence is stop”);
}
 /*****************po******************/

int po(int num, int pow)
{ int result=1;
  if(pow==0)
  return 1;
  else
 {
  for(int f=1;f&lt;=pow;f++)
    {
  result=result*num;
    }
 return result;
 }
}

/************ MOTOR 1 Function *****************/

void ReverseStepDefault()
{
  Serial.println(“motor a reverse”);
  Serial.println(“Moving in reverse at default step mode.”);
digitalWrite(dir_, HIGH); // Pull direction pin high to move in “reverse”
digitalWrite(dir, LOW);
delay(100);
for(int x=1; x&lt;200; x++) // Loop the stepping enough times for motion to be visible
{
  digitalWrite(stp, HIGH); //Trigger one step
  delay(305-X);
  digitalWrite(stp, LOW);  // Pull step pin low so it can be triggered again3
  delay(305-X);
  }
}


/************ MOTOR 2 Function *****************/

void ReverseStepDefault2()
{
  Serial.println(“Moving b reverse”);
  digitalWrite(dir_, HIGH); // Pull direction pin high to move in “reverse”
  digitalWrite(dir2, LOW);
  delay(100);
  for(int x =1; x&lt;200; x++) // Loop the stepping enough times for motion to be visible
  {
   digitalWrite(stp2,HIGH); // Trigger one step
   delay(305-Y);
   digitalWrite(stp2,LOW); // Pull step pin low so it can be triggered again3
   delay(305-Y);
   }
}

void StepForwardDefault2()
{
   Serial.println(“motor b forward”);
   digitalWrite(dir_,HIGH); // Pull direction pin high to move in “reverse”
   digitalWrite(dir2,HIGH);
   delay(100);
   for(int x=1; x&lt;200; x++) // Loop the forward stepping enough times for motion to be visible

{
  digitalWrite(stp2,HIGH); // Trigger one step forward
  delay(305-Y);
  digitalWrite(stp2,LOW); // Pull step pin low so it can be triggered again
  delay(305-Y);
    }
}

void res()
 {
   if(Serial.available())
  {
   ser_1[i]=Serial.read();
   char ch = Serial.read();
   if(ch ==’T’)
      {
       FLG=0;
       Serial.println(“stop is pressed”);
      }
   }
}
</code></pre>
 28033: <p>Here is some explanation :</p>
<pre><code>TCCR1A = 0x80; //OC1 will cleared on compare match
TCCR1B = 0x0A; //Turn on CTC mode --&gt; it means the interrupt counter will reset to 0, and causing the interrupt happen again and again.
               //Divide clock 1:8 (prescaler)
OCR1AH = 0x9C;
OCR1AL = 0x3F; //compare value = 0x9C3F = 39999
TIMSK1 = (1&lt;&lt;OCIE1A); // the micro will jump to the Output Compare A Interrupt vetor upon compare match.
</code></pre>
<p>Overall, this setup will make your program do whatever you put in the Interrupt Service Routine <code>ISR(TIMER1_COMPA_vect){...do whatever here...}</code></p>
<p>The interrupt interval is =</p>
<blockquote>
<p>(OCR1A+1) × prescaler ÷ F_CPU</p>
<p>(39999+1) × 8 ÷ 16.000.000 = 0.02 sec</p>
</blockquote>
<p>as for <code>res();</code> and <code>unsigned short FLG;</code> I dont know what that is, since its probably your own creation (not from arduino libs)</p>
<p>You can found more detailed explanation <a href="http://www.avrbeginners.net/architecture/timers/timers.html" rel="nofollow noreferrer">here</a> and <a href="http://letsmakerobots.com/node/28278" rel="nofollow noreferrer">here</a></p>
 22924: <p>I'm assuming you don't mean "all" when you ask "what does it <em>all</em> mean?"
I also assume that the comments are from you - in which case you've done reasonably well!</p>

<pre><code>/* The “define” function is NOT A PROGRAMMING STATEMENT... */
</code></pre>

<p>True, <code>#define</code> isn't a programming statement that produces code: it's a way of defining a symbol that you can use in the rest of the code, both for explanation purposes and ease of changing: without having to search for all copies of <code>22</code> - and then decide if <em>this particular</em> <code>22</code> is for that pin or not.</p>

<pre><code>unsigned short FLG=0;
</code></pre>

<p><code>int</code> is 16 bits in the Arduino, but in some systems it's 32 (and in others 64). <code>short</code> is (practically) always 16 bits regardless of system, so many programmers use <code>short</code> when they know they definitely want a 16-bit integer. <code>unsigned</code> merely means that it can never go negative - which allows the variable to hold higher values (0 to 65,535 instead of -32,768 to 32,767).</p>

<pre><code>int ser_1[3];
</code></pre>

<p>This is an array of 3 <code>int</code>s called <code>ser_1</code>, which can be accessed as <code>ser_1[0]</code>, <code>ser_1[1]</code> and <code>ser_1[2]</code>. In C, all arrays start at <code>0</code>. Why use an array? It makes it possible to use <em>another</em> variabe to say <em>which</em> <code>ser_1</code> you want: e.g. <code>ser_1[i]</code>. If <code>i</code> is <code>2</code>, you get the last one.</p>

<pre><code>int po(int,int);
</code></pre>

<p>This says that there will be a function called <code>po</code> that takes two <code>int</code> values, processes them, and returns an <code>int</code> result.</p>

<pre><code>noInterrupts(); // to prevent disruption of timing of codes, disables some functions in the background
</code></pre>

<p>Pretty much. An interrupt can happen at any time: the CPU will stop executing the main program, execute the interrupt code, then resume where it left off in the main program. Sometimes that's a bad thing! If the interrupt code is going to change something that <em>this</em> code needs to stay the same (or vice versa!), then Bad Things can happen. To prevent that, you can turn off interrupts temporarily with this function, and turn them on again later with <code>interrupts()</code> (which you'll see below).</p>

<pre><code>/* To use timers, the built-in timer registers on the AVR chip that store timer settings need to be configured.  There are a number of registers per timer.  Two of these registers –the Timer/Counter Control Registers- hold setup values, and are called TCCRxA and TCCRxB, where x is the timer number (TCCR1A and TCCR1B, etc.).   Each register holds 8 bits, and each bit stores a configuration value. */

 TCCR1A = 0x80; // 
 TCCR1B = 0x0A; // Enable reset on compare match, 1:8 clock. don’t understand
</code></pre>

<p>OK, this is complicated:</p>

<ul>
<li>By default, a timer simply counts from <code>0</code> to <code>65,535</code> (maximum 16 bit value), then resets and starts again.</li>
<li>How quickly? One 'tick' is the system clock, so <em>very</em> quickly! But you can "pre-scale" that with a divisor. In this case, they're using a divisor of 8, so the timer will "tick" an eighth as fast.</li>
<li>You can configure the timer to interrupt when it resets, so that special code will execute on a timed basis.</li>
<li>You can also tell it that if at any time it hits a certain number to <em>also</em> interrupt (Compare Match) - that's two different interrupts.</li>
<li>You can <em>also</em> tell it that when it hits that Compare number, to reset back to <code>0</code> and start counting again. That means that the timer will interrupt more often.  </li>
</ul>

<p><code>OCR1AH = 0x9C;</code><br>
<code>OCR1AL = 0x3F;</code></p>

<p>These stand for "Output Compare Register 1A High" and "Output Compare Register 1A Low" - a 16 bit value that is 0x9C3F (37839 decimal). If you divide the system clock by that number, (and a further 8 for the pre-scaler), you'll find out how often it interrupts.</p>

<pre><code>interrupts();
</code></pre>

<p>This turns on interrupts again.</p>

<pre><code>ISR(TIMER1_COMPA_vect)   // timer compare interrupt service routine
{
res();
}
</code></pre>

<p>This is the code that executes every timer interrupt. I don't know what <code>res()</code> does.</p>
 7508: <blockquote>
  <p>I am having problems with some statements such as
  "unsigned short FLG = 0;"</p>
</blockquote>

<p>This is the C and C++ syntax for declaring a variable. The statement
means “Let <code>FLG</code> be an unsigned short integer and let it's initial value
be zero”. An <code>unsigned short</code> can hold any integer value between 0 and
65535 inclusive.</p>

<p>The Arduino language reference documents the types
<a href="https://www.arduino.cc/en/Reference/Short" rel="noreferrer"><code>short</code></a> and
<a href="https://www.arduino.cc/en/Reference/UnsignedInt" rel="noreferrer"><code>unsigned int</code></a> but,
for a mysterious reason, it does not document <code>unsigned short</code>.</p>

<blockquote>
  <p>I cannot make out at all the timer interrupts part.</p>
</blockquote>

<p>Nobody can unless they know the internals of the AVR timers, which are
non trivial but are well covered in <a href="http://www.atmel.com/Images/Atmel-2549-8-bit-AVR-Microcontroller-ATmega640-1280-1281-2560-2561_datasheet.pdf" rel="noreferrer">the
datasheet</a>.
It's a thick document, but you only need to study chapters 14:
<em>Interrupts</em> and 17: <em>16-bit Timer/Counter (Timer/Counter 1, 3, 4, and
5)</em>.</p>

<p>I recommend you first read the <a href="http://gammon.com.au/timers" rel="noreferrer">Timers and counters
tutorial</a> by Nick Gammon, then go to the
datasheet for the most authoritative information.</p>

<p><strong>Edit</strong>: Just a comment... I find the program you have posted to be
written with poor programming style. More specifically, the section
setting the Timer 1 registers would be better written as follows:</p>

<pre class="lang-c++ prettyprint-override"><code>TCCR1A = bit(COM1A1);  // set OC1A to LOW on compare match
TCCR1B = bit(WGM12)    // CTC mode, TOP = OCR1A
       | bit(CS11);    // clock at F_CPU / 8
OCR1A  = 40000 - 1;    // period = 40000 prescaled clock cycles
TIMSK1 = bit(OCIE1A);  // enable COMPA interrupt
</code></pre>

<p>It is still obscure if you don't know the timer, but using explicit bit
names instead of pure numbers makes things a lot easier when you refer
to the datasheet. Also, there is no point in setting each byte of
<code>OCR1A</code> in a different statement: it only makes things harder to read.</p>
