Making two counters run alternately using Arduino Uno and 16x2 i2c lcd

  1. I want to implement two counters alternately. The first one is the Auto Time which is of max 10 seconds (user has the option of setting anything between 0 - 10 seconds).

  2. Second is the Timer Count which takes 'count' as well as the 'interval' required to run the counter via keypad (user can input any count between 0 - 99,999 & an interval between 0- 200 milliseconds)

  3. The Auto Time works individually. Also the Timer Count works individually.

  4. The combination of both is what I need to achieve.

Code for Auto Time:

#include <EEPROM.h>
#include <Wire.h>
#include <hd44780.h>                       // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header

hd44780_I2Cexp lcd; // declare lcd object: auto locate & config exapander chip
// LCD geometry
const byte LCD_COLS = 16;
const byte LCD_ROWS = 2;

unsigned long startTime = 0;
unsigned int seconds = 0;
//unsigned long seconds = 0;
unsigned int targetTime;

const int ledPin = 13;

void setup()
{
  lcd.begin(LCD_COLS, LCD_ROWS);
  EEPROM.get(50, targetTime);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
}

void loop()
{
  getSeconds();
}

void getSeconds()
{
  //unsigned long now = millis();
  unsigned long now = micros();
  static bool timeDoneFlag = false ;

  if (now >= (startTime))
  {
    if (!timeDoneFlag)
    {
      //startTime = startTime + 1000; // using millis
      startTime = startTime + 1000000L;  // using micros
      seconds = seconds + 1;
    }
    if (seconds > targetTime)
    {
      seconds = 0;
      timeDoneFlag = true;
      digitalWrite(ledPin, HIGH);
      delay(300);
      digitalWrite(ledPin, LOW);
    }
    else
    {
      dispAutoTime();
    }
  }
}

void dispAutoTime()
{
  char displayText[17] = "";
  snprintf(displayText, sizeof(displayText), "AutoTime: %2d sec", seconds);
  lcd.setCursor(0, 1);
  lcd.print(displayText);
}
[code]


Code for Timer Count:

[code]
#include <EEPROM.h>
#include <Wire.h>
#include <hd44780.h>                       // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header

hd44780_I2Cexp lcd; // declare lcd object: auto locate & config exapander chip
// LCD geometry
const int LCD_COLS = 16;
const int LCD_ROWS = 2;

unsigned long now;
unsigned long prevMillis;

unsigned long TimeCount = 0;
unsigned long targetCount;
unsigned long targetInterval;

void setup()
{
  lcd.begin(LCD_COLS, LCD_ROWS);
  EEPROM.get(0, targetCount);
  EEPROM.get(100, targetInterval);
}

void loop()
{
  countTime();
}

void countTime()
{
  static bool countDoneFlag = false ;
  unsigned long now = millis();

  while ((now - prevMillis) >= targetInterval)
  {
    if (!countDoneFlag)
    {
      TimeCount++;
    }
    prevMillis += targetInterval;
  }
  if (TimeCount >= targetCount)
  {
    TimeCount = 0;
    countDoneFlag = true;
  }
  else
  {
    dispTime();
    lcd.setCursor(1, 1);
    lcd.print("Set Count:");
    lcd.print(targetCount);
  }
}

void dispTime()
{
  char displayText[17] = "";
  snprintf(displayText, sizeof(displayText), "TimerCount:%5lu", TimeCount);
  lcd.setCursor(0, 0);
  lcd.print(displayText);
}

** The count set by the user will be displayed on the second line while the counter runs on the first.**

The combined code:

#include <EEPROM.h>
#include <Wire.h>
#include <hd44780.h>                       // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header

hd44780_I2Cexp lcd; // declare lcd object: auto locate & config exapander chip
// LCD geometry
const byte LCD_COLS = 16;
const byte LCD_ROWS = 2;

unsigned long prevMillis;

volatile unsigned long TimeCount = 0;
unsigned long targetCount;
unsigned long targetInterval;

volatile bool beginCounting = false;
volatile bool countDoneFlag = false;

unsigned long startTime = 0;
unsigned int seconds = 0;
unsigned int targetTime;

bool initiateAutotimer = true;
bool timeDoneFlag = false ;
const int ledPin = 12;

