Hola buenos días, tengo un programa para leer el desfase de corriente:
Tengo conectada una pantalla lcd liquid crystal:
#include <LiquidCrystal.h>
#include <math.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int pin = 13;
const int capacitor = 6;
float rads = 57.29577951; // 1 radian = approx 57 deg.
float degree = 360;
float frequency = 50;
float nano = 1 * pow (10,-6); // Multiplication factor to convert nano seconds into seconds
// Define floats to contain calculations
float pf;
float angle;
float pf_max = 0;
float angle_max = 0;
int ctr;
//float positivo;
void setup()
{
pinMode(pin, INPUT);
pinMode(capacitor, OUTPUT);
lcd.begin(16, 2);
}
void loop()
{
for (ctr = 0; ctr <= 10; ctr++) // Perform 4 measurements then reset
{
// 1st line calculates the phase angle in degrees from differentiated time pulse
// Function COS uses radians not Degree's hence conversion made by dividing angle / 57.2958
angle = ((((pulseIn(pin, HIGH)) * nano)* degree)* frequency);
pf = cos(angle / rads);
if (angle > angle_max) // Test if the angle is maximum angle
{
angle_max = angle; // If maximum record in variable "angle_max"
pf_max = cos(angle_max / rads); // Calc PF from "angle_max"
}
}
//if (angle_max > 360) // If the calculation is higher than 360 do following...
if (angle_max > 360)
{
angle_max = 0; // assign the 0 to "angle_max"
pf_max = 1; // Assign the Unity PF to "pf_max"
}
if (angle_max == 0) // If the calculation is higher than 360 do following...
{
angle_max = 0; // assign the 0 to "angle_max"
pf_max = 1; // Assign the Unity PF to "pf_max"
}
/*if(pf_max<=0){
pf_max=0;
}*/
/*if((pf_max>=0.82) && (pf_max <= 0.83)){
digitalWrite(capacitor, HIGH);
}
else{
digitalWrite(capacitor, LOW);
}
*/
if((pf_max>=0.92) && (pf_max <= 0.99)){
digitalWrite(capacitor, LOW);
}
else{
digitalWrite(capacitor, HIGH);
}
//positivo = pf_max;
/*Serial.print(angle_max, 2); // Print the result
Serial.print(",");
Serial.println(pf_max, 2);
Serial.println(fabs(caca));*/
lcd.clear();
lcd.setCursor(0,0);
lcd.print("PF=");
lcd.setCursor(4,0);
lcd.print(pf_max);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print("Ph-Shift=");
lcd.setCursor(10,1);
lcd.print(angle_max);
lcd.print(" ");
delay(1000); //tiempo de escaneo
angle = 0; // Reset variables for next test
angle_max = 0;
}
Estoy intentando poner un smoothing o averange para "PF_max" que es el factor de potencia para que no cambie tan bruscamente, pero no puedo hacer que haga uno con float, ya que todos los ejemplos o librerías son con enteros o directamente lo leen desde los pines analógicos.
Hay alguno para floats?