i have problem,with my code(here with)( made helping with forum specially Mr.strerretj). my project is measure level and volume by 2 ultrasonic (tank and reservoir)that levels and time , date display one by one (with updating lcd) in system standby mode(3 things).in motor automatically on/run ,that time display only tank level .those are basic functions & in my mind i need to add few buttons to control on motor manually/auto,and time period and also level wise & time manually adjust.And also levels display led 7 segment max7219 module
i am beginner still learn arduino,many of codes dont understand (like millis)
my problem is code levels not updating and changing and scrolling,pls anyone can help me further for this
#include <LiquidCrystal.h>
#define trigger1 11
#define echo1 10
#define trigger2 12
#define echo2 13
LiquidCrystal lcd(8,9,4,5,6,7);
// it's a 16x2 LCD so...
float time=0;
#define DISPLAY_TANKINFO 20
#define SCROLL_TANKINFO 21
#define DISPLAY_RESERVOIRINFO 22
#define SCROLL_RESERVOIRINFO 23
#define DISPLAY_PUMPSTATUS 24
#define SCROLL_PUMPSTATUS 25
#define DISPLAY_DATETIME 26
#define SCROLL_DATETIME 27
#define DELAY_TANK2RESERVOIR 28 // delay step between display of tank info and reservoir info
#define DELAY_RESERVOIR2TANK 29
byte currentDisplay = DISPLAY_TANKINFO;
unsigned long currentTime=0;
int tankLevel;
int reservoirLevel;
void setup() {
lcd.begin(16,2),
pinMode(trigger1,OUTPUT);
pinMode(echo1,INPUT);
pinMode(trigger2,OUTPUT);
pinMode(echo2,INPUT); // put your setup code here, to run once:
}
void loop() {
readTankLevel();
readReservoirLevel();
switch (currentDisplay)
{
case DISPLAY_TANKINFO:
displayTankInfo();
currentDisplay = SCROLL_TANKINFO;
break;
case SCROLL_TANKINFO:
if (scrollDisplayLeft(500, 16) == true)
{
currentDisplay = DELAY_TANK2RESERVOIR;
}
break;
case DELAY_TANK2RESERVOIR:
if(wait(1500) == true)
{
currentDisplay = DISPLAY_RESERVOIRINFO;
}
break;
case DISPLAY_RESERVOIRINFO:
displayRsvInfo();
currentDisplay = SCROLL_TANKINFO;
break;
case SCROLL_RESERVOIRINFO:
if (scrollDisplayLeft(500, 16) == true)
{
currentDisplay = DELAY_RESERVOIR2TANK;
}
break;
case DELAY_RESERVOIR2TANK:
if(wait(1500) == true)
{
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;
}
/*
read the tank level
*/
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(0, 0);
lcd.print("Tank Level: ");
lcd.print(tankLevel);
lcd.setCursor(15, 0);
lcd.print("%");
lcd.setCursor(0, 1);
lcd.print("volume : ");
lcd.print(tankLevel * 5);
lcd.setCursor(14, 1);
lcd.print("Lt");
}
void displayRsvInfo()
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Rsv 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;
unsigned long currentTime = 0;
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;
// if wait period not started yet
if(isStarted == false)
{
// set the start time of the wait period
startTime = currentTime;
// indicate that it's started
isStarted = true;
// indicate to caller that wait period is in progress
return false;
}
// 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;
}