Sei sulla pagina 1di 2

#include <LiquidCrystal.

h>
#include <LCDKeypad.h>
LiquidCrystal lcd(8, 13, 9, 4, 5, 6, 7);

const int currentPin = A1;


const int voltPin = A2;
const unsigned long sampleTime = 100000UL; // sample over
100ms, it is an exact number of cycles for both 50Hz and 60Hz mains
const unsigned long numSamples = 2500UL; // choose
the number of samples to divide sampleTime exactly, but low enough for the ADC to
keep up
const unsigned long sampleInterval = sampleTime/numSamples; // the sampling
interval, must be longer than then ADC conversion time

//Esta variable necesita ser ajustada en funcin al voltaje que te genere la placa.
2.5 voltios seran 514, pero siempre se pierde algo de voltaje en las resistencias
// TODO: Auto-Ajustarlo usando la media de una lista de tomas.
const int adc_zero = 507; //
relative digital zero of the arudino input from ACS712 (could make this a variable
and auto-adjust it)

void setup()
{
Serial.begin(9600);
//Inicializamos la pantalla LCD.
lcd.begin(16, 2);
lcd.clear();
}

void loop()
{
unsigned long currentAcc = 0;
int voltajemax= 0;
unsigned int voltajemin=1025;
unsigned int count = 0;
unsigned long prevMicros = micros() - sampleInterval ;
while (count < numSamples)
{
if (micros() - prevMicros >= sampleInterval)
{
// Leemos voltaje TODO: Comprobar si el voltaje <200 ERROR en conexin
int voltaje=analogRead(voltPin);
//Nos quedamos con el voltaje mximo y mnimo. Solo usaremos el mximo,
//pero no cuesta nada dejar el mnimo para futuros usos incrementando
precisin
if(voltaje<voltajemin) voltajemin=voltaje;
if(voltaje>voltajemax) voltajemax=voltaje;
//Valor puntual de la corriente
int adc_raw = analogRead(currentPin) - adc_zero;
currentAcc += (unsigned long)(adc_raw * adc_raw);
//Incrementamos contadores
count++;
prevMicros += sampleInterval;
}
}
int voltaje=calcularVoltaje(voltajemax,voltajemin);

float rms=calcularRMS(currentAcc,numSamples);
//Pasamos la intensidad en amperios, a Watios multiplicando por el voltaje
int watios=rms*voltaje;

writeLCDValues(watios,voltaje);
}

// Esta funcin calcula la intensidad medida en un rango de muestras.


// Es ajustada en funcin de las tomas y el voltaje del arduino.
// Para mas informacion consultar manual de arduino
float calcularRMS(unsigned long currentAcc,unsigned long numSamples){
// The 75.7576 for the conversion comes from solving for X, 5 V / X = .066 V / 1
A from the
// arduino max analog input voltage and the ACS712 sensitivity on the spec sheet
respectively
float rms = (sqrt((float)currentAcc/(float)numSamples) * (75.7576 / 1024.0));
return rms;
}

//Calculamos el voltaje en funcion a los valores tomados por el voltmetro


// En diferentes tomas. Se extrapola el valor mximo del voltaje
int calcularVoltaje(int vmax,int vmin){
return map(vmax,35, 981,0,227);
}

void writeLCDValues(int watios,int voltios){


String textoWatios="Wats: ";
String textoVoltios="Volt: ";
// Limpiamos LCD
lcd.clear();

//Escribimos los watios en la primera fila


textoWatios= textoWatios + watios ;
lcd.print(textoWatios);

//Escribimos los voltios en la segunda fila


lcd.setCursor(0,1);
textoVoltios = textoVoltios + voltios;
lcd.print(textoVoltios);

Potrebbero piacerti anche