Κώδικας:
#include <Keypad.h>
#include <LiquidCrystal.h>
#include <WString.h>
#include <EEPROM.h>
//****************************************************************************************************
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
//****************************************************************************************************
#define RIGHT 0
#define UP 1
#define DN 2
#define LEFT 3
#define MENU 4
#define MAX_DIMENSION 350
//****************************************************************************************************
int pingPin = 12;
int inPin = 13;
float volume;
long free_volume;
long duration, inches, cm;
float length, height, width;
byte screen = 2;
byte lastScreen = 4;
const int DEBOUNCE_DELAY = 50;
byte screenRefresh = 120;
long dimension_buffer = 0;
int hundrends;
int tenths;
int units;
//****************************************************************************************************
//Key message
int adcKeyIn;
int NUM_KEYS = 5;
int key=-1;
int oldKey=-1;
int adcKeyVal[5] ={30, 150, 360, 535, 760 };
//****************************************************************************************************
byte FI[8] = {
B01110,
B10101,
B10101,
B10101,
B01110,
B00100,
B00100,
B00000
};
byte PSI[8] = {
B10101,
B10101,
B10101,
B01110,
B00100,
B00100,
B00100,
B00000
};
byte OMEGA[8] = {
B01110,
B10001,
B10001,
B10001,
B01110,
B00000,
B11111,
B00000
};
byte GAMMA[8] = {
B11111,
B10000,
B10000,
B10000,
B10000,
B10000,
B10000,
B00000
};
byte DELTA[8] = {
B00100,
B01010,
B10001,
B10001,
B10001,
B10001,
B11111,
B00000
};
byte LAMDA[8] = {
B00100,
B01010,
B10001,
B10001,
B10001,
B10001,
B10001,
B00000
};
byte KSI[8] = {
B11111,
B00000,
B00000,
B01110,
B00000,
B00000,
B11111,
B00000
};
byte PEE[8] = {
B11111,
B10001,
B10001,
B10001,
B10001,
B10001,
B10001,
B00000
};
//****************************************************************************************************
void setup()
{
lcd.createChar(0, FI);
lcd.createChar(1, PSI);
lcd.createChar(2, OMEGA);
lcd.createChar(3, GAMMA);
lcd.createChar(4, DELTA);
lcd.createChar(5, LAMDA);
lcd.createChar(6, KSI);
lcd.createChar(7, PEE);
lcd.begin(16, 2);
lcd.setCursor(0,0);
lcd.print(" TANK VOLUME ");
lcd.setCursor(0,1);
lcd.print(" METER v1.0 ");
delay(2000);
length = 250;
width = 63;
height = 150;
cm = 40;
}
//****************************************************************************************************
void loop()
{
measure_volume();
switch (getKeyDebounce())
{
case MENU:
showMenu();
break;
}
}
//****************************************************************************************************
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
//****************************************************************************************************
// Convert ADC value to key number
int getKey(unsigned int input)
{
int k;
for (k = 0; k < NUM_KEYS; k++)
{
if (input < adcKeyVal[k])
{
return k;
}
}
if (k >= NUM_KEYS)
k = -1; // No valid key pressed
return k;
}
//****************************************************************************************************
int getKeyDebounce()
{
// wait for no keypress, then return keypress held for debounce period
adcKeyIn = analogRead(0); // read the value from the sensor
key = getKey(adcKeyIn); // convert into key press
do
{
key = getKey(analogRead(0));
} while (key != -1);
oldKey = key;
while (oldKey == -1 || key == -1 || key != oldKey)
{
oldKey = key;
delay(DEBOUNCE_DELAY);
key = getKey(analogRead(0));
}
return key;
}
//****************************************************************************************************
void showMenu()
{
boolean inMenu = 0;
lcd.clear();
lcd.setCursor(0, 0); lcd.print("PROGRAMMING");
lcd.setCursor(0, 1); lcd.print("TANK DIMENSIONS");
lastScreen = 100;
delay(2000);
while (inMenu == 0)
{
if ((screen == 0)&&(lastScreen != 0))
{
lcd.clear();
lcd.print("WIDTH");
lastScreen = 0;
}
if ((screen == 1)&&(lastScreen != 1))
{
lcd.clear();
lcd.print("LENGTH");
lastScreen = 1;
}
if ((screen == 2)&&(lastScreen != 2))
{
lcd.clear();
lcd.print("HEIGHT");
lastScreen = 2;
}
switch (getKeyDebounce())
{
case UP:
if (screen != 0)
screen++;
break;
case DN:
if (screen != 3)
screen--;
break;
case MENU:
inMenu = 1;
break;
}
}
if (screen == 0) setDimension();
if (screen == 2) showOptionsMenu();
lcd.clear();
screenRefresh = 120;
}
//****************************************************************************************************
void showOptionsMenu()
{
boolean inMenu = 0;
byte menuItem = 0;
byte lastItem = 1;
while (inMenu != 1)
{
if ((menuItem == 0)&&(lastItem != 0))
{
lcd.clear();
lcd.print("1.1");
lastItem = 0;
}
if ((menuItem == 1)&&(lastItem != 1))
{
lcd.clear();
lcd.print("1.2");
lastItem = 1;
}
if ((menuItem == 2)&&(lastItem != 2))
{
lcd.clear();
lcd.print("1.3");
lastItem = 2;
}
switch (getKeyDebounce())
{
case UP:
if (menuItem != 0)
menuItem++;
break;
case DN:
if (menuItem != 3)
menuItem--;
break;
case RIGHT:
if (menuItem == 0)
{
//EEPROM.write(3,(byte)timeZone);
}
if (menuItem == 1)
{
//EEPROM.write(3,(byte)timeZone);
}
if (menuItem == 2)
{
//EEPROM.write(3,(byte)timeZone);
}
if (menuItem == 3)
{
//EEPROM.write(3,(byte)timeZone);
}
lastItem = 100;
break;
case LEFT:
if (menuItem == 0)
{
//EEPROM.write(0,(byte)useCurrentSpeed);
}
if (menuItem == 1)
{
//EEPROM.write(1,(byte)useDirections);
}
if (menuItem == 2)
{
//EEPROM.write(2,(byte)useAbsoluteHeadings);
}
if (menuItem == 3)
{
//EEPROM.write(3,(byte)timeZone);
}
lastItem = 100;
break;
case MENU:
inMenu = 1;
break;
}
}
//showMenu();
}
//****************************************************************************************************
void measure_volume()
{
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(10);
digitalWrite(pingPin, LOW);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(inPin, INPUT);
duration = pulseIn(inPin, HIGH);
// convert the time into volume
// cm = microsecondsToCentimeters(duration);=============================================================================================>>>>>>>>>>>>>>>>>>>>>>
volume = (((height-cm)*length * width)/1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(volume);
lcd.print(" LITERS");
if(volume <=150.00)
{
lcd.setCursor(0, 1);
lcd.print("FUEL LOW - ADD");
}
if((volume >150.00) && (volume <= 1800.00))
{
lcd.setCursor(0, 1);
lcd.print("NORMAL ");
}
if((volume >1800.00) && (volume<= 2100.00))
{
lcd.setCursor(0, 1);
lcd.print("FULL ");
}
if(volume >2100.00)
{
lcd.setCursor(0, 1);
lcd.print("WARNING-OVERFLOW");
}
delay(100);
}
//****************************************************************************************************
void setDimension()
{
lcd.print("SET DIMENSION");
}
Είναι χωρίς σχόλια αλλά θα χρειαστείς οθόνη και κουμπιά. Η γνώμη μου είναι να κάνεις απλά πράγματα πρώτα για να μπείς στην ουσία. Για ό,τι θέλεις, εδώ είμαι!!!