-
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).
-
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)
-
The Auto Time works individually. Also the Timer Count works individually.
-
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]