Hello. I have a problem with my code.I have a lcd and I want to blink a character every 0.5 seconds BUT I don't want to use delay.I want to use MILLIS function.Everything is ok ,but many times the character lag about 0.5-1 second MORE than 0.5 seconds.I don't know if the problem is in the code or not ,but I want your help!!!
Then,I would like to know if exists another way...
CODE:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
unsigned long previousMillis = 0;
bool real = true;
byte A[8] =
{
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111
};
void setup()
{
lcd.begin(20, 4);
}
void characters_for_AI()
{
lcd.createChar(0, A);
lcd.setCursor(5, 3);
lcd.write(byte(0));
lcd.setCursor(6, 2);
lcd.write(byte(0));
lcd.setCursor(7, 1);
lcd.write(byte(0));
lcd.setCursor(8, 0);
lcd.write(byte(0));
lcd.setCursor(9, 0);
lcd.write(byte(0));
lcd.setCursor(10, 1);
lcd.write(byte(0));
lcd.setCursor(11, 2);
lcd.write(byte(0));
lcd.setCursor(12, 3);
lcd.write(byte(0));
lcd.setCursor(7, 2);
lcd.write(byte(0));
lcd.setCursor(8, 2);
lcd.write(byte(0));
lcd.setCursor(9, 2);
lcd.write(byte(0));
lcd.setCursor(10, 2);
lcd.write(byte(0));
lcd.createChar(0, A);
lcd.setCursor(15, 0);
lcd.write(byte(0));
lcd.setCursor(15, 1);
lcd.write(byte(0));
lcd.setCursor(15, 2);
lcd.write(byte(0));
lcd.setCursor(15, 3);
lcd.write(byte(0));
}
void function()
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= 500)
{
previousMillis = currentMillis;
if (real)
{
characters_for_AI();
}
else
{
lcd.clear();
}
}
real = !real;
}
void loop()
{
function();
}