Can anyone please tell me why this does not work?
void BlinkColon()
{
static unsigned long ColonBlink;
while (millis() > ColonBlink + 1000)
{
lcd.setCursor(13, 0);
lcd.print(":");
ColonBlink = millis();
}
while (millis() > ColonBlink + 1000)
{
lcd.setCursor(13, 0);
lcd.print(" ");
ColonBlink = millis();
}
}
What i am trying to is to get time colon separator to blink without delay on my clock
Because you keep on updating ColonBlink in the while loop. Chances that millis() is greater than ColonBlink + 1000 are very very slim because ColonBlink is nearly the same as millis() when you test at the beginning of the while.
Tried this still does not work
void BlinkColon()
{
unsigned long ColonBlink = millis();
const long ColonDelay = 1000;
while (millis() - ColonBlink >= ColonDelay)
{
lcd.setCursor(13, 0);
lcd.print(":");
//ColonBlink = millis();
}
while (millis() - ColonBlink >= ColonDelay)
{
lcd.setCursor(13, 0);
lcd.print(" ");
}
}
something like this:
void setup() {
// put your setup code here, to run once:
}
void loop()
{
BlinkColon();
}
void BlinkColon()
{
static unsigned long ColonBlink = 0;
static bool blinkState = false;
if (millis() - ColonBlink >= 1000UL)
{
blinkState = -blinkState;
lcd.setCursor(13, 0);
lcd.print(blinkState ? ":" : " ");
ColonBlink = millis();
}
}
Between to the two while loops you need to 'reset' ColonBlink to the new time
while(...)
{
}
ColonBlink = millis();
while(...)
{
}
The way you have coded this does not have any benefits over a simple delay. If it's the intention to prevent blocking of the main loop where you call BlinkColon, you're not quite on track yet.
Try this untested non blocking version
void BlinkColon()
{
static unsigned long ColonBlink = millis();
static colonState = true;
static char colonChar = ':';
const long ColonDelay = 1000;
if (millis() - ColonBlink >= ColonDelay)
{
if (colonChar == ':')
{
colonChar == ' ';
}
else
{
colonChar == ':';
}
lcd.setCursor(13, 0);
lcd.print(colonChar);
ColonBlink = millis();
}
}
Thanks I will try all suggestions and update
UKHeliBob:
Try this untested non blocking version
const long ColonDelay = 1000;
if (millis() - ColonBlink >= ColonDelay)
{
if (colonChar == ':')
{
colonChar == ' ';
}
else
{
colonChar == ':';
}
but fix it a little first
I've been trying for ten minutes to blink my...oh...never mind.
Do you need this
static colonState = true;
I cant see where it being used
but fix it a little first
Pooh !
Corrected version
void BlinkColon()
{
static unsigned long ColonBlink = millis();
static char colonChar = ':';
const long ColonDelay = 1000;
if (millis() - ColonBlink >= ColonDelay)
{
if (colonChar == ':')
{
colonChar = ' ';
}
else
{
colonChar = ':';
}
lcd.setCursor(13, 0);
lcd.print(colonChar);
ColonBlink = millis();
}
}