void setup()
{
  lcd.begin(LCD_COLS, LCD_ROWS);
  EEPROM.get(50, targetTime);
  EEPROM.get(0, targetCount);
  EEPROM.get(100, targetInterval);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
}

void loop()
{
  getSeconds();
  countTime();
}

void getSeconds()
{
  unsigned long now2 = micros();

  if (now2 >= (startTime))
  {
    if (initiateAutotimer && !timeDoneFlag)
    {
      startTime = startTime + 1000000L;  // using micros
      seconds = seconds + 1;
    }
    if (seconds > targetTime)
    {
      seconds = 0;
      initiateAutotimer = false;
      timeDoneFlag = true;
      beginCounting = true;
      countDoneFlag = false;
      digitalWrite(ledPin, HIGH);
      delay(300);
      digitalWrite(ledPin, LOW);
    }
    else
    {
      dispAutoTime();
    }
  }
}

void dispAutoTime()
{
  char displayText[17] = "";
  snprintf(displayText, sizeof(displayText), "AutoTime: %2d sec", seconds);
  lcd.setCursor(0, 1);
  lcd.print(displayText);
}

void countTime()
{
  unsigned long now1 = millis();

  while ((now1 - prevMillis) >= targetInterval)
  {
    if (beginCounting && !countDoneFlag)
    {
      TimeCount++;
    }
    prevMillis += targetInterval;
  }
  if (TimeCount >= targetCount)
  {
    TimeCount = 0;
    countDoneFlag = true;
    beginCounting = false;
    initiateAutotimer = true;
    timeDoneFlag = false;
  }
  else
  {
    dispTime();
    lcd.setCursor(1, 1);
    lcd.print("Set Count:");
    lcd.print(targetCount);
  }
}

void dispTime()
{
  char displayText[17] = "";
  snprintf(displayText, sizeof(displayText), "TimerCount:%5lu", TimeCount);
  lcd.setCursor(0, 0);
  lcd.print(displayText);
}

** Problems encountered: **

Auto time runs for whatever seconds user has set for the first time when the controller is powered on but is not displayed on the second line. Instead Set Count is displayed prominently while Auto Time runs in the background and is faintly displayed.[/code]
[/code]

Take a look at Several things at the same time

Don't use delay() in either portion of the program and why use the memory hungry snprintf() function when you could just print what you want directly to the LCD ?

UKHeliBob:
Don't use delay() in either portion of the program

Done, I got rid of delay.

UKHeliBob:
and why use the memory hungry snprintf() function when you could just print what you want directly to the LCD ?

Since I need to print incrementing values. How to do it otherwise?

Done, I got rid of delay.

OK. Let's see both programs now

I need to print incrementing values. How to do it otherwise?

  lcd.setCursor(0, 0);
  lcd.print("TimerCount:");
  lcd.print(TimeCount);

Even better, just print the text once in setup() and only print the value of TimeCount when it changes.

Without delay..

#include <EEPROM.h>
#include <Wire.h>
#include <hd44780.h>                       // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header

hd44780_I2Cexp lcd; // declare lcd object: auto locate & config exapander chip
// LCD geometry
const byte LCD_COLS = 16;
const byte LCD_ROWS = 2;

unsigned long prevMillis;

volatile unsigned long TimeCount = 0;
unsigned long targetCount;
unsigned long targetInterval;

volatile bool beginCounting = false;
volatile bool countDoneFlag = false;

unsigned long startTime = 0;
unsigned int seconds = 0;
unsigned int targetTime;

bool initiateAutotimer = true;
bool timeDoneFlag = false ;

void setup()
{
  lcd.begin(LCD_COLS, LCD_ROWS);
  EEPROM.get(50, targetTime);
  EEPROM.get(0, targetCount);
  EEPROM.get(100, targetInterval);
}

void loop()
{
  getSeconds();
  countTime();
}

void getSeconds()
{
  unsigned long now2 = micros();

  if (now2 >= (startTime))
  {
    if (initiateAutotimer && !timeDoneFlag)
    {
      startTime = startTime + 1000000L;  // using micros
      seconds = seconds + 1;
    }
    if (seconds > targetTime)
    {
      seconds = 0;
      initiateAutotimer = false;
      timeDoneFlag = true;
      beginCounting = true;
      countDoneFlag = false;
    }
    else
    {
      dispAutoTime();
    }
  }
}

