Understanding Code with interrupts and serial input

Hello guy,

I just started with arduinos, and got this code here to immedeiately work with. It was originally for a teensy, but I am working with an arduino due.
This code is to read the data of an SRM crank in. In other words, it sends data via FM to a contstruction on a breadboard and this breadboard is connected to the arduino. Everything was set up before I started to use it, I just had to change the baud rate and one time Serial.println to SerialUSB.prinln.

But unfortunately, I don’t really get what’s happening in this code, so a friend recommended me to ask the community here.
Does anyone get the code here?

Thank you very much!

//October 2017
// Simple read HZ using pin 11 and interrupts
//Connect red (Blue) to 3.3v
//Black (green) = ground (GND)
//White (violett) = hz signal into any digital pin, just change number below to match pin
#define hzPin 11
#define DEGREE_FLAG 1

volatile uint8_t bUpdateFlagsShared; //hold the updated flag
// shared variables are updated by the ISR and read by loop.
const int BUFFERSIZE = 50;//change this to whatever size you want to read from the serial port
volatile uint8_t BufferInShared = 0;
volatile uint8_t HZSamplesShared = 0;
volatile int Samples = 0;

unsigned long HZStart;
unsigned long HZPeriod;
unsigned long HZ;

void setup(void)
 Serial.begin(9600);//this might need to change for the DUE
 pinMode(hzPin, INPUT);
 attachInterrupt(hzPin, counterhz, RISING);

void loop(void)
 static int HZIn[BUFFERSIZE];
 static uint8_t bUpdateFlags;
 static uint8_t HZSamples = 0;

 noInterrupts(); // turn interrupts off quickly while we take local copies of the shared variables
 if (bUpdateFlagsShared)
   // take a local copy of which channels were updated in case we need to use this in the rest of loop
   bUpdateFlags = bUpdateFlagsShared;
   if (bUpdateFlags & DEGREE_FLAG)
     HZSamples = HZSamplesShared;
     memcpy (HZIn, HZInShared, sizeof HZInShared);
   bUpdateFlagsShared = 0;
   BufferInShared = 0; //need to be reset?
   HZSamplesShared = 0;

 if (bUpdateFlags & DEGREE_FLAG  && Samples < 50) {
   for (int i = 0; i < HZSamples; i++) { //print HZ array to serial port
     if (i < HZSamples - 1) {
   bUpdateFlags = 0;

void counterhz() //This is what determines the frequency
 HZPeriod = micros() - HZStart;
 if (HZPeriod >= 75) { //285us=3500hz, 334=3000hz
   HZStart = micros();

   HZ = 1000000 / HZPeriod; //convert period to frequency
   if ( HZSamplesShared < BUFFERSIZE && HZ > 300 && HZ < 10000) {//simple filter to eliminate small or large values
     HZInShared[HZSamplesShared++] = HZ;
     HZ = 10000;//not sure why I do this, you migth be able to remove it
 if (HZSamplesShared == BUFFERSIZE) {//when buffer if full, set flag to print data to serial port
   bUpdateFlagsShared |= DEGREE_FLAG;


They attache the counterhz function to the interrupt, set to RISING, so when the pin goes high, that ISR runs, and records the time since the last time it went high in that array,

If the number of entries it’s put into the array is enough to fill it, it sets a flag.

That flag is then tested for in loop, and if present, it does the math to get a frequency from that, and prints it to serial.

Do you have a more specific question?

Also, use code tags when posting code (the </> button)

Sorry about the code tag!

What is a flag? I mean I know about what the code is doing, but I have no idea about the steps, when is happening what.

So what I am doing right now is, I will connect a switch button to another input pin of the arduino (A0). Whenever I click the button, I want the serial port to show me the time, of when I clicked. I wanted to try that with another interrupt, but I think that interrupts and time do not work well together, from what I have found out in my research. I just made an if statement now, which looks for a higher value than 500, cause the button gives me values between 0 and about 600. I just want to get the first moment, when the signal rises, but with the following code, I always get the following numbers as well until I release the button.

if (sensorValue > 500)

Do you have a recommendation for that? I think that my input voltage is too low to distinguish between "HIGH" and "LOW".