help for delay methods

Project description:
I am working on a hobby project in which I had made a timer circuit using standalone arduino IC. In this project there are two push buttons among which one is for setting minutes and the other one is for starting timer after setting minutes. And its basic function is whatever time we will set in minutes, a relay connecting to one of the micro controller pin will be energized and particular device connected to that relay will be ON for defined minutes.

Problem:
So problem is I am not able to get accurate timing to switch OFF relay. I am currently using method of checking time by millis function and comparing it with entered (value601000) milliseconds.

What can be done to get accurate timings ?

Thanks in advance !

Here's my code:

#include<LiquidCrystal.h>
LiquidCrystal lcd(A5,A4,A3,A2,A1,A0);

unsigned long currentMilli = 0; 
unsigned long waitIntervalMilli = 0;
unsigned long count = 0;

byte mark=0;
//float minutes;

const int set_minutes = 8;
const int start_timer = 7;
const int device_pin = 11;

void setup() {
 // put your setup code here, to run once:
initializeInterrupts();
lcd.begin(16,2);
pinMode(set_minutes,INPUT);
pinMode(start_timer,INPUT);
pinMode(device_pin,OUTPUT);
}
void initializeInterrupts()
{
 cli();
 PCICR = 0x05 ;
 PCMSK0 = 0b00000001;
 PCMSK2 = 0b10000000;
 sei();
}
ISR(PCINT0_vect)
{
 if(digitalRead(set_minutes)== HIGH && mark == 0)
 {
   count = count + 5;//counting in minutes
   if(count > 360)
   {
     count = 0;
   }
   lcd.clear();
   lcd.print("Minutes: ");
   lcd.setCursor(0,1);
   lcd.print(count);
   waitIntervalMilli = count * 60 * 1000; //converting entered value to milliseconds  
 }
}
ISR(PCINT2_vect)
{
 if(count != 0 && digitalRead(start_timer) == HIGH)
 {  
   lcd.clear();
   lcd.setCursor(0,0);
   lcd.print("Minutes Left:");
   digitalWrite(device_pin,HIGH);
   currentMilli = 0;
   mark = 1;
 }
}
void loop() {   
 currentMilli = millis();
 if((waitIntervalMilli-currentMilli) <= 0 && mark == 1)
 {
   mark = 0;
   count = 0;
   digitalWrite(device_pin,LOW);
 }
}

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. If your browser doesn't show the posting toolbar then you can just manually add the code tags:
[code]`` [color=blue]// your code is here[/color] ``[/code]
Using code tags and other important information is explained in the How to use this forum post. Please read it.

What kind of accuracy are you wanting to get?

First of all thanks pert for replying and sorry I am new here didn't know about that... I edited it :slight_smile:

So the main problem is device connected to relay is getting OFF before time(like sometimes 5 seconds or 10 seconds or sometimes more)

I wanted to switch OFF that device according to the time entered.

You have all the wrong code inside your ISRs. Especially you should not be doing any printing. The ISR code should be something as simple as

ISR(PCINT0_vect)
{
    change0triggered = true;
}

and the rest of the code should be elsewhere.

But you really don't need interrupts at all to detect SLOOOOWWWW humans pressing buttons. Polling and digitalRead() will be plenty fast enough and much simpler.

Have a look at Several Things at a Time and at Planning and Implementing a Program

...R

your timer starts with reset of Arduino, not when you stop pressing the button. the sketch compares the interval with millis from the start of Arduino.

Ohh! thanks Juraj for pointing it out then i tried other way too like this

unsigned long previousMilli = 0;
unsigned long currentMilli = 0;
unsigned long interval = //whatever value there will be according to above code

void loop()
{
   currentMilli = millis();
   if((currentMilli - previousMilli) >= interval)
   {
        digitalWrite(device_pin,LOW);
        previousMilli = currentMilli;
   }
}

this way also if i set timer for one minute it switches off device before time

here are some values taken by me repeatedly (input time 1 minute and device stops at following time)

  1. 52 sec
  2. 26 sec
  3. 39 sec
  4. 46 sec
  5. 49 sec
  6. 49 sec
  7. 48 sec
  8. 49 sec
  9. 49 sec
    10.47 sec

I would save the start millis and then substract millis() - startMilis

Rest of the code is missing, so don't know what you're doing there.
Expecting something as simple as this:

unsigned long int previousMillis

void loop() {
   if((millis() - previousMillis) >= interval) {
        digitalWrite(device_pin, LOW);
        previousMillis = millis();
   }
}

void buttonPressed() {
  previousMillis = millis();
}