// https://forum.arduino.cc/t/i-have-problem-in-automatic-transfer-switch-
// it-doesnt-update-pins-on-arduino-nano-if-i-
// run-when-main-supply-is-on-it-display-supply-but-donot-update/1247207
#include <TimerOne.h>
// include the library code:
//#include <LiquidCrystal.h> //library for LCD
#include <LiquidCrystal_I2C.h>
#define potinput A0
#define feedbackinput A1
#define Main_Check A2
#define outA 9
#define outB 10
#define main 8
#define ups 7
// initialize the library with the numbers of the interface pins
//LiquidCrystal lcd(12, 11, 6, 5, 4, 3);
LiquidCrystal_I2C lcd(0x27, 20, 4); // Inicializa o objeto LiquidCrystal_I2C
const int Main_Relay = A3;
//const int Ups_Relay = A4;
const int Ups_Relay = 12;
const int Ups_Start = 7;
int f_pwm = 20000; //max 20000
int f_sine = 50;
float sinus[200];
float phi = 3.14;
int flag = 0;
int sample = 0, samples = 0;
int potinputval;
int feedbackinputval;
int A;
int max_power = 800; //max 1023
float invert = 0.0;
int total_sample;
int phase = 0, x = 0;
//----------------------------------------------------------
void setup() {
total_sample = round((((1000000. / f_sine) / (1000000. / f_pwm)) / 2.));
for (int sudut = 0; sudut < total_sample; sudut++)
{
float rad = sudut * (180. / total_sample) * phi / 180;
sinus[sudut] = sin(rad);
}
float t_pwm = (1000000. / f_pwm);
pinMode(potinput, INPUT);
pinMode(feedbackinput, INPUT);
pinMode(Main_Check, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(Ups_Start, OUTPUT);
pinMode(Main_Relay, OUTPUT);
pinMode(Ups_Relay, OUTPUT);
//lcd.begin(20, 4); // set up the LCD's number of columns and rows:
lcd.init(); // inicia o display 16x2
lcd.setCursor(0, 0);
lcd.print(" FINAL YEAR PROJECT ");
lcd.setCursor(0, 1);
lcd.print(" INTELLIGENT UPS ");
delay(100);
A = 0;
Timer1.initialize(t_pwm);
Timer1.attachInterrupt(generate_sinus);
//samples=(total_sample*2/3);
Timer1.pwm(outA, (sinus[sample]* A));
Timer1.pwm(outB, (sinus[sample]* A));
}
//----------------------------------------------------------
void loop() {
// Measure The Votage ***********************************
int sensorValue = analogRead(Main_Check); // read the input on analog pin A2
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float vol = (sensorValue * 5.0) / 1023.0; //
int voltage = vol * 5;
lcd.setCursor(14, 3); //For debug
lcd.print(voltage); //For debug
if (voltage < 5) {
digitalWrite(Ups_Start, HIGH); // 7
digitalWrite(Main_Relay, LOW); // A3
digitalWrite(Ups_Relay, HIGH); // A4 (12)
lcd.setCursor(0, 2);
lcd.print(" MAIN SUPPLY: OFF ");
lcd.setCursor(0, 3);
lcd.print(" UPS : ON ");
}
else {
digitalWrite(Ups_Start, LOW);
digitalWrite(Ups_Relay, LOW);
digitalWrite(Main_Relay, HIGH);
lcd.setCursor(0, 2);
lcd.print(" MAIN SUPPLY: ON ");
lcd.setCursor(0, 3);
lcd.print(" UPS : OFF ");
}
potinputval = analogRead(potinput);
feedbackinputval = analogRead(feedbackinput);
while (feedbackinputval < potinputval ) {
if (A >= max_power) {
potinputval = analogRead(potinput);
feedbackinputval = analogRead(feedbackinput);
}
else {
A = A + 1;
potinputval = analogRead(potinput);
feedbackinputval = analogRead(feedbackinput);
}
}
lcd.setCursor(17, 3); //For debug
lcd.print(sensorValue); //For debug
while (feedbackinputval > potinputval ) {
if (A == 0) {
potinputval = analogRead(potinput);
feedbackinputval = analogRead(feedbackinput);
}
else {
A = A - 1;
potinputval = analogRead(potinput);
feedbackinputval = analogRead(feedbackinput);
}
}
}
//----------------------------------------------------------
void generate_sinus() {
generate();
}
//----------------------------------------------------------
void generate() {
if (sample >= total_sample && flag == 1 ) {
flag = 0;
sample = 1;
//TCCR1A=0b10100000;
}
if (sample >= total_sample && flag == 0) {
flag = 1;
sample = 1;
//TCCR1A=0b10100000;;
}
sample++;
if (flag == 0) {
Timer1.pwm(outA, (sinus[sample]* A));
//PORTB=(0<<PORTB3);
Timer1.pwm(outB, 0);
}
if (flag == 1) {
Timer1.pwm(outA, 0);
//PORTB=(1<<PORTB3);
Timer1.pwm(outB, (sinus[sample]* A));
}
}