Hola como va, este programa esta diseñado para abasteser un sistema fotovoltaico cuando las baterias se descargan, ademas de ello el generado se puede encender mediante un interruptor y usarlo para soldar, me gustaria saber sus opiniones! Saludos.
//////////////////
/*Esteban Werner*/
//////////////////
// Libraries
#include <Servo.h>
#include <ZMPT101B.h>
// Declaration of global control variables for each case
bool generator_control = false; // Generator Control
bool welder_control = false; // Welder Control
bool battery_charging_control = false; // Charge Of Batteries
bool automatic_load_control = false; // Automatic Load Batteries
bool blink_light_control = false; // Blink
//Bye bye Magic Numbers
const int success_start_voltaje = 180;
const int overload_start_voltaje = 280;
const int max_time_for_5_starts = 300000;
const int low_batery_voltaje = 22.5;
const int high_batery_voltaje = 24;
const int minimun_welder_voltaje = 20;
const int max_starts_trys = 5;
const int max_shotdowns_trys = 5;
// Sensors and Switches
const int SVB = A0; // 24VDC Voltage Sensor
const int I1 = A2; // Switch to turn on the generator
const int I2 = A3; // Switch to turn on the welder
const int I3 = A4; // Switch to restart
// Voltage threshold
const int high_state = 4;
// Sensor 220V and Servos
ZMPT101B zmpt(A1); // 220V Sensor
Servo myServo1; // Gasoil
Servo myServo2; // Decompressor
// Relays
const int relay1 = 23;
const int relay2 = 25;
const int relay3 = 27;
const int relay4 = 29;
const int relay5 = 31;
const int relay6 = 33;
const int relay7 = 35;
const int relay8 = 37;
const int relay16 = 41;
// Define variables
unsigned long timeStart; // Variable to store the start time
int execution_counter = 0; // ON counter
int shutdown_counter = 0; // OFF counter
void setup() {
Serial.begin(9600); // Serial Communication
myServo1.attach(8); // Gasoil Pin
myServo2.attach(2); // Decompressor Pin
// Initialization of analog pins
pinMode(I1, INPUT); // Start Switch
pinMode(I2, INPUT); // Welder Switch
pinMode(I3, INPUT); // Restart Switch
pinMode(SVB, INPUT); // DC 24V Voltage Sensor
// Initialization of digital pins
pinMode(relay1, OUTPUT); // Dead Pin
pinMode(relay2, OUTPUT); // Generator Start
pinMode(relay3, OUTPUT); // Generator Start
pinMode(relay4, OUTPUT); // Generator Start
pinMode(relay5, OUTPUT); // Welding Light
pinMode(relay6, OUTPUT); // Battery Light
pinMode(relay7, OUTPUT); // Reset Light
pinMode(relay8, OUTPUT); // Battery Contactor
pinMode(relay16, OUTPUT); // Generator Light
// Relays Off
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
digitalWrite(relay3, HIGH);
digitalWrite(relay4, HIGH);
digitalWrite(relay5, HIGH);
digitalWrite(relay6, HIGH);
digitalWrite(relay7, HIGH);
digitalWrite(relay8, HIGH);
digitalWrite(relay16, HIGH);
// Servo
digitalWrite(8, LOW);
digitalWrite(2, LOW);
// Counter
timeStart = millis(); // Save the start time
}
void loop() {
unsigned long tiempoActual = millis(); // Get the current time
// Check if 5 minutes have passed (300,000 milliseconds)
if (tiempoActual - timeStart >= max_time_for_5_starts) {
// Reset the counter and update the start time
execution_counter = 0;
shutdown_counter = 0;
timeStart = tiempoActual;
}
if (execution_counter < max_starts_trys && shutdown_counter < max_shotdowns_trys) {
// Sensor 220V
float voltage = zmpt.getRmsVoltage(); // Library for countries with 110V, so multiply by 2
float realvoltage = voltage * 2.182333490788852;
// Storage of analog data in variable
int valueI1 = analogRead(I1);
int valueI2 = analogRead(I2);
int valueI3 = analogRead(I3);
int value24vdc= analogRead(SVB);
// Calculation of voltage and/or switch state
int estadoI1 = (valueI1/1023)*5;
int estadoI2 = (valueI2/1023)*5;
int estadoI3 = (valueI3/1023)*5;
int estado24vdc = (value24vdc/1023.0)*25.0;
// Standby State
if(automatic_load_control == false && battery_charging_control == false && welder_control == false && generator_control == false){
Serial.println("Standby State ""info for +");
}else{
char info = 'info';
if (Serial.available() > 0) {
char received = Serial.read();
if (received == info) {
data();
}
}
}
/*PANIC BUTTON*/
if(estadoI3 > high_state){
deadstate();
while(true){
digitalWrite(relay7, LOW); // Reset Light // Emergency
Serial.println("Panic Button");
int valueI3 = analogRead(I3);
int estadoI3 = (valueI3/1023)*5;
if(estadoI3 < high_state){
digitalWrite(relay7, HIGH); // Reset Light OFF
delay(100);
asm volatile (" jmp 0"); // Jump to the beginning of the program
}
}
}
/*Control to start the generator*/
if(generator_control == false && automatic_load_control == false){
if(estadoI1 > high_state){
generatorstart();
}
}else{
if(generator_control == true && automatic_load_control == false){
if((estadoI1 < high_state && welder_control == false)){
Serial.println("Error: Braked by switch");
generatorbrake();
}else{
if(welder_control == false && generator_control == true){
if (realvoltage < success_start_voltaje){
Serial.println("Error: Braked by 220v Sensor");
generatorbrake();
}
}
}
}
}
/*CONTROL WHEN WELDING IS USED*/
if(welder_control == false){
if (generator_control == true && estadoI2 > high_state){
digitalWrite(relay8, HIGH); // Disconnect Battery Power
digitalWrite(relay6, HIGH); // Battery Charging Light OFF
digitalWrite(relay5, HIGH); // Welding Light OFF (To be able to use it)
battery_charging_control = false; // Control Variable
welder_control = true; // Control Variable
Serial.println("Using Welder");
}}else{
if(estadoI2 < high_state && welder_control == true){
welder_control = false;
digitalWrite(relay8, LOW); // Connect Battery Power
digitalWrite(relay6, LOW); // Battery Charging Light ON
digitalWrite(relay5, LOW); // Welding Light ON (To NOT use it)
Serial.println("Leaving Welder Switch");
battery_charging_control = true;
} else{
if(realvoltage < minimun_welder_voltaje){
digitalWrite(relay8, HIGH); // Disconnect Battery Power
battery_charging_control = false; // Control Variable
welder_control = false; // Control Variable
Serial.println("Leaving Welder 220v Sensor");
generatorbrake();
}
}
}
/*CONTROL WHEN STARTING AUTOMATICALLY*/
if(generator_control == false && battery_charging_control == false && welder_control == false && automatic_load_control == false){
if (estado24vdc < low_batery_voltaje && welder_control == false && generator_control == false && automatic_load_control == false && welder_control == false){
generatorstart();
if(generator_control == true){
automatic_load_control = true;
Serial.println("The generator was off and turned on to charge the batteries");
}else{
Serial.println("Error: The generator was off and attempted to turn on to charge the batteries");
}
}
}else{
if(estado24vdc > high_batery_voltaje && estadoI1 < high_state && automatic_load_control == true && generator_control == true && welder_control == false){
generatorbrake();
automatic_load_control = false;
}
}
}else {
limitExceeded();
}
}
void generatorstart(){
execution_counter++; // Increment the execution counter
Serial.println("Starting is being executed");
blink_light_control = true;
while(blink_light_control == true){
digitalWrite(relay5, LOW); // Light
digitalWrite(relay6, LOW); // Light
digitalWrite(relay7, LOW); // Light
digitalWrite(relay16, LOW); // Light
delay(1000);
digitalWrite(relay5, HIGH); // Light
digitalWrite(relay6, HIGH); // Light
digitalWrite(relay7, HIGH); // Light
digitalWrite(relay16, HIGH); // Light
delay(1000);
}
digitalWrite(relay2, LOW); // Key Contact
digitalWrite(relay3, LOW); // Key Contact
// Diesel
myServo1.write(50); // Move the servo 50 degrees for startup
delay(1000);
//Start
digitalWrite(relay4, LOW); // Stick relays to start the generator
delay(500); // Time to start the generator
digitalWrite(relay4, HIGH); // Unstick relays to start the generator
delay(500);
// Decompressor
myServo2.write(180); // Decompressor
delay(1000); // Time to decompress
myServo2.write(70); // Go back to its place
delay(10000); // Delay for it to reach 180V
// Various
float voltage = zmpt.getRmsVoltage(); // Library for countries with 110V, so multiply by 2
float realvoltage = voltage * 2;
// If the generator throws voltage, update control variable to TRUE
if(realvoltage > success_start_voltaje){
Serial.println("El voltaje es mayor a 180");
if(realvoltage < overload_start_voltaje){
Serial.println("El voltaje es menor a 280, por lo tanto se da como exitoso");
blink_light_control = false;
generator_control = true; // Update Control Variable since the generator is ON
digitalWrite(relay16, LOW); // Generator Light on
Serial.println("Successful Startup");
}else{
blink_light_control = false;
generatorbrake();
Serial.println("Error: Startup Failed, voltage greater than 280V");
}
}else{
blink_light_control = false;
generatorbrake();
Serial.println("Error: Startup Failed, voltage less than 180V");
}
if(generator_control == true){
digitalWrite(relay8, LOW); // Connect Battery Power
digitalWrite(relay6, LOW); // Battery Charging Light
digitalWrite(relay5, LOW); // Light DO NOT USE WELDER (On)
battery_charging_control = true;
}
delay(1000);
}
void generatorbrake(){
if (shutdown_counter < max_shotdowns_trys){
Serial.println("Shutdown is being executed");
digitalWrite(relay8, HIGH); // Disconnect Battery Power
battery_charging_control = false; // Update Control Variable
digitalWrite(relay6, HIGH); // Battery Charging Light OFF
digitalWrite(relay5, HIGH); // Welding Light OFF
delay(1000);
myServo1.write(90); // Disconnect diesel
delay(11000); // Delay for the Generator to stop, and then update, or not, the control variable
float voltage = zmpt.getRmsVoltage(); // Library for countries with 110V, so multiply by 2
float realvoltage = voltage * 2; // " " " " " " " "
shutdown_counter++; // Increment the Shutdown counter
if(realvoltage < success_start_voltaje){
generator_control = false;
digitalWrite(relay16, HIGH); // Generator Light Off
Serial.println("Generator Off");
}else{
Serial.println("Error: Tried to Turn Off, did not turn off, so it tries again");
generatorbrake();
}
digitalWrite(relay2, HIGH); // Release Contact
digitalWrite(relay3, HIGH); // Release Contact
digitalWrite(relay5, HIGH); // Welding Light Off
}else{
limitExceeded();
}
}
void limitExceeded(){
Serial.println("The limit of executions has been exceeded. Blocking...");
while (true) {
Serial.println("Reset");
digitalWrite(relay7, LOW); // Reset Light ON
digitalWrite(relay5, HIGH); // Welding Light OFF
digitalWrite(relay6, HIGH); // Battery Light OFF
digitalWrite(relay16, HIGH); // Generator Light OFF
int valueI3 = analogRead(I3);
int estadoI3 = (valueI3/1023)*5;
if(estadoI3 > high_state){
digitalWrite(relay7, HIGH); // Reset Light OFF
delay(100);
asm volatile (" jmp 0"); // Jump to the beginning of the program
}
}
}
void data(){
// Voltage Sensor 220V
float voltage = zmpt.getRmsVoltage(); // Library for 110V countries, so multiply by 2
float realvoltage = voltage * 2.182333490788852;
// Analog data storage in variables
int valueI1 = analogRead(I1);
int valueI2 = analogRead(I2);
int valueI3 = analogRead(I3);
int value24vdc = analogRead(SVB);
// Voltage and/or Switch state calculation
float estadoI1 = (valueI1 / 1023.0) * 5.0;
float estadoI2 = (valueI2 / 1023.0) * 5.0;
float estadoI3 = (valueI3 / 1023.0) * 5.0;
float estado24vdc = (value24vdc / 1023.0) * 25.0;
Serial.println("/////////////////////////");
Serial.print("Generator: ");
if (generator_control > 0){
Serial.println("On");
} else {
Serial.println("Off");
}
Serial.print("AutomaticLoad: ");
if (automatic_load_control > 0){
Serial.println("On");
} else {
Serial.println("Off");
}
Serial.print("Batteries: ");
if (battery_charging_control > 0){
Serial.println("On");
} else {
Serial.println("Off");
}
Serial.print("Welder: ");
if (welder_control > 0){
Serial.println("On");
} else {
Serial.println("Off");
}
Serial.println("/////////////////////////");
Serial.print("Switch 1: ");
Serial.print(estadoI1);
Serial.println("V");
Serial.print("Switch 2: ");
Serial.print(estadoI2);
Serial.println("V");
Serial.print("Switch 3: ");
Serial.print(estadoI3);
Serial.println("V");
Serial.print("Sensor 24VDC: ");
Serial.print(estado24vdc);
Serial.println("V");
Serial.print("Sensor 220VAC: ");
Serial.print(realvoltage);
Serial.println("V");
}
void deadstate(){
digitalWrite(relay8, HIGH); // Battery charging contactor
delay(500);
// Various
myServo1.write(90); // Diesel unplug
myServo2.write(70); // Decompressor
// Relay Off
digitalWrite(relay1, HIGH); // Dead
digitalWrite(relay2, HIGH); // Contact
digitalWrite(relay3, HIGH); // Contact
digitalWrite(relay4, HIGH); // Start
digitalWrite(relay5, HIGH); // Light
digitalWrite(relay6, HIGH); // Light
digitalWrite(relay7, HIGH); // Light
// Relay 8 does not turn off as it is later turned on to indicate an error
digitalWrite(relay16, HIGH); // Light
// Global control variables set to FALSE
bool ControlDeGenerador = false;
bool ControlSoldadora = false;
bool ControlCargaBaterias = false;
bool ControlCargaAUTOMATICA = false;
}