Serial.println after x seconds is counted using the TimerOne library

#include <TimerOne.h>

int timerInterruptLed = 13;

int x = 1;

volatile boolean onOrOff = HIGH;        //Set led on or off inside the ISR
volatile unsigned long functionCounter = 0; //Count how many times the function is called

unsigned int motherLoopCounter = 0; //To pick up the value of the function loop

void setup() {
  Serial.begin(9600);
  Serial.println("Hi! Timer program running!");
  pinMode(timerInterruptLed, OUTPUT);
  Timer1.initialize(2000000); //2 million microseconds is 2 seconds
  Timer1.attachInterrupt(function);
  digitalWrite(timerInterruptLed, HIGH);
}

void loop() {
  if(x > 0)
  {
  noInterrupts();
  motherLoopCounter =  functionCounter;
  interrupts();

  if (motherLoopCounter == 5) {
    noInterrupts();
    x = 0;
    functionCounter = 0;
    int w = 200;
    Serial.println(w);
  }
  }
}

//interrupt service routine function
void function() {
  functionCounter  ++;
  if (functionCounter == 5)
  {
    onOrOff =! onOrOff;
    digitalWrite(timerInterruptLed, onOrOff);
  }
}

After 10 seconds are over, I want the program to print the value 200, or for that matter, even a string saying “10 seconds done” would do. But, the output always comes out to be just the first two characters of the string I print or 20 in 200. Where am I going wrong?

Serial.print uses interrupts.

I would expect that you have to call another function to say "ok, start timing"! I mean - what happens if you set the timer to 5us and it takes a few cycles before you can get around to attaching the interrupt? Th library has got to have a way to handle that scenario.

  noInterrupts();
  motherLoopCounter =  functionCounter;
  interrupts();

Why are motherLoopCounter and functionCounter different types? You can NOT reasonably expect a value in a long, signed or unsigned, to fit in an int, signed or unsigned.

    int w = 200;
    Serial.println(w);

Well, that told you a lot!

Why are you using int and long for the types for motherLoopCounter and functionCounter, when the maximum value that you allow them to take on is 5? Are you certain that you don't need long long as the type?

If you used reasonable types, the copy and reset would be atomic operations, so you would NOT need to be diddling with interrupts.

PaulMurrayCbr:
I would expect that you have to call another function to say "ok, start timing"! I mean - what happens if you set the timer to 5us and it takes a few cycles before you can get around to attaching the interrupt? Th library has got to have a way to handle that scenario.

Timer1.attachInterrupt and Timer1.detachInterrupt are ones that answer your question I believe.

PaulS:

  noInterrupts();

motherLoopCounter =  functionCounter;
  interrupts();



Why are motherLoopCounter and functionCounter different types? You can NOT reasonably expect a value in a long, signed or unsigned, to fit in an int, signed or unsigned.



int w = 200;
    Serial.println(w);



Well, that told you a lot!

Why are you using int and long for the types for motherLoopCounter and functionCounter, when the maximum value that you allow them to take on is 5? Are you certain that you don't need long long as the type?

If you used reasonable types, the copy and reset would be atomic operations, so you would NOT need to be diddling with interrupts.

Thank you, PaulS for the suggestions. I had to detach the interrupt before serial printing the value of w and it worked fine post that and the data type mismatch is something I looked into and corrected. If I hadn't, there would have been issues for higher values of motherLoopCounter. Thank You!

sterretje:
Serial.print uses interrupts.

Yes, detaching the interrupt helped me print the value correctly. Thank you!