void dispAutoTime()
{
  char displayText[17] = "";
  snprintf(displayText, sizeof(displayText), "AutoTime: %2d sec", seconds);
  lcd.setCursor(0, 1);
  lcd.print(displayText);
}

void countTime()
{
  unsigned long now1 = millis();

  while ((now1 - prevMillis) >= targetInterval)
  {
    if (beginCounting && !countDoneFlag)
    {
      TimeCount++;
    }
    prevMillis += targetInterval;
  }
  if (TimeCount >= targetCount)
  {
    TimeCount = 0;
    countDoneFlag = true;
    beginCounting = false;
    initiateAutotimer = true;
    timeDoneFlag = false;
  }
  else
  {
    dispTime();
    lcd.setCursor(1, 1);
    lcd.print("Set Count:");
    lcd.print(targetCount);
  }
}

void dispTime()
{
  char displayText[17] = "";
  snprintf(displayText, sizeof(displayText), "TimerCount:%5lu", TimeCount);
  lcd.setCursor(0, 0);
  lcd.print(displayText);
}

For now can we keep the issue with displaying using sprintf aside?

I'd like to solve the problem of displaying the Auto time. The Auto Time and the Set Count: xyz on the second line are interfering with each other.

Could you help me with that?

  while ((now1 - prevMillis) >= targetInterval)
  {
    if (beginCounting && !countDoneFlag)
    {
      TimeCount++;
    }
    prevMillis += targetInterval;
  }

That blocks execution of the loop() function in the same way that delay() does.

You need to set the start time from millis() when the start action happens, then check each time through loop() whether the required period has passed. If so, do what you need. If not go round loop() again until it does. In loop() you can put any other non blocking code that you require to do other things, including a completely different check on another period if that is what you need to do.

UKHeliBob:

  while ((now1 - prevMillis) >= targetInterval)

{
    if (beginCounting && !countDoneFlag)
    {
      TimeCount++;
    }
    prevMillis += targetInterval;
  }



That blocks execution of the loop() function in the same way that delay() does.

Just read in another post that delay as well as while both block execution.

UKHeliBob:
You need to set the start time from millis() when the start action happens

So that is when the flag for either Auto Time or the Timer gets set to true right?

UKHeliBob:
In loop() you can put any other non blocking code that you require to do other things, including a completely different check on another period if that is what you need to do.

The Auto Time counting loop can be included in the Timer loop? The Auto time uses micros and not millis.

UKHeliBob:

  while ((now1 - prevMillis) >= targetInterval)

{
    if (beginCounting && !countDoneFlag)
    {
      TimeCount++;
    }
    prevMillis += targetInterval;
  }

will using an 'if' instead of while eliminate blocking? But when I use 'if' the counting slows down I don't know why. tried to find out the reason and finally settled on using the 'while'. But now it is leading to blocking. Please suggest something.

Thanks in advance.

Post the whole program that uses if

An example for you to play with

unsigned long A_startTime;
unsigned long B_startTime;
unsigned long A_period = 5000;
unsigned long B_period = 4000;
unsigned long currentTime;
boolean A_running = true;
boolean B_running = true;

void setup()
{
  Serial.begin(115200);
  showStates();
}

void loop()
{
  char inChar = 'X';
  currentTime = millis();
  if (Serial.available())
  {
    inChar = toupper(Serial.read());
  }
  if (inChar == 'A')
  {
    A_running = !A_running;
    showStates();
  }
  else if (inChar == 'B')
  {
    B_running = !B_running;
    showStates();
  }
  if (A_running)  //if A timing taking place
  {
    //check if A period has elapsed
    if (currentTime - A_startTime > A_period)
    {
      Serial.println("A period ended.  Restarting A");
      A_startTime = currentTime;
    }
  }
  if (B_running)  //if B timing taking place
  {
    //check if B period has elapsed
    if (currentTime - B_startTime > B_period)
    {
      Serial.println("B period ended.  Restarting B");
      B_startTime = currentTime;
    }
  }
}

void showStates()
{
  Serial.println();
  Serial.print("A : ");
  Serial.println(A_running ? "running" : "stopped");
  Serial.print("B : ");
  Serial.println(B_running ? "running" : "stopped");
  Serial.println();
}

