Hola, soy aficionado en esto de la programación, y tengo una duda en el siguiente código que lo copie de otro que era para lectura de una batería y se muestre en un LCD, yo lo que que quiero es la lectura de 4 baterías y se muestre en el LCD, el código lo modifique y se compila. También es para implementarlo en un cargador de baterías LiPo, para que muestre la carga, no se si este código es lo que quiero.
Luego quiero que cuando empiece a cargar la figura de la batería que e puesto, Haga una animación que tengo en otro código.
El código Original:
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
/* Funcion de configuracion de pines del modulo
LCD/I2C (Direccion,en,rw,rs,d4,d5,d6,d7,backlight,polaridad)
*/
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
byte pinV=A0; // Para leer el voltaje
byte ledR=12; // LED rojo
byte ledA=11; // LED amarillo
byte ledV=10; // LED verde
float voltaje=0.0;
int medida=0;
int porcent=0;
int pinOffset=A1;
float offset=0.0;
int valorPot = 0;
unsigned long tAntes =0; // para el refresco de pantalla
unsigned long tAhora =0;
unsigned long tEjec =10000;
void setup()
{ lcd.begin(16,2); // inicializamos el LCD.
lcd.backlight(); // encendemos la retroiluminación.
pinMode(ledR, OUTPUT);
pinMode(ledA, OUTPUT);
pinMode(ledV, OUTPUT);
Serial.begin(9600);
digitalWrite(ledR, LOW);
digitalWrite(ledA, LOW);
digitalWrite(ledV, HIGH);
}
void loop()
{ tAhora = millis(); // Comprobamos para refrescar el LCD
if( tAhora - tAntes >= tEjec)
{ // cada 10 segundos
tAntes = tAhora; // actualizamos variables
lcd.clear(); // Refrescamos
}
medida = analogRead(pinV); // Leemos voltaje de la batería
valorPot = analogRead(pinOffset); //Leemos ajuste de offset
int ajuste = map (valorPot, 0,1023,60,5); // Mapeamos a un rango más corto
/* Podemos jugar con los dos últimos valores de la función map. Cuanta más diferencia
haya entre ellos, menos precisión tendremos con el potenciómetro.
El valor más grande es para voltajes más lejanos de 5V. El más pequeño para
las fuentes muy cercanas a 5V. Así, con una fuente de 4.90V el valor de offset será
de 9 ó 10.
*/
offset = (ajuste/100.0);
lcd.setCursor(12,1);
lcd.print (offset);
voltaje= ((((medida*5.0)/1023.0))- offset); // Convertimos a valor con decimales
if (voltaje<=0)
{ // Si es menor o igual a cero
voltaje=0.0; // Le damos valor 0, para evitar negativos
}
porcent=((voltaje*100)-320); // Fórmula para mostrar el porcentaje de carga
if(porcent<=0)
{ // Evitamos valores negativos
porcent=0;
}
// Condicionales para encender los LEDs
if(porcent>=41)
{ digitalWrite(ledR, LOW);
digitalWrite(ledA, LOW);
digitalWrite(ledV, HIGH);
}
if(porcent<41 && porcent>10)
{ digitalWrite(ledR, LOW);
digitalWrite(ledA, HIGH);
digitalWrite(ledV, LOW);
}
if(porcent<=10)
{ digitalWrite(ledR, HIGH);
digitalWrite(ledA, LOW);
digitalWrite(ledV, LOW);
}
lcd.setCursor(0,0);
lcd.print("Voltaje: ");
lcd.print(voltaje);
lcd.print("V");
lcd.setCursor(0,1);
lcd.print("Carga: ");
lcd.print(porcent);
lcd.print("% ");
//Descomentar para mostrar por monitor serial.
//Serial.print(medida); Serial.print(" ");
//Serial.print(voltaje);Serial.print(" ");
//Serial.print(porcent); Serial.println("%");
delay(1000);
}
El código modificado:
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27,20,4);
byte Bateria[8] =
{
B01110,
B10001,
B10001,
B10001,
B10001,
B10001,
B10001,
B11111
};
byte Carga1[8] = {
B00110,
B01110,
B11100,
B01110,
B00111,
B01110,
B01100,
B10000
};
byte Voltaje0 = A0;
byte Voltaje1 = A1;
byte Voltaje2 = A2;
byte Voltaje3 = A3;
int medida=0;
int pinOffset=A6;
int valorPot= 0;
int porcent0 = 0;
int porcent1 = 0;
int porcent2 = 0;
int porcent3 = 0;
float Volt0 = 0.0;
float Volt1 = 0.0;
float Volt2 = 0.0;
float Volt3 = 0.0;
float offset=0.0;
unsigned long tAntes =0; // para el refresco de pantalla
unsigned long tAhora =0;
unsigned long tEjec =10000;
void setup()
{
lcd.init(); // inicializamos el LCD.
lcd.backlight(); // encendemos la retroiluminación.
Serial.begin(9600);
lcd.setCursor(2,1); // Mover el cursor a la posición
lcd.print("CARGADOR BATERIAS");
lcd.setCursor(6,2); // Mover el cursor a la posición
lcd.print("By TrUpE");
delay(5000); // Realizar una pausa de 5 segundo
lcd.clear();
}
void loop() {
tAhora = millis(); // Comprobamos para refrescar el LCD
if( tAhora - tAntes >= tEjec)
{ // cada 10 segundos
tAntes = tAhora; // actualizamos variables
lcd.clear(); // Refrescamos
}
medida = analogRead(Voltaje0), analogRead(Voltaje1), analogRead(Voltaje2), analogRead(Voltaje3);
int ajuste = map (valorPot= 0, 0,1023,60,5); // Mapeamos a un rango más corto
offset = (ajuste/100.0);
lcd.setCursor(9,1);
lcd.print("offs");
lcd.setCursor(9,2);
lcd.print (offset);
Volt0, Volt1, Volt2, Volt3 = ((((medida*5.0)/1023.0))- offset); // Convertimos a valor con decimales
if (Volt0, Volt1, Volt2, Volt3 <= 0 )
{ // Si es menor o igual a cero
Volt0, Volt1, Volt2, Volt3 = 0.0; // Le damos valor 0, para evitar negativos
}
porcent0, porcent1, porcent2, porcent3 =((Volt0, Volt1, Volt2, Volt3 * 100) -320 ); // Fórmula para mostrar el porcentaje de carga
if(porcent0, porcent1, porcent2, porcent3 <= 0)
{ // Evitamos valores negativos
porcent0, porcent1, porcent2, porcent3 = 0;
}
lcd.createChar(0, Bateria);
lcd.setCursor(0,0);
lcd.print("1");
lcd.write (byte (0));
lcd.print(" ");
lcd.print(Volt0);
lcd.print("V");
lcd.createChar(1, Carga1);
lcd.setCursor(14,0);
lcd.write (byte (1));
lcd.print(" ");
lcd.print(porcent0);
lcd.print("%");
lcd.createChar(0, Bateria);
lcd.setCursor(0,1);
lcd.print("2");
lcd.write (byte (0));
lcd.print(" ");
lcd.print(Volt1);
lcd.print("V");
lcd.createChar(1, Carga1);
lcd.setCursor(14,1);
lcd.write (byte (1));
lcd.print(" ");
lcd.print(porcent1);
lcd.print("%");
lcd.createChar(0, Bateria);
lcd.setCursor(0,2);
lcd.print("3");
lcd.write (byte (0));
lcd.print(" ");
lcd.print(Volt2);
lcd.print("V");
lcd.createChar(1, Carga1);
lcd.setCursor(14,2);
lcd.write (byte (1));
lcd.print(" ");
lcd.print(porcent2);
lcd.print("%");
lcd.createChar(0, Bateria);
lcd.setCursor(0,3);
lcd.print("4");
lcd.write (byte (0));
lcd.print(" ");
lcd.print(Volt3);
lcd.print("V");
lcd.createChar(1, Carga1);
lcd.setCursor(14,3);
lcd.write (byte (1));
lcd.print(" ");
lcd.print(porcent3);
lcd.print("%");
delay(1000);
}
El código de la animación:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 in PCF8574 by NXP and Set to 0x3F in PCF8574A by Ti
LiquidCrystal_I2C lcd(0x27, 20, 4);
byte CargaBateria0[8] = {
B01110,
B10001,
B10001,
B10001,
B10001,
B10001,
B10001,
B11111
};
byte CargaBateria1[8] = {
B01110,
B10001,
B10001,
B10001,
B10001,
B10001,
B11111,
B11111
};
byte CargaBateria2[8] = {
B01110,
B10001,
B10001,
B10001,
B10001,
B11111,
B11111,
B11111
};
byte CargaBateria3[8] = {
B01110,
B10001,
B10001,
B10001,
B11111,
B11111,
B11111,
B11111
};
byte CargaBateria4[8] = {
B01110,
B10001,
B10001,
B11111,
B11111,
B11111,
B11111,
B11111
};
byte CargaBateria5[8] = {
B01110,
B10001,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111
};
byte CargaBateria6[8] = {
B01110,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111
};
int t = 500;
void setup() {
lcd.init(); // inicializamos el LCD.
lcd.backlight(); // encendemos la retroiluminación.
Serial.begin(9600);
for(;;){ //for(int i = 0; i<8; i++)
preLoad(); }
}
void loop() { }
void preLoad()
{
lcd.clear();
lcd.createChar(0,CargaBateria0);
lcd.setCursor(3,0);
lcd.write(byte(0));
delay(t);
lcd.clear();
lcd.createChar(1,CargaBateria1);
lcd.setCursor(3,0);
lcd.write(byte(1));
delay(t);
lcd.clear();
lcd.createChar(2,CargaBateria2);
lcd.setCursor(3,0);
lcd.write(byte(2));
delay(t);
lcd.clear();
lcd.createChar(3,CargaBateria3);
lcd.setCursor(3,0);
lcd.write(byte(3));
delay(t);
lcd.clear();
lcd.createChar(4,CargaBateria4);
lcd.setCursor(3,0);
lcd.write(byte(4));
delay(t);
lcd.clear();
lcd.createChar(5,CargaBateria5);
lcd.setCursor(3,0);
lcd.write(byte(5));
delay(t);
lcd.clear();
lcd.createChar(6,CargaBateria6);
lcd.setCursor(3,0);
lcd.write(byte(6));
delay(t);
}
Pues bien estos son los códigos y voy perdido pues e intentado poner la animación pero no se como hacer para que Haga lo que quiero, gracias de antemano. salu2.