#include <Time.h>
#include <LiquidCrystal.h>
#include <Wire.h>
#define V_LOAD_PIN A0
#define R_LOAD 5.5
#define FINAL_VOLTAGE 0.2
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
float joules = 0;
float voltage = 0;
float temp = 0;
uint8_t hours = 0;
uint8_t mins = 0;
uint8_t lastSecond;
bool batteryAttached = false;
bool testComplete = false;
bool Bexecutado = false;
time_t startTime = 0;
void setup() {
Serial.begin(9600);
pinMode(V_LOAD_PIN, INPUT);
lcd.begin(16, 2);
// INICIO PARTE DO CODIGO B //
lcd.print("2009 VlxsLAB");
delay(1000);
lcd.clear();
// FIM PARTE DO CODIGO B ///
lcd.print(" Attach Battery");
lcd.setCursor(0, 1);
lcd.print(" to begin test");
time_t t = now();
lastSecond = second(t);
}
void loop() {
voltage = 5.0 * ((float) analogRead(V_LOAD_PIN)) / 1024.0;
if (voltage < 0.03) {
batteryAttached=false;
Bexecutado=false;
lcd.clear();
lcd.setCursor(0, 0); //Print in the first line:
lcd.print("Sem baterias");
}
if (batteryAttached) {
if (!Bexecutado) {
// INICIO DO CODIGO B///
lcd.clear();
lcd.setCursor(0, 0); //Print in the first line:
lcd.print("Testador Bateria");
lcd.setCursor(0, 1); //Now in the second line
lcd.print(analogRead(0)*5.00/1023.00); //we print the voltage
lcd.print("V =>");
lcd.setCursor(9,1);
if ((analogRead(0)*5.00/1023.00) > 1.40) //And choose what is the battery status
{
lcd.print("Prefeito");
}
else if ((analogRead(0)*5.00/1023.00) > 1.20)
{
lcd.print("Bom");
}
else if ((analogRead(0)*5.00/1023.00) > 1.20)
{
lcd.print("Fraca");
}
else if ((analogRead(0)*5.00/1023.00) > 0.30)
{
lcd.print("Muito Fraca");
}
else
{
lcd.print("");
}
delay(3000); //Troquei o tempo de espera no delay para visualizar antes de comecar o teste
Bexecutado=true;
// FIM DO CODIGO B //
}
if (testComplete) {
updateDisplay();
} else {
time_t t = now()-startTime;
uint8_t sec = second(t);
if (sec != lastSecond) {
lastSecond = sec;
hours = hour(t);
mins = minute(t);
voltage = 5.0 * ((float) analogRead(V_LOAD_PIN)) / 1023.0;
float current = voltage / R_LOAD;
joules += voltage * current;
int val = analogRead(A1);
float voltagetemp = (val) * 5;
voltagetemp /= 1024.0;
temp = (voltagetemp - 0.5) * 10 ;
updateDisplay();
Serial.print(t);
Serial.print(",");
Serial.print(voltage);
Serial.print(",");
Serial.print(current);
Serial.print(",");
Serial.print(joules);
Serial.print(",");
Serial.print(temp);
Serial.println();
if (voltage < FINAL_VOLTAGE) {
testComplete = true;
}
}
}
} else {
voltage = 5.0 * ((float) analogRead(V_LOAD_PIN)) / 1024.0;
if (voltage > 0.02) {
startTime = now();
batteryAttached = true;
Serial.println("time,voltage,current,joules,temp");
}
}
}
// Update the LCD with the following format:
//
// +----------------+
// |99999.9J 99.99Wh|
// |9.99V 99^C 59:59|
// +----------------+
void updateDisplay() {
char row[32];
memset(row, 0, 17);
float wattHours = joules / 3600;
if (joules < 9999)
fmtDouble(joules, 2, &row[0], 8);
else
fmtDouble(joules, 1, &row[0], 8);
strcat(row, "J");
char whStr[9];
fmtDouble(wattHours, 2, whStr, sizeof(whStr));
sprintf(&row[9], "%5sWh", whStr);
for (int i=0;i<16;i++)
if (row[i] == 0)
row[i] = ' ';
lcd.setCursor(0, 0);
lcd.print(row);
if (testComplete) {
lcd.setCursor(0, 1);
lcd.print(" Test Complete! ");
} else {
memset(row, 0, 17);
fmtDouble(voltage, 2, &row[0], 8);
strcat(row, "V");
fmtDouble(temp, 0, &row[6], 8);
strcat(&row[6], "\xDF");
strcat(&row[6], "C");
uint8_t degree = 0xEF;
sprintf(&row[11], "%02d:%02d", hours, mins);
for (int i=0;i<16;i++)
if (row[i] == 0)
row[i] = ' ';
lcd.setCursor(0, 1);
lcd.print(row);
}
}
void fmtDouble(double val, byte precision, char *buf, unsigned bufLen = 0xffff);
unsigned fmtUnsigned(unsigned long val, char *buf, unsigned bufLen = 0xffff, byte width = 0);
unsigned fmtUnsigned(unsigned long val, char *buf, unsigned bufLen, byte width) {
if (!buf || !bufLen)
return(0);
// produce the digit string (backwards in the digit buffer)
char dbuf[10];
unsigned idx = 0;
while (idx < sizeof(dbuf)) {
dbuf[idx++] = (val % 10) + '0';
if ((val /= 10) == 0)
break;
}
// copy the optional leading zeroes and digits to the target buffer
unsigned len = 0;
byte padding = (width > idx) ? width - idx : 0;
char c = '0';
while ((--bufLen > 0) && (idx || padding)) {
if (padding)
padding--;
else
c = dbuf[--idx];
*buf++ = c;
len++;
}
// add the null termination
*buf = '\0';
return(len);
}
void fmtDouble(double val, byte precision, char *buf, unsigned bufLen) {
if (!buf || !bufLen)
return;
const byte maxPrecision = 6;
if (precision > maxPrecision)
precision = maxPrecision;
if (--bufLen > 0) {
// check for a negative value
if (val < 0.0) {
val = -val;
*buf = '-';
bufLen--;
}
// compute the rounding factor and fractional multiplier
double roundingFactor = 1;
unsigned long mult = 1;
for (byte i = 0; i < precision; i++) {
roundingFactor /= 10.0;
mult *= 10;
}
if (bufLen > 0) {
// apply the rounding factor
val += roundingFactor;
// add the integral portion to the buffer
unsigned len = fmtUnsigned((unsigned long)val, buf, bufLen);
buf += len;
bufLen -= len;
}
// handle the fractional portion
if ((precision > 0) && (bufLen > 0)) {
*buf++ = '.';
if (--bufLen > 0)
buf += fmtUnsigned((unsigned long)((val - (unsigned long)val) * mult), buf, bufLen, precision);
}
}
// null-terminate the string
*buf = '\0';
}
O que pretendo com este protejo, é fazer um testador de pilhas.
Que irá mostrar a tensão da pilha, Watt hora da pilha, temperatura do ambiente, Joules usados da pilha e o tempo que o teste demorou a ser realizado.
É um protejo em constante desenvolvimento, pois pretendo incorporar, LEDS, e a iteração com o sistema Android.
Que irá mostrar a tensão da pilha, Watt hora da pilha, temperatura do ambiente, Joules usados da pilha e o tempo que o teste demorou a ser realizado.
É um protejo em constante desenvolvimento, pois pretendo incorporar, LEDS, e a iteração com o sistema Android.
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.