Timer1 Problem causing unexpected behavior

Hello everyone,

I'm new to this forum but have been coding simple programs for a while on PIC and Arduino, however have only used basic functions and libraries for most applications. This time it is an unlock system system with an RF remote and different combinations of buttons to do everything. Everything worked very well, till I decided to have an auto power off feature if I unlocked the unit but didnt power it up within a few seconds(or Minutes). So read a lot about Timers and decided to use timer1 which is causing unexpected results as of now. If I comment the new bits out, code works perfectly. I'm posting only the timer bits of my code so somebody could help me out. All I need is a time out for one particular if loop in the receiver unit but at the same time the code should be running to accept new RF commands without freezing the loop.

#include "TimerOne.h"

/*All my variables and IO pins are defined here */

volatile unsigned long system_clock = 0; //Timer clock Reset
volatile unsigned long power_timer = 0; //Power Timer Reset

void setup(void) 
{
       /*All Pinmodes defined properly
       ....
       ....
       */

       Timer1.initialize(1000000);      // initialize timer1, and set a 1 second period
       Timer1.attachInterrupt(clock);  // attaches callback() as a timer overflow interrupt

}
void clock(void) //1Hz clock Signal for Timer
{
        system_clock++;
}
void loop(void)
{
        /*Reads input pins from Remote and executes a bunch of if statements
          .....
          .....
         */

        noInterrupts();
        power_timer = system_clock;
        if (open_relay = HIGH and machine_run == 0 and power_timer == 10) //Timeout on No Start 
        {
             open_relay = LOW; //Turn relay off
             digitalWrite(open_relay_pin, open_relay);
             delay(200);
         }
         interrupts();
 }

Anything which is supposed to run after this stage does not operate or respond. I think something is not implemented properly in my Timer1 please help. If I delete this part, everything is robust and reliable. Thank you.

Everything worked very well, till I decided to have an auto power off feature if I unlocked the unit but didnt power it up within a few seconds(or Minutes). So read a lot about Timers

Sorry, but timers are the wrong approach. Suppose your daughter went out on a date, and said she'd be home at ten. Would you set a timer, and start fretting when the timer went off? Of course not. You have a watch, so setting a timer would not be necessary.

The Arduino has a watch of sorts - millis().

        if (open_relay = HIGH and machine_run == 0 and power_timer == 10) //Timeout on No Start

Assigning HIGH to open_relay is certainly odd...

        noInterrupts();
        power_timer = system_clock;
        if (open_relay = HIGH and machine_run == 0 and power_timer == 10) //Timeout on No Start 
        {
             open_relay = LOW; //Turn relay off
             digitalWrite(open_relay_pin, open_relay);
             delay(200);
         }
         interrupts();

Calling delay() with interrupts disabled is a colossally bad idea.

Don't get me wrong, I use the the delay in abundance when needed :slight_smile: But if I use it here, then till my delay is done, my remote wont do anything. I do not want the code to stop at all and that is why I used the timer. It has to go through the multiple if statements and at any point it matches the timer, I want it to run the above if loop.

Also, yes I forgot that delay depends on timer0 but deleting the disable/enable interrupt nor moving it before running the delay made no difference to the problem :frowning:

I do not want the code to stop at all and that is why I used the timer.

But, it's not the right approach. Suppose you are riding a merry-go-round, and each time around, you can see a clock. You need to get off 3 minutes after you get on. You don't need a timer. Simply note the time when you got on, and, each time around, see if you've been on long enough. The blink without delay example shows how to use millis() to accomplish what you need to do.