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.