timer 1 start kills varialble

Hi

unsigned long PollStart     = 0;
unsigned long PollTimeout   = 3000;

Loop() is

  if (millis() > (PollStart + PollTimeout)) {
    
    if(gsm.CallAvailable(IncomingPhone)){
      gsm.CallEnd();
      CheckNum(IncomingPhone); 
    }

    PollStart = millis();  
  }

CheckNum returns

  digitalWrite(Relay1Pin, !digitalRead(Relay1Pin));
  digitalWrite(RelayLedPin, digitalRead(Relay1Pin));
  timer1_start();

timer1_start() is

void timer1_start(void) {
  TIFR |= (1 << TOV1); 
  TCNT1 = isr1_counter;
  TIMSK |= (1 << TOIE1);       
}

after timer1_start() always PollStart becomes 11587770 so “loops” nothing if PollStart placed first

  if (millis() > (PollStart + PollTimeout)) {
    PollStart = millis(); 
    if(gsm.CallAvailable(IncomingPhone)){
      gsm.CallEnd();
      CheckNum(IncomingPhone); 
    }
     
  }

How to fix it?

please post 1 complete sketch that demonstrates this issue. It is much more helpful than a series of chunks of your code.

What does the Timer1 overflow interrupt ISR do?

Timer 1 cpp (ATMega16)

/ ************************** TIMER 1 ***************************
void (*isr1)();
volatile uint16_t isr1_counter;

void timer1_ISR(void (*isr)()) {
	isr1 = *isr;

void timer1_setPeriod(uint32_t time) {
  TCCR1A = 0;
  TCCR1B = 0;

  time = 65536 - time/139;                    
  TCCR1B = ((1 << CS12) | (1 << CS10));       
  TIMSK &= ~(1 << TOIE1); 
  isr1_counter = time;
  TCNT1 = 0; 
}

void timer1_start(void) {
  TIFR |= (1 << TOV1); 
  TCNT1 = isr1_counter; 
  TIMSK |= (1 << TOIE1);        
}



void timer1_stop(void) {
  TIMSK &= ~(1 << TOIE1);	
}


ISR(TIMER1_OVF_vect) {
  timer1_stop();
  TCNT1 = isr1_counter;
  (*isr1)();
}

init

    timer1_setPeriod( Settings.RelayOnTime * 1000000 /2 );
    timer1_ISR(handler);

interruption

void handler() {
  digitalWrite(Relay1Pin, LOW);
  digitalWrite(RelayLedPin, LOW);
}

blh64:
please post 1 complete sketch.

5 files, 16 Kb flashed size)

spballiance:

please post 1 complete sketch.

5 files, 16 Kb flashed size)

Then please write a short program in a single file that illustrates your problem.

When you run into a problem it is always best to figure it out with the shortest possble program that has no extraneous stuff.

...R

bug localised:
before timer there is strcpy(t, mess.c_str()); (t is message buffer)
After it Pollstart crashes because len t < len mess

@spballiance, is there some reason you can't follow simple instructions? You've been requested (twice) to post a complete code that compiles and demonstrates the problem. Trying to find the problem by looking at disjointed code snippets is extremely difficult. If your code is too large and contains unrelated clutter, post an MCVE. This is the smallest possible code that actually compiles and demonstrates the problem you're seeing.

it is very strange that strcpy does not contain a length check

before refactoring to strcpy everything works perfect

void AddStrToChar(char *t, String &s){
      uint16_t i, j, k;
      j = s.length();
      k = sizeof(t);
      for (i=0; (i<j) && (i<k); i++) {
        t[i] = s.charAt(i);
      }
      t[(i)]=0;
}

spballiance:
it is very strange that strcpy does not contain a length check

No. How could it?

spballiance:
before refactoring to strcpy everything works perfect

void AddStrToChar(char *t, String &s){

uint16_t i, j, k;
     j = s.length();
     k = sizeof(t);
     for (i=0; (i<j) && (i<k); i++) {
       t[i] = s.charAt(i);
     }
     t[(i)]=0;
}

Except that “sizeof t” is always 2 (sizeof char *) so you never copied more than 2 characters to your buffer.
Use strncpy() to protect against overflowing your buffer.