user58605: Is this timer set correctly for Arduino Mega? It should count exactly one second. And why when I print in serial monitor (seconds_passed++) is it incremented by 2 not by 1?
Full code on Pastebin: LINK
noInterrupts();
//set and initialize the TIMER1

TCCR1A = 0; // set entire TCCR1A register to 0
TCCR1B = 0; // set entire TCCR1B register to 0
TCCR1B |= (1 << CS12);
TIMSK1 |= (1 << TOIE1);
TCNT1 = 3036;

attachInterrupt(digitalPinToInterrupt(ignition_pin), ignitionSignal, CHANGE);
interrupts();

ISR(TIMER1_OVF_vect) {
  //TIMER1 overflow interrupt -- occurs every 1sec --
  instantSpeed();
  LPG_Consumption();
  LPG_injector_open_duration = 0;
  unleadedConsumption();
  unleadinj_Open_Duration = 0;
  seconds_passed++;
  vss_pulses = 0;
  TCNT1 = 3036;
}


user38721: Your timer configuration is ok so far. Never the less I recommend using the TIMER1_COMPA_vect interrupt vector instead of TIMER1_OVF_vect which has the advantage of better readability (in my oppinion) and TCNT1 doesn't have to be reset in the ISR (instead, OCR1A is just initialized with 62500 once in setup()). Therefore, TIMER0 has to run in CTC (clear timer on compare match) mode and WGM12 has to be set in TCCR1B.
The reason why seconds_passed is incremented twice is because you Serial.print(seconds_passed++) in instantSpeed(). Printing on the serial interface is damn slow and using it in an ISR is a no-go!

58605: <p>Is this timer set correctly for Arduino Mega? It should count exactly one second. And why when I print in serial monitor (seconds_passed++) is it incremented by 2 not by 1?</p>

<p>Full code on Pastebin: <a href="https://pastebin.com/Efkx07WR" rel="nofollow noreferrer">LINK</a></p>

<pre><code>noInterrupts();
//set and initialize the TIMER1

TCCR1A = 0; // set entire TCCR1A register to 0
TCCR1B = 0; // set entire TCCR1B register to 0
TCCR1B |= (1 &lt;&lt; CS12);
TIMSK1 |= (1 &lt;&lt; TOIE1);
TCNT1 = 3036;

attachInterrupt(digitalPinToInterrupt(ignition_pin), ignitionSignal, CHANGE);
interrupts();

ISR(TIMER1_OVF_vect) {
  //TIMER1 overflow interrupt -- occurs every 1sec --
  instantSpeed();
  LPG_Consumption();
  LPG_injector_open_duration = 0;
  unleadedConsumption();
  unleadinj_Open_Duration = 0;
  seconds_passed++;
  vss_pulses = 0;
  TCNT1 = 3036;
}
</code></pre>
 38721: <p>Your timer configuration is ok so far. Never the less I recommend using the <code>TIMER1_COMPA_vect</code> interrupt vector instead of <code>TIMER1_OVF_vect</code> which has the advantage of better readability (in my oppinion) and <code>TCNT1</code> doesn't have to be reset in the ISR (instead, OCR1A is just initialized with 62500 once in <code>setup()</code>). Therefore, TIMER0 has to run in CTC (clear timer on compare match) mode and <code>WGM12</code> has to be set in TCCR1B.</p>

<p>The reason why <code>seconds_passed</code> is incremented twice is because you <code>Serial.print(seconds_passed++)</code> in <code>instantSpeed()</code>. Printing on the serial interface is damn slow and using it in an ISR is a no-go!</p>
