/*
*/
#include <Wire.h>
#include <LiquidCrystal.h>
//#include <LiquidCrystal_I2C.h>
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
//LiquidCrystal_I2C lcd(0x27,20,4);
#define DS1307_ADDRESS 0x68
#define buttonPin 13
#define buttonPinSel4 5
#define buttonPinSel3 6
#define buttonPinSel2 A0
#define buttonPinSel A1
#define buttonPinNivel A2
const int dataPin = 2; /* Q7 */
const int clockPin = 3; /* CP */
const int latchPin = 4; /* PL */
const int numBits = 8; /* quantidade de nivel CI > 8 * number of shift registers */
int numNivel[8];
int nivelx;
//declara variavel usada
boolean leBotao(int porta);
boolean lerbotao = false;
boolean lerbotaoAnt = false;
byte zero = 0x00;
int number, lastMin1, lastMin2, lastHour1, lastHour2;
void showDigit(int numbery);
int numeroMenu = 0;
int numeroTitulo = 4;
char* tituloMenu[5] = {
"0 - Nivel Agua ",
"1 - Hora ",
"2 - Liga Veiculo ",
"3 - Ligar Luzes ",
" "
};
byte segment[8][8] = {
{ B01111, B11111, B11111, B11111, B11111, B11111, B11111, B11111 },
{ B11111, B11111, B11111, B00000, B00000, B00000, B00000, B00000 },
{ B11110, B11111, B11111, B11111, B11111, B11111, B11111, B11111 },
{ B11111, B11111, B11111, B11111, B11111, B11111, B11111, B01111 },
{ B00000, B00000, B00000, B00000, B00000, B11111, B11111, B11111 },
{ B11111, B11111, B11111, B11111, B11111, B11111, B11111, B11110 },
{ B11111, B11111, B11111, B00000, B00000, B00000, B11111, B11111 },
{ B11111, B00000, B00000, B00000, B00000, B11111, B11111, B11111 }
//{ B00000, B00000, B01110, B01110, B01110, B01110, B00000, B00000 },
//{ B00001, B00011, B00010, B00110, B01100, B01000, B11000, B10000 },
};
int col = 0;
//Tela inicial do programa
void showStartupMessage() {
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("Seja Bem Vindo!");
delay(1000);
lcd.setCursor(0, 1);
lcd.print(" Iniciando Sistema");
delay(1000);
lcd.setCursor(3, 2);
String message = "Le MotorHome!";
for (byte i = 0; i < message.length(); i++) {
lcd.print(message[i]);
delay(50);
}
lcd.setCursor(2, 3);
//lcd.print(leNivel(3));
lcd.print("(19) 97414 3497");
delay(1000);
lcd.clear();
}
void menuP(int text) {
//lcd.clear();
lcd.setCursor(0, 0);
lcd.print("< MENU >");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 2);
lcd.print("> ");
lcd.print(tituloMenu[text]);
lcd.setCursor(2, 3);
int y = text;
y++;
lcd.print(tituloMenu[y]);
}
void menuHome(int Menu){
if(Menu == 0){
int x = 0;
while(x < numeroTitulo){
numeroMenu = x;
menuP(numeroMenu);
if (leBotao(buttonPinSel2) == HIGH){
x++;
if(x >= numeroTitulo){
x = numeroTitulo-1;
}
}
if (leBotao(buttonPinSel) == HIGH){
x--;
if(x <= 0){
x = 0;
}
}
if (leBotao(buttonPinSel3) == HIGH){
x=numeroTitulo;
}
if (leBotao(buttonPinSel4) == HIGH){
if (numeroMenu == 0){
leNivel();
delay(1000);
}
if (numeroMenu == 1){
lcd.clear();
showClock();
delay(1000);
}
if (numeroMenu == 3){
lcd.clear();
showClock();
delay(1000);
}
}
}
}
}
//Funcao mostra nivel no LCD
void NivelAgua(int nive) {
lcd.clear();
lcd.setCursor(0, 0);
//String message = "Nivel Agua: ";
String message = " N I V E L A G U A ";
for (byte i = 0; i < message.length(); i++) {
lcd.print(message[i]);
delay(50);
}
switch (nive) {
case 0:
lcd.print("Vazio");
col = 0;
col = col + 8;
showDigit(0);
lcd.print(" %");
break;
case 1:
//lcd.print("1%");
col = 0;
col = col + 8;
showDigit(1);
lcd.print(" %");
break;
case 2:
//lcd.print("25%");
col = 0;
col = col + 6;
showDigit(2);
col = col + 4;
showDigit(5);
lcd.print(" %");
break;
case 3:
//lcd.print("50%");
col = 0;
col = col + 6;
showDigit(5);
col = col + 4;
showDigit(0);
lcd.print(" %");
break;
case 4:
//lcd.print("75%");
col = 0;
col = col + 6;
showDigit(7);
col = col + 4;
showDigit(5);
lcd.print(" %");
break;
case 5:
//lcd.print("100%");
col = 0;
col = col + 4;
showDigit(1);
col = col + 3;
showDigit(0);
col = col + 4;
showDigit(0);
lcd.print(" %");
break;
}
delay(500);
return;
}
//Funcao para ler o botao conforme a porta selecionada
boolean leBotao(int porta) {
static boolean estadoAnterior[] = {
0,0,0,0,0};
boolean estadoBotao = digitalRead(porta);
boolean ligado = false;
if (!estadoBotao && estadoAnterior[porta]) {
ligado = true;
}
estadoAnterior[porta] = estadoBotao;
return ligado;
}
//Funcao ler o nivel
void leNivel() {
// Step 1: Sample
digitalWrite(latchPin, LOW);
digitalWrite(latchPin, HIGH);
nivelx=0;
// Step 2: Shift
//Serial.print("Bits: ");
for (int i = 0; i < numBits; i++) {
int bit = digitalRead(dataPin);
if (bit == HIGH) {
//Serial.print("1");
numNivel[i] = bit;
} else {
//Serial.print("0");
numNivel[i] = bit;
}
digitalWrite(clockPin, HIGH); // Shift out the next bit
digitalWrite(clockPin, LOW);
}
//Serial.println();
for (int i = 0; i < numBits; i++) {
//Serial.print(numNivel[i]);
if(numNivel[i] == 1)
nivelx++;
}
//Serial.print(" nivelx=");
//Serial.print(nivelx);
NivelAgua(nivelx);
//Serial.println();
delay(100);
}
byte convertToBCD(byte val) { // Converts the decimal number to BCD
return ( (val / 10 * 16) + (val % 10) );
}
byte convertToDecimal(byte val) { // Converts from BCD to decimal
return ( (val / 16 * 10) + (val % 16) );
}
void setup() {
//lcd.init();
//lcd.backlight();
Serial.begin(9600);
Wire.begin();
lcd.begin(20, 4);
for (byte i = 0; i < 8; i++) {
lcd.createChar(i, segment[i]);
}
pinMode(dataPin, INPUT);
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(buttonPinSel, INPUT_PULLUP);
pinMode(buttonPinSel2, INPUT_PULLUP);
pinMode(buttonPinSel3, INPUT_PULLUP);
pinMode(buttonPinSel4, INPUT_PULLUP);
pinMode(buttonPinNivel, INPUT_PULLUP);
showStartupMessage();
}
void loop(){
showClock();
if (leBotao(buttonPin) == HIGH){
menuHome(0);
lcd.clear();
}
if (leBotao(buttonPinNivel)){
leNivel();
delay(1000);
lcd.clear();
}
}
void digit0() // Set the segments to the number 0
{
lcd.setCursor(col, 2); // Select the top line
lcd.write((byte)0); // Segment 0 selected
lcd.write(1); // Segment 1 selected
lcd.write(2);
lcd.setCursor(col, 3); // Select the bottom line
lcd.write(3);
lcd.write(4);
lcd.write(5);
}
void digit1() // Set the segments to the number 1
{
lcd.setCursor(col, 2);
lcd.write(1);
lcd.write(2);
lcd.setCursor(col + 1, 3);
lcd.write(5);
}
void digit2() // Set the segments to the number 2
{
lcd.setCursor(col, 2);
lcd.write(6);
lcd.write(6);
lcd.write(2);
lcd.setCursor(col, 3);
lcd.write(3);
lcd.write(7);
lcd.write(7);
}
void digit3() // Set the segments to the number 3
{
lcd.setCursor(col, 2);
lcd.write(6);
lcd.write(6);
lcd.write(2);
lcd.setCursor(col, 3);
lcd.write(7);
lcd.write(7);
lcd.write(5);
}
void digit4() // Set the segments to the number 4
{
lcd.setCursor(col, 2);
lcd.write(3);
lcd.write(4);
lcd.write(2);
lcd.setCursor(col + 2, 3);
lcd.write(5);
}
void digit5() // Set the segments to the number 5
{
lcd.setCursor(col, 2);
lcd.write((byte)0);
lcd.write(6);
lcd.write(6);
lcd.setCursor(col, 3);
lcd.write(7);
lcd.write(7);
lcd.write(5);
}
void digit6() // Set the segments to the number 6
{
lcd.setCursor(col, 2);
lcd.write((byte)0);
lcd.write(6);
lcd.write(6);
lcd.setCursor(col, 3);
lcd.write(3);
lcd.write(7);
lcd.write(5);
}
void digit7() // Set the segments to the number 7
{
lcd.setCursor(col, 2);
lcd.write(1);
lcd.write(1);
lcd.write(2);
lcd.setCursor(col + 1, 3);
lcd.write((byte)0);
}
void digit8() // Set the segments to the number 8
{
lcd.setCursor(col, 2);
lcd.write((byte)0);
lcd.write((byte)6);
lcd.write(2);
lcd.setCursor(col, 3);
lcd.write(3);
lcd.write(7);
lcd.write(5);
}
void digit9() // Set the segments to the number 9
{
lcd.setCursor(col, 2);
lcd.write((byte)0);
lcd.write((byte)6);
lcd.write((byte)2);
lcd.setCursor(col + 2, 3);
lcd.write((byte)5);
}
void showDigit(int numbery) { // Shows the number in the position defined by "X"
switch (numbery) {
case 0: digit0();
break;
case 1: digit1();
break;
case 2: digit2();
break;
case 3: digit3();
break;
case 4: digit4();
break;
case 5: digit5();
break;
case 6: digit6();
break;
case 7: digit7();
break;
case 8: digit8();
break;
case 9: digit9();
break;
}
}
void showClock() {
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(zero);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 7);
int seconds = convertToDecimal(Wire.read());
int minutes = convertToDecimal(Wire.read());
int hours = convertToDecimal(Wire.read() & 0b111111);
int dayWeek = convertToDecimal(Wire.read());
int dayMonth = convertToDecimal(Wire.read());
int month = convertToDecimal(Wire.read());
int year = convertToDecimal(Wire.read());
lcd.setCursor(0, 0);
// 01234567890123456789
lcd.print("< SELECIONE MENU >");
if (seconds & 1) {
lcd.setCursor(10, 2);
lcd.print(".");
lcd.setCursor(10, 3);
lcd.print(".");
} else {
lcd.setCursor(10, 2);
lcd.print(" ");
lcd.setCursor(10, 3);
lcd.print(" ");
}
col = 2;
number = hours / 10;
// Conditional to avoid overlapping characters
if (lastHour1 != minutes % 10) {
lcd.setCursor(2, 2);
lcd.print(" ");
lcd.setCursor(2, 3);
lcd.print(" ");
lastHour1 = minutes % 10;
}
showDigit(number); // Show the customized number on the display
col = col+4;
number = hours % 10;
if (lastHour2 != minutes % 10) {
lcd.setCursor(6, 2);
lcd.print(" ");
lcd.setCursor(6, 3);
lcd.print(" ");
lastHour2 = minutes % 10;
}
showDigit(number);
col = col+5;
number = minutes / 10;
if (lastMin1 != minutes % 10) {
lcd.setCursor(11, 2);
lcd.print(" ");
lcd.setCursor(11, 3);
lcd.print(" ");
lastMin1 = minutes % 10;
}
showDigit(number);
col = col+4;
number = minutes % 10;
if (lastMin2 != minutes % 10) {
lcd.setCursor(15, 2);
lcd.print(" ");
lcd.setCursor(15, 3);
lcd.print(" ");
lastMin2 = minutes % 10;
}
showDigit(number);
}
void setDateTime() { // Set the date and time of the DS1307
byte seconds = 00; // Values from 0 to 59
byte minutes = 00; // Values from 0 to 59
byte hours = 00; // Values from 0 to 23
byte dayWeek = 1; // Values from 0 to 6, where 0 = Domgino, 1 = Monday
byte dayMonth = 17; // Values from 1 to 31
byte month = 5; // Values from 1 to 12
byte year = 21; // Values from 0 to 99
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(zero); // Stop at CI so that it can receive data
// The lines below write the values of date and
// time they were placed in the variables above
Wire.write(convertToBCD(seconds));
Wire.write(convertToBCD(minutes));
Wire.write(convertToBCD(hours));
Wire.write(convertToBCD(dayWeek));
Wire.write(convertToBCD(dayMonth));
Wire.write(convertToBCD(month));
Wire.write(convertToBCD(year));
Wire.write(zero); // Start at CI
Wire.endTransmission();
}