Si, lo se, estoy usando el pin 6 de Arduino uno, ya estoy consiguiendo que inicie el PWM, (batería sin carga) pero no consigo que se apague (bateria cargada) cuando termina de cargar.
Nota: ya enciende y apaga el PWM con la nuevo ajuste de variables.
#define solar_current_in A0
#define solar_voltage_in A1
#define battery_voltage_in A2
#define solar_min_voltage 14.100
#define battery_min_voltage 11.600
#define charging_current 0.400
#define bulk_voltage_max 12.400
#define bulk_voltage_min 12.000
#define absorption_voltage 12.500
#define absorption_max_current 0.260
#define absorption_min_current 0.106
#define float_voltage 12.600
#define float_voltage_max 12.600
#define float_voltage_min 12.500
#define float_max_current 0.120
#define load_enable 2
#define PWM_out 6
#define LCD_refresh_rate 1000
int rele8 = 8;
int rele9 = 9;
int rele10 = 10;
int rele11 = 11;
const int sensorPin = A0;
const unsigned long TIEMPO_CURRENT = 10UL;
float sensorValue = 0;
float currentValue = 0;
float voltsporUnidade = 0.0041012855327468;
float ruido = 0.00;
unsigned long tiempocurrent;
int Current;
double Vout = 0;
const int sensor0 = A5;
const int sensor1 = A4;
const int sensor2 = A3;
const unsigned long TIEMPO_LECTURAS = 3000UL;
byte BULK = 0;
byte ABSORPTION = 1;
byte FLOAT = 2;
byte mode = 0;
float bat_voltage = 0;
int pwm_value = 3;
float solar_current = 0;
float current_factor = 0.100;
float solar_voltage = 0;
float solar_power = 0;
String load_status = "OFF";
int pwm_percentage = 0;
unsigned int before_millis = 0;
unsigned int now_millis = 0;
String mode_str = "BULK";
float adc_voltage0 = 0.0;
float in_voltage0 = 0.0;
float adc_voltage1 = 0.0;
float in_voltage1 = 0.0;
float adc_voltage2 = 0.0;
float in_voltage2 = 0.0;
float R1 = 30000.0;
float R2 = 7500.0;
float R3 = 30000.0;
float R4 = 7500.0;
float R5 = 30000.0;
float R6 = 7500.0;
float ref_voltage0 = 5.0;
float ref_voltage1 = 5.0;
float ref_voltage2 = 5.0;
int adc_value0 = 0;
int adc_value1 = 0;
int adc_value2 = 0;
unsigned long tiempolecturas;
void setup() {
Serial.begin(9600);
pinMode(rele8, OUTPUT);
pinMode(rele9, OUTPUT);
pinMode(rele10, OUTPUT);
pinMode(rele11, OUTPUT);
pinMode(solar_voltage_in, INPUT);
pinMode(solar_current_in, INPUT);
pinMode(battery_voltage_in, INPUT);
pinMode(PWM_out, OUTPUT);
digitalWrite(PWM_out, LOW);
pinMode(load_enable, OUTPUT);
digitalWrite(load_enable, LOW);
TCCR1B = TCCR1B & B11111000 | B00000001; // PWM 31372.55 Hz
Serial.begin(9600);
before_millis = millis;
}
void loop() {
if (millis() - tiempolecturas > 3000UL) {
if (millis() - tiempocurrent > 10UL) {
currentValue = 0;
for (int index = 0; index < 5000; index++) {
sensorValue = analogRead(sensorPin);
sensorValue = (sensorValue - 516.39) * voltsporUnidade;
currentValue = currentValue + (sensorValue / 100) * 1000;
delay(1);
}
currentValue = currentValue / 5000;
Serial.print("corrente = " );
currentValue = currentValue - ruido;
Serial.print(currentValue, 3);
Serial.print("\n" );
Serial.println("pwm ");
Serial.print(pwm_percentage);
Serial.print("\n" );
//Serial.println("");
if (Current > 0.100) {
digitalWrite(rele8, HIGH);
} if (Current < 0.300) {
digitalWrite(rele8, LOW);
}
solar_voltage = get_solar_voltage(15);
bat_voltage = get_battery_voltage(15);
solar_current = get_solar_current(15);
solar_power = bat_voltage * solar_current;
pwm_percentage = map(pwm_value, 0, 255, 0, 100);
now_millis = millis();
if (now_millis - before_millis > LCD_refresh_rate)
{
before_millis = now_millis;
Serial.println("solar voltaje");
Serial.println(solar_voltage, 3);
Serial.println("pwm");
Serial.println(pwm_percentage);
Serial.println("solar_current =");
Serial.println(solar_current, 3);
Serial.println(load_status);
Serial.println(bat_voltage, 3);
}
if (bat_voltage < battery_min_voltage) {
digitalWrite(load_enable, LOW);
load_status = "OFF";
}
else {
digitalWrite(load_enable, HIGH);
load_status = "ON";
}
////////////////////////////////BATERIA 1 ///////////////////////////////////////////
adc_value0 = analogRead(sensor0);
adc_voltage0 = (adc_value0 * ref_voltage0) / 1024.0;
in_voltage0 = adc_voltage0 / (R2 / (R1 + R2));
if (in_voltage0 > 16.10) {
digitalWrite(rele9, HIGH);
}
if (in_voltage0 < 15.80) {
digitalWrite(rele9, LOW);
}
//////////////////////BATERIA 2 /////////////////////////////////////////////////////
adc_value1 = analogRead(sensor1);
adc_voltage1 = (adc_value1 * ref_voltage1) / 1024.0;
in_voltage1 = adc_voltage1 / (R4 / (R3 + R4));
if (in_voltage1 > 12.70) {
digitalWrite(rele10, HIGH);
}
if (in_voltage1 < 12.40) {
digitalWrite(rele10, LOW);
}
///////////////////////BATERIA 3 /////////////////////////////////////////////////
adc_value2 = analogRead(sensor2);
adc_voltage2 = (adc_value2 * ref_voltage2) / 1024.0;
in_voltage2 = adc_voltage2 / (R6 / (R5 + R6));
if (in_voltage1 > 8.90) {
digitalWrite(rele11, HIGH);
}
if (in_voltage1 < 8.20) {
digitalWrite(rele11, LOW);
}
/////////////////////////// FLOAT ///////////////////////////
if (mode == FLOAT) {
if (bat_voltage < float_voltage_min) {
mode = BULK;
mode_str = "BULK";
}
else {
if (solar_current > float_max_current) {
mode = BULK;
mode_str = "BULK";
}
else {
if (bat_voltage > float_voltage) {
pwm_value--;
pwm_value = constrain(pwm_value, 0, 254);
}
else {
pwm_value++;
pwm_value = constrain(pwm_value, 0, 254);
}
}
analogWrite(PWM_out, pwm_value);
}
}
else {
if (bat_voltage < bulk_voltage_min) {
mode = BULK;
mode_str = "BULK";
}
else if (bat_voltage > bulk_voltage_max) {
mode_str = "ABSORPTION";
mode = ABSORPTION;
}
//////////////////////////// BULK /////////////////////
if (mode == BULK) {
if (solar_current > charging_current) {
pwm_value--;
pwm_value = constrain(pwm_value, 0, 254);
}
else {
pwm_value++;
pwm_value = constrain(pwm_value, 0, 254);
}
analogWrite(PWM_out, pwm_value);
}
///////////////////////// ABSORPTION //////////////////
if (mode == ABSORPTION) {
if (solar_current > absorption_max_current) {
pwm_value--;
pwm_value = constrain(pwm_value, 0, 254);
}
else {
if (bat_voltage > absorption_voltage) {
pwm_value++;
pwm_value = constrain(pwm_value, 0, 254);
}
else {
pwm_value--;
pwm_value = constrain(pwm_value, 0, 254);
}
if (solar_current < absorption_min_current) {
mode = FLOAT;
mode_str = "FLOAT";
}
}
analogWrite(PWM_out, pwm_value);
}
}
Serial.println("sensor 0 ");
Serial.print(in_voltage0, 2);
Serial.println("V");
Serial.println("sensor 1 ");
Serial.print(in_voltage1, 2);
Serial.println("V");
Serial.println("sensor 2 ");
Serial.print(in_voltage2, 2);
Serial.println("V");
tiempolecturas = millis();
tiempocurrent = millis();
}
}
}
//////////////////// fin vood loop(); ///////////////////////
float get_solar_voltage(int n_samples)
{
float voltage = 0;
for (int i = 0; i < n_samples; i++)
{
voltage += (analogRead(solar_voltage_in) * (5.0 / 1023.0) * 5.610);
}
voltage = voltage / n_samples;
if (voltage < 0) {
voltage = 0;
}
return (voltage);
}
float get_battery_voltage(int n_samples)
{
float voltage = 0;
for (int i = 0; i < n_samples; i++)
{
voltage += (analogRead(battery_voltage_in) * (5.0 / 1023.0) * 4.757);
}
voltage = voltage / n_samples;
if (voltage < 0) {
voltage = 0;
}
return (voltage);
}
float get_solar_current(int n_samples)
{
float Sensor_voltage;
float current = 0;
for (int i = 0; i < n_samples; i++)
{
Sensor_voltage = analogRead(solar_current_in) * (5.0 / 1023.0);
current = current + (Sensor_voltage - 2.51503) / current_factor;
}
current = current / n_samples;
if (current < 0) {
current = 0;
}
return (current);
}
float in_voltage(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
Es muy difícil ajustarlo, tengo tres días haciendo ajustes, ahora seria bueno minimizarlo creo que hay muchos renglones innecesarios.
Saludos.