Alternating the call of two separate functions each 5 seconds

Hi there

I have the following code

void loop {

LCD();

}


void LCD() {
static long timepassed = 0;

if ( millis() - timepassed >= 5000) {
subLCD1();
timepased = millis();
}
else
{
subLCD2();
timepased = millis();
}

void PANTALLA() 
{
static long previousMillisLCD = 0;
  if (millis() - previousMillisLCD >= 300)
  {
    lcd.print("MAP ECT TPS RPM");
    lcd.setCursor(0, 1);
    lcd.print("                ");//16 blank spaces
    lcd.setCursor ( 0, 1 );
    lcd.print(PRESION);
    lcd.setCursor ( 4, 1 );
    lcd.print(TEMP);
    lcd.setCursor ( 8, 1 );
    lcd.print(ACCEL_LCD);
    lcd.setCursor ( 11, 1 );
    lcd.print(RPM);
    previousMillisLCD = millis();
  }
}


void subLCD2() 
{
static long previousMillisLCD = 0;
  if (millis() - previousMillisLCD >= 300)
  {
    lcd.print(" MASA   TIEMPO");
    lcd.setCursor(0, 1);
    lcd.print("                ");//16 blank spaces
    lcd.setCursor ( 0, 1 );
    lcd.print(MASA);
    lcd.setCursor ( 9, 1 );
    lcd.print(TI);
    previousMillisLCD = millis();
  }
}

I´m not being able to get the hang of it, I believe is because the 300 ms refresh in each subLCD is messing with the other.

Also you will see that the variables are declared static, that´s why running some isolated test calling only one subfunction gave better results.

What do you guys think I´ll be the best approach?

I need the 1602 LCD to show some data from 5 seconds, then change to another data for more 5 seconds,then going back to the first data, and so on.

If anything else fails I already ordered a 1604 LCD!

Best regards

Fabius_rex:
I have the following code

Thanks for using code tags, but this is not the code you should be giving us. This is not your complete code. Where is setup()? Where is subLCD1()? Where is PANTALLA() called?

Please do not waste our time giving us something that is not the code you need help with.

Somethings wrong friend. You're "LCD()" function never closes before you declare a new function called PANTALLA(), which never seems to be called anyway. I assume you meant to have a close " } " to the LCD before PANTALLA()? And you meant to say "subLCD1()" and not PANTALLA()? Otherwise this makes no sense.

As Paul said, you need to offer us more complete code. But also, at least tell what you're seeing (if anything). I wildly flickering display? A display with random characters? No display at all? "Not getting the hang of it" is descriptive, and we've all been there. But it doesn't tell us much.

One thing I see right away is that in each function, you start by doing an lcd.print() without a lcd.setcursor(). ALWAYS do a setcursor() before ANY lcd.print(), unless you explicitly need to print one thing after another. For example:

lcd.setCursor(0,0);
lcd.print("Temperature is ");
lcd.print(temp);

Without the initial lcd.setCursor(), not only might you be printing outside the display, but sometimes it can cause wild flashing of the displayed text, that can leave you totally confused as to what is going on.

And don't get too frustrated. Working with LCDs for the first time can get very frustrating. Over time you'll learn a lot of tricks. You're right that most problems are about timing, but its a long list. Its always good to simplify down to one or two things that work as expected before adding more.

Sorry I tried to put it simple but copied some code and written new.

Here is the full skecth

Also, I cannot use delay since the display is to show the variables running but the proyect is time sensitive.

#include <Wire.h>
#include <hd44780.h>                       // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header
hd44780_I2Cexp lcd; // declare lcd object: auto locate & config exapander chip

//Variables
int PRESION;
int TEMP; 
int ACCEL_LCD; 
float MASA; 
int TI; 
//Variables


void setup()
{

  lcd.begin (16, 2); 

void loop()
{
PANTALLA();
PANTALLA2();
//I want to alternate each 5 seconds the call from one to the other. So Pantalla(); shows me the firts data 5 seconds then PANTALLA2() refreshes the display with another data 5 other seconds and the again PANTALLA() is called
}

void PANTALLA() //Funcion que controla display y variables
{
  if (millis() - previousMillisLCD >= 300)
  { //Abre if
    lcd.print("MAP ECT TPS RPM");
    lcd.setCursor(0, 1);
    lcd.print("                ");//16 blank spaces
    lcd.setCursor ( 0, 1 );
    lcd.print(PRESION);
    lcd.setCursor ( 4, 1 );
    lcd.print(TEMP);
    lcd.setCursor ( 8, 1 );
    lcd.print(ACCEL_LCD);
    lcd.setCursor ( 11, 1 );
    lcd.print(RPM);
    previousMillisLCD = millis();
  } // Cierra If;
}// Cierra Pantalla();


void PANTALLA2()  //Funcion que controla display y variables 2
{
  if (millis() - previousMillisLCD >= 300)
  { //Abre if
    lcd.print(" MASA   TIEMPO");
    lcd.setCursor(0, 1);
    lcd.print("                ");//16 blank spaces
    lcd.setCursor ( 0, 1 );
    lcd.print(MASA);
    lcd.setCursor ( 9, 1 );
    lcd.print(TI);
    previousMillisLCD = millis();
  }//Cierra IF
} //Cierra Pantalla2();


The display works if you call one or the other, but I cannot make the functions alternate altogheter

We can help you fix this.

But first I have a question about your design:

...the display is to show the variables running but the proyect is time sensitive.

When the sketch is switching data as you say you want, you will not see some of the the variables running for 5 seconds, then you will not see the other variables running for 5 seconds. But you say the data is time sensitive? Are you sure this is what you want to do? Can you not show all variables running at the same time? What about a 20x4 display?

I thought about the 16x4 display yes.

What I meant with time sensitive is that I cannot use in my sketch the delay() function.

The display is just to check in those 5 seconds how the variables evolve.

It´s a Arduino E.F.I attempt, the variables are, RPM. Temperature, Pressure, injector pulse, etc.

It does not matter the 5 seconds refresh being long or short, first I need to get the two segments of data being displayed, then I worry about how much time.

Thanks in advance for your time and patience.

Regards