millis?

here i send my codes and i got problem how can i scroll speedup and delay time reduce between two display functions(tankinfo to reservoirinfo)

#include <LiquidCrystal.h>

#define trigger1 11
#define echo1 10
#define trigger2 12
#define echo2 13
#define DISPLAY_TANKINFO 0 //new
#define SCROLL_TANKINFO 1
#define DISPLAY_RESERVOIRINFO 2
#define SCROLL_RESERVOIRINFO 3
#define DISPLAY_PUMPSTATUS 4  // not added yet
#define SCROLL_PUMPSTATUS 5 // not added yet
#define DISPLAY_DATETIME 6  // not added yet
#define SCROLL_DATETIME 7
#define DELAY_TANK2RESERVOIR 8  // delay step between display of tank info and reservoir info
#define DISPLAY_WELLEMPTY 9
#define DELAY_RESERVOIR2TANK 10 
#define WAIT_WELLFILLING 11


// it's a 16x2 LCD so...
int screenWidth = 16;
int screenHeight = 2;
float time=0;
byte currentDisplay = DISPLAY_TANKINFO;
int tankLevel;
int reservoirLevel;
unsigned long currentTime;

LiquidCrystal lcd(8,9,4,5,6,7);

void setup()
{
 lcd.begin(16,2),
 pinMode(trigger1,OUTPUT);
 pinMode(echo1,INPUT);
 pinMode(trigger2,OUTPUT);
 pinMode(echo2,INPUT);
}

void loop()
{ 
  readTankLevel();
  readReservoirLevel();
  
    if (reservoirLevel < 1)
  {
    currentDisplay = DISPLAY_WELLEMPTY;
  }

 
  switch (currentDisplay)
  {
    case DISPLAY_TANKINFO:
      displayTankInfo();
      currentDisplay = SCROLL_TANKINFO;
      break;
      
    case SCROLL_TANKINFO:
      if (scrollDisplayLeft(20, 16) == true)
      {
        currentDisplay = DELAY_TANK2RESERVOIR;
      }
      break;
      
    case DELAY_TANK2RESERVOIR:
      if(wait(1) == true)
      {
        currentDisplay = DISPLAY_RESERVOIRINFO;
      }
      break;

    case DISPLAY_RESERVOIRINFO:
      displayReservoirInfo();
      currentDisplay = SCROLL_RESERVOIRINFO;
      break;
      
    case SCROLL_RESERVOIRINFO:
      if (scrollDisplayLeft(20, 16) == true)
      {
        currentDisplay = DELAY_RESERVOIR2TANK;
      }
      break;

    case DELAY_RESERVOIR2TANK:
      if(wait(1000) == true)
      {
        currentDisplay = DISPLAY_TANKINFO;
      }
      break;  
      
      case DISPLAY_WELLEMPTY:
      displayWellEnpty();
      currentDisplay = WAIT_WELLFILLING;
      wait(0);
      scrollDisplayLeft(20, 0);
      break;

      case WAIT_WELLFILLING:
      if (reservoirLevel > 5) // arbitrary limit
      {
        // start from the beginning
        currentDisplay =  DISPLAY_TANKINFO;
      }
      break;


  }
  
}


//..............................................
/*
  read the tank level
*/
void readTankLevel()
{
  digitalWrite(trigger1, LOW);
  delayMicroseconds(2);
  digitalWrite(trigger1, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigger1, LOW);
  delayMicroseconds(2);
  time = pulseIn(echo1, HIGH);
  tankLevel = (140 - (time * 340 / 20000)) / 1.3;
}

void readReservoirLevel()
{
  digitalWrite(trigger2, LOW);
  delayMicroseconds(2);
  digitalWrite(trigger2, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigger2, LOW);
  delayMicroseconds(2);
  time = pulseIn(echo2, HIGH);
  reservoirLevel = (140 - (time * 340 / 20000)) / 1.3;
}

    /*
  setup the display to show the tank information
*/
void displayTankInfo()
{
  lcd.clear();
  lcd.setCursor(16, 0);
  lcd.print("Tank Level: ");
  lcd.print(tankLevel);
  lcd.setCursor(31, 0);
  lcd.print("%");
  lcd.setCursor(16, 1);
  lcd.print("volume   : ");
  lcd.print(tankLevel * 5);
  lcd.setCursor(30, 1);
  lcd.print("Lt");
}

