Go Down

Topic: delay and mills on NANO + LCD  (Read 44 times) previous topic - next topic

laoadam

I'd like the LCD show aaa=bbb, and show xxx=yyy for a while when the button pressed.
The code below works well,  need help how to use mills replace the delay. Thanks.

Code: [Select]

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

unsigned long interval5 = 5000;  // the time we need to wait
unsigned long previousMillis5 = 0; // millis() returns an unsigned long.

unsigned long interval = 10000; // the time we need to wait
unsigned long previousMillis = 0; // millis() returns an unsigned long.

unsigned long currentMillis = millis(); // grab current time

void setup() {

  Serial.begin(9600);  // Used to type in characters

  lcd.begin(16, 2);  // initialize the lcd for 16 chars 2 lines, turn on backlight

  //-------- Write characters on the display ------------------
  // NOTE: Cursor Position: (CHAR, LINE) start at 0
  lcd.setCursor(0, 0); //Start at character 4 on line 0
  lcd.print("Hello, world!");
  delay(20);
  lcd.setCursor(0, 1);
  lcd.print("testing");
  //delay(5000);
  // Set the PWM pins as output.

  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {

  button();
  test();

}

void button() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
 // if (buttonState == HIGH) {

 unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis; 

     if (buttonState == HIGH) {

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("xxx=");
    lcd.print("yyy");
    //delay(2000);
  }
  }

  else {
    // turn LED off:
    digitalWrite(ledPin, LOW);

  }
}


void test() {

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("aaa=");
  lcd.print("bbb");
  delay(1000);

}

Go Up