thanks for the example Bob, i shall try it out. I've tried the following.

#include <EEPROM.h>
#include <Wire.h>
#include <hd44780.h>                       // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header

hd44780_I2Cexp lcd; // declare lcd object: auto locate & config exapander chip
// LCD geometry
const byte LCD_COLS = 16;
const byte LCD_ROWS = 2;

unsigned long startTime1 = 0;
unsigned long TimeCount = 0;
unsigned long targetCount;
unsigned long targetInterval;

void setup()
{
  lcd.begin(LCD_COLS, LCD_ROWS);
  EEPROM.get(0, targetCount);
  EEPROM.get(100, targetInterval);
}

void loop()
{
  countTime();
}

void countTime()
{
  unsigned long now = millis();
  static bool countDoneFlag = false ;

  if (now >= (startTime1))
  {
    if (!countDoneFlag)
    {

      TimeCount = TimeCount + 1;
    }
    startTime1 = startTime1 + targetInterval;
  }
  if (TimeCount >= targetCount)
  {
    TimeCount = 0;
    countDoneFlag = true;
  }
  else
  {
    dispTime();
    lcd.setCursor(1, 1);
    lcd.print("Set Count:");
    lcd.print(targetCount);
  }
}

void dispTime()
{
  char displayText[17] = "";
  snprintf(displayText, sizeof(displayText), "TimerCount:%5lu", TimeCount);
  lcd.setCursor(0, 0);
  lcd.print(displayText);
}

The if is causing the counting to slow down but when I when i use while it fastens up. The 'while' will cause blocking in the combined code as you pointed out.

Am I implementing millis() incorrectly?

GautamD:
Am I implementing millis() incorrectly?

You have not implemented millis() timing in the recommended way, which is to use subtraction to determine whether the timing period has elapsed

if (timeNow - startTime >= period)
{
  //do something
}

Using subtraction prevents problems when millis() rolls over to zero.

Does the program you posted do what you want ? If not, then what is wrong with it ?

If there is a problem can I suggest that you create a version that uses serial output to make it easier to test for those of us, like me, who do not have an LCD screen attached to our test systems.

Hi Bob, I've now implemented millis() with subtraction. Also the LCD part has been replaced with serial print. On the serial monitor when 'if' is used the counters runs slowly & with a while it runs faster.

#include <EEPROM.h>

unsigned long startTime1 = 0;
unsigned long TimeCount = 0;
unsigned long targetCount;
unsigned long targetInterval;

void setup()
{Serial.begin(9600);
  EEPROM.get(0, targetCount);
  EEPROM.get(100, targetInterval);
  Serial.print("Timer Count:");
}

void loop()
{
  countTime();
}

void countTime()
{
  unsigned long now = millis();
  static bool countDoneFlag = false ;

  if ((now - startTime1) >= (targetInterval))
  {
    if (!countDoneFlag)
    {
      TimeCount = TimeCount + 1;
    }
    startTime1 = startTime1 + targetInterval;
  }
  if (TimeCount >= targetCount)
  {
    TimeCount = 0;
    countDoneFlag = true;
  }
  else
  {
    dispTime();
  }
}

void dispTime()
{
Serial.println(TimeCount);
}

Thanks for the test program. It makes things easier to test when it uses just the Arduino board.

Generally using while is a bad idea in a time sensitive program because it prevents loop() from running freely

Here is my version of your program. Note the slight change to the logic, the comments that I have added and the change to a function name to reflect what it does. I have hard coded the values of targetCount and targetInterval as I don't have them saved to EEPROM. Are you sure that you have the values saved correctly ?

#include <EEPROM.h>

unsigned long startTime1 = 0;
unsigned long TimeCount = 0;
unsigned long targetCount;
unsigned long targetInterval;

void setup()
{
  Serial.begin(115200);
  EEPROM.get(0, targetCount);
  EEPROM.get(100, targetInterval);
  targetCount = 10;
  targetInterval = 1000;
  Serial.print("Target count : ");
  Serial.println(targetCount);
  Serial.print("Interval : ");
  Serial.println(targetInterval);
}

void loop()
{
  countTime();
}

