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;
}