user1739123
user1739123

Reputation: 93

Arduino Sketch Loop using Millis()

The goal of this program is to write a string to the Arduino's LCD, and cycle between two different messages. The problem with my current version is that it cycles back and forth with no delay. How would I get these to write one at a time?

Here is the code, I left out some of the irrelevant parts:

  #include <LiquidCrystal.h>
  #include <string.h>
  // These are the pins our LCD uses.
  LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
    // initalize the lcd, and button input at zero.
    int lcd_key     = 0;
    int adc_key_in  = 0;
    //define values for each button
    #define btnRIGHT  0
    #define btnUP     1
    #define btnDOWN   2
    #define btnLEFT   3
    #define btnSELECT 4
    #define btnNONE   5
 // read the buttons
 int read_LCD_buttons()
  {
   adc_key_in = analogRead(0);      // detects the value from the buttons
   // The buttons give values close to which values we saet them between.
   if (adc_key_in > 1000) return btnNONE; // When the input is greater than 1000 that         means no buttons are being pressed,
   if (adc_key_in < 50)   return btnRIGHT;
   if (adc_key_in < 195) return btnUP; 
   if (adc_key_in < 380) return btnDOWN; 
   if (adc_key_in < 555) return btnLEFT; 
   if (adc_key_in < 790) return btnSELECT;  
   return btnNONE; // if there is some issue with values, the programs will not break.
  }
  void setup()
  {
   Serial.begin(9600); //Set the serial monitor.
   lcd.begin(16, 2); //Set the LCD
  }
  void loop()
    {

     timer = millis();
   if (left == true) //Right alignment
         {
         lcd.clear() ; //Clear any existing text
         lcd.setCursor(5, 0); //Set cursor to right side.
         timer = millis();
         if (millis() < (timer + 5000)) {
         if (show1 == true) //See if first line should be displayed. If false, nothing is displayed.
         {
         lcd.print("Time");
         }
         //Second line
         lcd.setCursor(4, 1); 
         if (show2 == true)//See if second line should be displayed
         {
         lcd.print("12:00 PM");
         }
         }
         if ((timer + 5000) > millis() < (timer + 10000)) {
         //Display Date
         lcd.setCursor(5, 0);
         if (show1 == true)//See if first line should be displayed.
         {
         lcd.print("Date");
         }
         //Second line
        lcd.setCursor(1, 1);
        if (show2 == true)//See if second second should be displayed.
        {
        lcd.print("Nov. 16, 2012");
        }
        }
      }
   }

Upvotes: 1

Views: 1829

Answers (2)

codewarrior
codewarrior

Reputation: 1269

This condition if ((timer + 5000) > millis() < (timer + 10000)) makes no sense in C - at least it don't do what you are expecting.

It is invoked like below:

  • first (timer + 5000) > millis() is invoked and its value is 0 or 1
  • next 0 or 1 (from first condition) is compared with (timer + 10000) which is always true (assuming that you have not overflow time value and you are not comparing with large negative number)

You should use something like if ((timer + 5000) > millis() && mills() < (timer + 10000)) or rather:

int hlp_time = millis(); 
if ((timer + 5000) > hlp_time && hlp_time < (timer + 10000))

since time returned by millis() will vary between each check in if condition.

Upvotes: 2

user1683391
user1683391

Reputation: 79

Have you tried setting timer = millis(); to outside the loop?

Upvotes: 0

Related Questions