Ok look at the attached example:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
int garagesensorpin =10;
int blpin =9;
// DEFINERE VARIABLER
// ------------------
// Kode for å få æ, ø, å, Æ, Ø, Å i display
byte Lae[8] = {B00000,B00000,B11010,B00101,B01111,B10100,B11111,B00000}; // æ
byte Loe[8] = {B00000,B00001,B01110,B10101,B10101,B01110,B10000,B00000}; // ø
byte Laa[8] = {B00100,B00000,B01110,B00001,B01111,B10001,B01111,B00000}; // å
byte Sae[8] = {B01111,B10100,B10100,B11110,B10100,B10100,B10111,B00000}; // Æ
byte Soe[8] = {B00001,B01110,B10011,B10101,B11001,B01110,B10000,B00000}; // Ø
byte Saa[8] = {B00100,B00000,B01110,B10001,B11111,B10001,B10001,B00000}; // Å
// æ, ø, å, Æ, Ø, Å er plassert i RAMadresse 0, 1, 2, 3, 4, 5
void setup()
{
lcd.init(); // initialize the lcd
lcd.begin(16,2); // stille inn displayet for 16x2 tegn, og slå på bakbelysningen
lcd.backlight();
pinMode(garagesensorpin, INPUT_PULLUP); // Bryter til garasjen
pinMode(blpin, INPUT_PULLUP); // Bryter til backlight
// Laste opp seks selvdefinerende bokstaver
lcd.createChar(0, Lae); // æ
lcd.createChar(1, Loe); // ø
lcd.createChar(2, Laa); // å
lcd.createChar(3, Sae); // Æ
lcd.createChar(4, Soe); // Ø
lcd.createChar(5, Saa); // Å
lcd.clear(); // Nødvendig etter Upload
}
void loop()
{
// Text on display
int garagesensorpinStatus = 0;
garagesensorpinStatus = digitalRead(garagesensorpin);
if (garagesensorpinStatus == HIGH)
{
lcd.setCursor(0,0);
lcd.print("Garage");
lcd.setCursor(0,1);
lcd.print("is ");
lcd.print("open ");
}
else
{
lcd.setCursor(0,0);
lcd.print("Garage");
lcd.setCursor(0,1);
lcd.print("is closed");
}
// Backlight on - ten seconds - then off again
int blpinStatus = 0;
blpinStatus = digitalRead(blpin);
static unsigned long blTime;
static boolean blOn;
if (blpinStatus == LOW)
{
delay(20); // Debounce
blOn = true;
blTime = millis();
lcd.backlight();
}
if (blOn == true && millis() - blTime > 10000)
{
lcd.noBacklight();
blOn = false;
}
}
Look at this example.
See the variable :
static unsigned long blTime;
Now look at this:
if (blpinStatus == LOW)
{
delay(20); // Debounce
blOn = true;
blTime = millis();
lcd.backlight();
}
if (blOn == true && millis() - blTime > 10000)
{
lcd.noBacklight();
blOn = false;
}
See how the flag is set , the variable reads the millis time to get the start time , executes the command to turn on the backlight
and then begins checking for an ELAPSED TIME of TEN SECONDS. (10000 mS)
You MUST SEE THAT , right ?
Now here it says "IF ELAPSED TIME = 10 SECONDS, , RESET FLAG AND TURN OFF BACKLIGHT .
(The whole purpose of this entire program is just to turn ON the backlight, wait 10 seconds , then turn it off.)
if (blOn == true && millis() - blTime > 10000)
{
lcd.noBacklight();
blOn = false;
Now look at your first post and tell me if the lightbulb goes on...
I am trying to make a motor turn on for a period of time for example 30 seconds when a button is pressed. Then I want the motor to turn do nothing until the button is pressed again. Once it is pressed I want the motor to reverse itself for 30 seconds and stop.
Now tell me you still don't "get" millis.
Backlight_Delayed_TurnOFF.ino (2.49 KB)