void countTime()
{
  unsigned long now = millis();
  static bool countDoneFlag = false ;
  if (!countDoneFlag) //only bother to check the time period if we are actually timing
  {
    if ((now - startTime1) >= targetInterval)
    {
      startTime1 = startTime1 + targetInterval;
      TimeCount++;
      dispCount(); //update the display only when the value changes
    }
    if (TimeCount >= targetCount)
    {
      TimeCount = 0;
      countDoneFlag = true;
      Serial.println("Counting finished");
    }
  }
}

void dispCount()
{
  Serial.print("Current count : ");
  Serial.println(TimeCount);
}

Thanks for the code Bob. I tried it on the serial monitor. You have set the count to 10 and the interval to 1000 ms. So counting takes 10 seconds to complete. But when I display it on the lcd, counting slows down. The count I'm reading from the memory is 10,000 while the interval is 1 millisecond. Really confused why such behaviour is persisting.

Here is the code with count displayed on the LCD.

#include <EEPROM.h>
#include <Wire.h>
#include <hd44780.h>                       // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header

hd44780_I2Cexp lcd; // declare lcd object: auto locate & config exapander chip
// LCD geometry
const byte LCD_COLS = 16;
const byte LCD_ROWS = 2;

unsigned long startTime1 = 0;
unsigned long TimeCount = 0;
unsigned long targetCount;
unsigned long targetInterval;

void setup()
{
  lcd.begin(LCD_COLS, LCD_ROWS);
  EEPROM.get(0, targetCount);
  EEPROM.get(100, targetInterval);
}

void loop()
{
  countTime();
}

void countTime()
{
  unsigned long now = millis();
  static bool countDoneFlag = false ;

  if (!countDoneFlag) //only bother to check the time period if we are actually timing
  {
    if ((now - startTime1) >= targetInterval)
    {
      startTime1 = startTime1 + targetInterval;
      TimeCount++;
      dispTimer();  //update the display only when the value changes
    }
    if (TimeCount >= targetCount)
    {
      TimeCount = 0;
      countDoneFlag = true;
    }
  }
}

void dispTimer()
{
  char displayText[17] = "";
  snprintf(displayText, sizeof(displayText), "TimerCount:%5lu", TimeCount);
  lcd.setCursor(0, 0);
  lcd.print(displayText);
  lcd.setCursor(1, 1);
  lcd.print("Set Count:");
  lcd.print(targetCount);
}

Is the use of sprintf causing the lag?

I even tried using 'while' in the above code but that didn't cause the counter to run faster.

Also what I believe is, as the count becomes larger and the interval becomes smaller, that is probably causing the lag in my case.

The count I'm reading from the memory is 10,000 while the interval is 1 millisecond.

If the interval is 1 millisecond then using millis() is going to be a problem as, by definition, it has a resolution of 1 millisecond assuming that nothing else is going on in the sketch, which it is.. Substitute micros() for millis() and adjust the parameter accordingly.

Also, as previously mentioned, why use snprint() for TimeCount when you can simply print the value to the LCD as you do for targetCount ?

UKHeliBob:
If the interval is 1 millisecond then using millis() is going to be a problem as, by definition, it has a resolution of 1 millisecond assuming that nothing else is going on in the sketch, which it is.. Substitute micros() for millis() and adjust the parameter accordingly.

I'll have a go at micros() for counting. But the user is going to set the interval in milliseconds & within a range of 0- 200 milliseconds. Will I have to make any changes to that code as well. Following is the code for setting the interval and storing it.

#include <EEPROM.h>
#include <Wire.h>
#include <hd44780.h>                       // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header

hd44780_I2Cexp lcd; // declare lcd object: auto locate & config exapander chip

// LCD geometry
const byte LCD_COLS = 16;
const byte LCD_ROWS = 2;

