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


#include <TimerOne.h>
// include the library code: 
#include <LiquidCrystal.h> //library for LCD 

#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); 


const int Main_Relay = A3;
const int Ups_Relay  = A4;
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.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
  // 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;
  
  if(voltage < 5) 
  
  {
    digitalWrite(Ups_Start, HIGH);
    digitalWrite(Main_Relay, LOW);
    digitalWrite(Ups_Relay, HIGH);
    
    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); 
     }
   }
   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));
          
      }
     
    
 }

 

Welcome, and thanks for posting your code in code tags.

Please explain more clearly and in more detail what your project is about, how it is behaving and how you wanted it to behave.

Please post a schematic and links to the specs or data sheets of any major or unusual components.

I understand English is not your first language, but please try to use complete sentences.

1 Like

im designing arduino based uninterruptible power supply(ups) which will work as follow generate pwm signal at pin 9 and 10 for inverter h bridge also i combined the code to work for automatic transfer switch between main supply and ups in which main relay i connected to pin A3 and ups relay to pin A4 and the pin A2 to chek status for presence of main supply

int sensorValue = analogRead(Main_Check);  // read the input on analog pin
  // 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;
  
  if(voltage < 5) 
  
  {
    digitalWrite(Ups_Start, HIGH);
    digitalWrite(Main_Relay, LOW);
    digitalWrite(Ups_Relay, HIGH);
    
    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  ");
  }

on lcd it display MAIN SUPPLY : ON but when i disconnect a push button nothing change

also when i run simulation while main supply is off it display MAIN SUPPLY : OFF but when i connect a push button for main supply nothing change
help me pleaseee

@silavwe ,

Your other topic on the same subject deleted.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

1 Like

If you want your teacher to give you a good rating , comment on every line of your code.
If he doesn't give a good rating at least we'll know what each line does.

I am having problems visualizing your hardware and sensors. I need an annotated schematic to help. PaulRB asked for this in post #2.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.