Hola, estoy desarrollando un proyecto al cual lo he llamado "Calculadora Inteligente"
Bueno, para este proyecto use estos materiales:
-LCD 16x2
-Modulo I2C(para la pantalla)
-Keypad 4x4
-Sensor de ultrasonidos HRS-04
-Sensor de temperatura y humedad DHT11
-Arduino UNO R3
Mi problema se presento en el codigo.
al principio, sin poner la calculadora, los sensores funcionaban bien, pero al momento de introducir el codigo para "crear" la calculadora, todo fue mal.
Los numeros que yo apreto no aparecen en el LCD, pero siguen apareciendo los datos de los sensores.
Este es mi codigo.
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
long first = 0;
long second = 0;
double total = 0;
char customKey;
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','+'},
{'4','5','6','-'},
{'7','8','9','*'},
{'C','0','=','/'}
};
byte rowPins[ROWS] = {9,8,7,6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5,4,3,2}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);
#include "DHT.h"
#define DHTPIN 10
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
#define Pecho 12
#define Ptrig 11
long duracion, distancia;
LiquidCrystal_I2C lcd(0x3F,16,2); // set the LCD address to 0x20 for a 16 chars and 2 line display
void setup()
{
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight();
pinMode(Pecho, INPUT);
pinMode(Ptrig, OUTPUT);
dht.begin();
lcd.print("Cal. Inte. v1.0");
lcd.setCursor(0,1);
lcd.print("By Matias M.");
delay(2500);
lcd.clear();
}
void loop()
{
float h=dht.readHumidity();
float t=dht.readTemperature();
digitalWrite(Ptrig, LOW);
delayMicroseconds(2);
digitalWrite(Ptrig, HIGH);
delayMicroseconds(10);
digitalWrite(Ptrig, LOW);
duracion = pulseIn(Pecho, HIGH);
distancia = (duracion/2) / 29;
lcd.print("Distancia:");
lcd.print(distancia);
lcd.print("cm");
lcd.setCursor(0,1);
lcd.print("Humedad:");
lcd.print(h);
delay(2000);
lcd.clear();
lcd.print("Temp.:");
lcd.print(t);
delay(3000);
lcd.clear();
}
void Cal()
{
customKey = customKeypad.getKey();
switch(customKey)
{
case '0' ... '9': // This keeps collecting the first value until a operator is pressed "+-*/"
lcd.setCursor(0,0);
first = first * 10 + (customKey - '0');
lcd.print(first);
break;
case '+':
first = (total != 0 ? total : first);
lcd.setCursor(0,1);
lcd.print("+");
second = SecondNumber(); // get the collected the second number
total = first + second;
lcd.setCursor(0,3);
lcd.print(total);
first = 0, second = 0; // reset values back to zero for next use
break;
case '-':
first = (total != 0 ? total : first);
lcd.setCursor(0,1);
lcd.print("-");
second = SecondNumber();
total = first - second;
lcd.setCursor(0,3);
lcd.print(total);
first = 0, second = 0;
break;
case '*':
first = (total != 0 ? total : first);
lcd.setCursor(0,1);
lcd.print("*");
second = SecondNumber();
total = first * second;
lcd.setCursor(0,3);
lcd.print(total);
first = 0, second = 0;
break;
case '/':
first = (total != 0 ? total : first);
lcd.setCursor(0,1);
lcd.print("/");
second = SecondNumber();
lcd.setCursor(0,3);
second == 0 ? lcd.print("Invalid") : total = (float)first / (float)second;
lcd.print(total);
first = 0, second = 0;
break;
case 'C':
total = 0;
lcd.clear();
break;
}
}
long SecondNumber()
{
while( 1 )
{
customKey = customKeypad.getKey();
if(customKey >= '0' && customKey <= '9')
{
second = second * 10 + (customKey - '0');
lcd.setCursor(0,2);
lcd.print(second);
}
if(customKey == '=') break; //return second;
}
return second;
}
Ese es mi codigo.
Espero que me puedan ayudar con estee proyecto.
Gracias desde ya.