#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] =
{
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {11, 10, 9, 8}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {7, 6, 5, 4}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

bool acquireInterval = true;
bool IntervalfromMem = true;
unsigned long interval;
const byte entryMaxSize2 = 4;
static char digits2[entryMaxSize2 + 1];
static byte index2;

void setup()
{
  Serial.begin(115200);
  lcd.begin(LCD_COLS, LCD_ROWS);
}

void readSavedInterval()
{
  EEPROM.get(100, interval); //read interval from mem
  lcd.clear();
  lcd.setCursor(1, 0);
  lcd.print("Set Interval:");
  lcd.setCursor(1, 1);
  lcd.print(interval); //print the interval
  lcd.print(" millisec");
  checkInterval();
}

void checkInterval()
{
  if (interval != 0)
  {
    IntervalfromMem = true;
  }
  else
  {
    IntervalfromMem = false;
  }
}

void loop()
{
  if (keypad.getKeys()) // check for keypad activity
  {
    // we won't handle multiple keypresses, just single ones, so just key index 0
    const byte key = keypad.key[0].kchar;
    const byte state = keypad.key[0].kstate; // IDLE, PRESSED, HOLD, RELEASED
    switch (key)
    {
      case '*': initializeInterval(); break;
      case 'D': setInterval(); break;
      default: getNewInterval(state, key);
    }
  }
}

void initializeInterval()
{
  readSavedInterval();
  index2 = 0; // reset the interval
  acquireInterval = true;
}

unsigned long tempinterval;             // temporary interval created by key entry
void getNewInterval(const byte state, char key)
{
  if (state == PRESSED)
  {
    if (acquireInterval)
    {
      IntervalfromMem = false;
      // if not 4 characters yet
      if (index2 < entryMaxSize2)
      {
        //add key to userinput array and increment counter
        if ( key >= '0' && key <= '9') // key is of type char and has a value between 0 and 9 so do something with it.
        {
          digits2[index2++] = key;
          digits2[index2] = '\0';
          tempinterval = atol(digits2);
          if (tempinterval > 1000)
          {
            invalidInterval();
          }
          else
          {
            lcd.clear();
            lcd.setCursor(1, 0);
            lcd.print("Set Interval:");
            char displayText[14] = "";
            snprintf(displayText, sizeof(displayText), "%4lu millisec", tempinterval);
            lcd.setCursor(1, 1);
            lcd.print(displayText);
          }
        }
      }
      else
      {
        Intervalwarning();
      }
    }
  }
}

void setInterval()
{
  if (IntervalfromMem)
  {
    lcd.clear();
    lcd.setCursor(1, 0);
    lcd.print("Interval Set: ");
    lcd.setCursor(1, 1);
    lcd.print(interval); // print the count
    lcd.print(" millisec");
    IntervalfromMem = false;
    acquireInterval = false;
  }
  else if (acquireInterval)
  {
    if (tempinterval == 0)
    {
      invalidInterval();
    }
    else
    {
      lcd.clear();
      lcd.setCursor(1, 0);
      lcd.print("Interval Set: ");
      lcd.setCursor(1, 1);
      lcd.print(tempinterval);
      lcd.print(" millisec");
      saveInterval();
      acquireInterval = false;
    }
  }
}

void saveInterval()
{
  unsigned long interval = atol(digits2);
  EEPROM.put(100, interval); // <<<<< save to EEPROM here
  Serial.print(interval);
}

void invalidInterval()
{
  lcd.clear();
  lcd.setCursor(1, 0);
  lcd.print("Invalid Interval!!");
  lcd.setCursor(1, 1);
  lcd.print("Press *"); // suggesting the user to enter the interval again
  IntervalfromMem = false;
  acquireInterval = false;
}

void Intervalwarning()
{
  lcd.clear();
  lcd.setCursor(1, 0);
  lcd.print("4 Digits Only!!"); // warning for the user if more than permitted digits are entered
  lcd.setCursor(1, 1);
  lcd.print("Press *"); // suggesting the user to enter the interval again
  IntervalfromMem = false;
  acquireInterval = false;
}

And in the Timer code I'm reading the stored interval into the targetInterval variable.

UKHeliBob:
Also, as previously mentioned, why use snprint() for TimeCount when you can simply print the value to the LCD as you do for targetCount ?

Yes I'll be making that change as well. Currently I was just trying to concentrate on getting the timer working.

the user is going to set the interval in milliseconds & within a range of 0- 200 milliseconds. Will I have to make any changes to that code as well.

No need to change what the user enters or how they enter it as long as you get a number in milliseconds from then. Then, behind the scenes you turn that into microseconds for the purpose of timing. Note that when using micros() for timing you must use subtraction to determine when the period ends because it rolls over to zero much faster than millis(). Note also that micros() has a resolution of 4 microseconds and increments in steps of 4 rather than 1