I want to make a timercounter with millis function. I wrote a code but ıt does not count 10seconds barely. There is sth wrong but ı could not see. My aim after pushing button, 10seconds later it must turn off. But ıt turns off not certainly 10s, sometimes 5-8-9 it is changable.
#define button 2
#define relay_drive 13
unsigned long current_time , last_time=0 ;
unsigned long _10scounter;
int _10s_Flag;
void setup()
{
pinMode(button, INPUT);
pinMode(relay_drive, OUTPUT);
}
void loop()
{
milis_interrupt();
if (!digitalRead(button)) {digitalWrite(relay_drive, HIGH); _10s_Flag = 0;} //pull up res
if(_10s_Flag & digitalRead(relay_drive))
{
_10s_Flag = 0;
digitalWrite(relay_drive, LOW);
}
}
void milis_interrupt(){
current_time= millis(); //millis
if(current_time>last_time){
if(_10scounter>=10000) {_10scounter=0; _10s_Flag = 1;}
if(_10scounter<10000) _10scounter++;
last_time = current_time;
}
}
Could you help me with my code, dear friends.
This line:
will most always be true.
What you want is something like:
which checks that the difference in times has exceeded your limit of 10 secs onds.
/regards