void displayReservoirInfo()
{
  lcd.clear();
  lcd.setCursor(16, 0);
  lcd.print("Rsv Level: ");
  lcd.print(reservoirLevel);
  lcd.setCursor(31, 0);
  lcd.print("%");
  lcd.setCursor(16, 1);
  lcd.print("vole   : ");
  lcd.print(reservoirLevel * 5);
  lcd.setCursor(30, 1);
  lcd.print("Lt");
}

void displayWellEnpty()
{
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("ass Level: ");
  lcd.print(reservoirLevel);
  lcd.setCursor(15, 0);
  lcd.print("%");
  lcd.setCursor(0, 1);
  lcd.print("vole   : ");
  lcd.print(reservoirLevel * 5);
  lcd.setCursor(14, 1);
  lcd.print("Lt");
}
    /*
  scroll the display left
  In:
    duration for each step (in ms)
    number of steps
  Returns:
    true if scroll completed, flase if scroll in progress
*/
bool scrollDisplayLeft(unsigned long stepDuration, byte numSteps)
{
  static unsigned long lastUpdateTime = 0;
  static byte positionCounter = 0;
  currentTime = millis();
  if (currentTime - lastUpdateTime >= stepDuration)
  {
    lastUpdateTime = currentTime;
    lcd.scrollDisplayLeft();
    positionCounter++;
    if (positionCounter >= numSteps)
    {
      positionCounter = 0;
      return true;
    }
  }

  return false;
}
    
 
/*
  check if a duration has lapsed
  In:
    duration (in ms)
  Returns:
    false if duration has not lapsed, else true
 */
bool wait(unsigned long duration)
{
  static unsigned long startTime;
  static bool isStarted = false;
  currentTime = millis();
  if(duration == 0)
  {
    isStarted = true;
    return true;
  }

  // check if wait period has lapsed
  if(currentTime - startTime >= duration)
  {
    // lapsed, indicate no longer started so next time we call the function it will initialise the start time again
    isStarted = false;
    // indicate to caller that wait period has lapsed
    return true;
  }

  // indicate to caller that wait period is in progress
  return false;
}

walakulu:
here i send my codes and i got problem how can i scroll speedup and delay time reduce between two display functions(tankinfo to reservoirinfo)

I can't understand that. Can you explain it more clearly?

Also tell us what your program actually does and what you want it to do that is different.

...R

so sorry for that.
this is automatic motor control project code made by helping some expert in the forum. i am learning arduino at 0 level.i want to apply for my home motor and try for that! in this project i want to run 3 task(1-water level,2-res level,3-time and date with temperature) and display one by one.(and also my mind have add few buttons to control water level to motor start and display level on max7219 led 7 segment module in same time)so in my this code some codes like motor relay,date/ time, buttons not yet added.some master helping for some stages so i make this but,some problem i got like i mention early.now i want solve this and go to next step.if you can pls help me.

here i send more clearly

1.mesure levels in tank and reservoir by US-04 and display lcd one by one and led 7 segment max7219 module display (and correction to the wrong measurement due to water wave)
2.time and date with temperature(water) display after levels display on lcd

and updating levels/run date when displaying periods (using millis or another method)

this 3 display over and over(loop)

  1. add few option like motor start/stop time or timer,set levels to motor start /end tank levels,adjust time manually using few buttons

It looks as if your Reply #3 is meant to be a substitute for Reply #2 - is that correct?

It is still not at all clear what parts are working and what parts you need help with.

The code in your Original Post seems to have lots of lines that set the value of currentDisplay but not a single line that makes use of that value.

...R

yes sir,

now that is two levels run one by one on lcd but i cant understand how speed up scroll.
actually i need display like this on lcd

tank level/volume {10sec(levels should update in this 10 sec)} ------------->(scroll fast)-----------> reservoir level/volume {10sec(levels should update in this 10 sec)}------------->(scroll fast)-----------> time,date and tem[20sec}-------------->tank level/volume (all levels and time/tem should update not delay when displaying periods)

additionally i want add few buttons to control start motor manually and automatically start in low level as mention last my massage.

can you change this code to complete above task or completely forget that code and guide me a making a new code ?
i am a beginner so if you have time pls explain.

You seem to have a second thread on the same subject run two or more thing and display lcd one by one - Programming Questions - Arduino Forum

Replying to both is going to get confusing if it has not already happened.

@walakulu, click Report to Moderator and ask to have this Thread merged with the other one.

I am not going to waste time checking 2 Threads to see what has been said.

...R

ok self learning is best thing,thank you helping me to success my project.