Help with LCD input data.
Hi! I'm new on this world of arduino, and I have a project where I'm working for myself. I would like some guy with experience on arduino code could help me please.
My problem is the next one: I have write the next code to introduce values with a potenciometer and a boton on a LCD.
It's works of the follow way:
When I puss the boton "pin", sentence IF is true then I can write the values with the potenciometer. All right, but when sentence IF finised the value of the last state of the analogic imput is copied to all others!. I don't undestand why!!
Ex:
Number -19 is writed, but as the last value es 9, when the program go out of IF sentence, copy change the number to +99!! Number 9 is copied to all others variables.
Here is the code, please, could someone tell my why?? Thanks a lot!!!!
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <LiquidCrystal.h>
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x20 for a 16 chars and 2 line display
int val = analogRead(0);
int estadoAnterior = 0;
int estadoActual;
int pin=2; // aquí el pin digital que se quiere leer
int led=13;
char ref []={"000"}; // Declaramos un buffer donde guardameros los digitos de la temperatura
int i=0; //Variable para controlar cuando se graban los datos de las temperaturas
void setup() {
Serial.begin(9600);
pinMode(pin, INPUT);
pinMode(led, OUTPUT);
//******************************************************************************
lcd.init(); // cargamos la pantalla LCD
lcd.init();
// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("TEMPERATURA DEPOSITO");
lcd.setCursor(0,1);
lcd.print("D1 C D2 C");
lcd.setCursor(0,2);
lcd.print("min min ON");
lcd.setCursor(0,3);
lcd.print("max max ");
//********************************************************************************
}
void loop()
{
//Comprueba si se ha pulsado el boton 1, si es verdadero salta a menuinicial
digitalWrite(led, LOW);
estadoActual = digitalRead(pin);
if (estadoAnterior != estadoActual) // ha habido un cambio de estado
{
estadoAnterior = estadoActual;
delay (500);
menuinicial(); //Salta al bloque menuinicial
}
}
//Menu inicial
void menuinicial()
{
digitalWrite(led, HIGH);
delay (150);
// Primer digito
do{ // Mientras no cambie el estado del boton 1, repetirá este bloque
// Mientras que no cambie el estado del boton 1, repetirá este bloque
int val = analogRead(1);
val = map(val, 0, 1023, 0, 9); // Mapea un valor analogico en 10 numeros (de 0 a 9)
if (val<=4) { ref[0] = {'-'};} else { ref[0] = {'+'};}
lcd.setCursor(3,2); // for LCD
lcd.print(ref[0]); // for LCD
estadoActual = digitalRead(pin);
} while(estadoAnterior != estadoActual);
estadoAnterior = estadoActual; //Inicializa el estado del boton 1
delay (300);
//***************
// Segundo digito
do{ // Mientras que no cambie el estado del boton 1, repetirá este bloque
int val = analogRead(1);
val = map(val, 0, 1023, 0, 10); // Mapea un valor analogico en 10 numeros (de 0 a 9)
if (val==0) {ref[1]={'0'};} if (val==1) {ref[1]={'1'};} if (val==2) {ref[1]={'2'};} if (val==3) {ref[1]={'3'};} if (val==4) {ref[1]={'4'};} if (val==5) {ref[1]={'5'};} if (val==6) {ref[1]={'6'};} if (val==7) {ref[1]={'7'};} if (val==8) {ref[1]={'8'};} if (val==9) {ref[1]={'9'};}
lcd.setCursor(4,2); // for LCD
lcd.print(ref[1]); // for LCD
estadoActual = digitalRead(pin);
} while(estadoAnterior != estadoActual);
estadoAnterior = estadoActual; //Inicializa el estado del boton 1
delay (300);
//***************
// Tercer digito
do{ // Mientras que no cambie el estado del boton 1, repetirá este bloque
int val = analogRead(1);
val = map(val, 0, 1023, 0, 10); // Mapea un valor analogico en 10 numeros (de 0 a 9)
if (val==0) {ref[2]={'0'};} if (val==1) {ref[2]={'1'};} if (val==2) {ref[2]={'2'};} if (val==3) {ref[2]={'3'};} if (val==4) {ref[2]={'4'};} if (val==5) {ref[2]={'5'};} if (val==6) {ref[2]={'6'};} if (val==7) {ref[2]={'7'};} if (val==8) {ref[2]={'8'};} if (val==9) {ref[2]={'9'};}
lcd.setCursor(5,2);
lcd.print(ref[2]);
estadoActual = digitalRead(pin);
} while(estadoAnterior != estadoActual);
estadoAnterior = estadoActual; //Inicializa el estado del boton 1
delay (300);
//***************
}