Got this message from cyfox3ph : how to add time limit

Good day Sir!
Sir I need advice or suggestion how to make a code.

I have a project to turn on the transfer switch by using two relays.
situation when the voltage become low the solar relay will off and grid relay will on and if the voltage become higher than voltage low but not above voltage full no change in relay still relay grid is on and when the voltage become higher than voltage full the solar relay will on and grid relay will off.

my problem sir, i dont know how to put or code to add a time limit to stop the relay ON to save power consumption in relay. what i want if the relay will ON it will stop after 30 seconds enough to change. the transfer switch

this is my code.

//Control 2 relays using Voltage Sensor
//Constant vars

const int relaySolar = 12; //pin for Solar relay
const int relayGrid = 11; //pin for Grid relay
const int statusLEDPin = 13; //pin for status LED
const int battAnalogPin = A1; //analog input pin for voltage sensor

// vars that will change

int voltReading = 0; //Voltage sensor initial value
int lastVoltReading = 0; //Voltage sensor averaging value
int previousState = 0; //previous switch mode. 0 is battery charge, 1 is grid tie
float battVoltage = 0.0; //initial battery voltage
float battFull = 12.9; //battery full voltage
float battLow = 12.6; //battery low voltage
float battNo = 0.0; //No voltage reading
float voltageReference = 4.98872180; //Using a multimeter check voltage between 5v and ground on arduino and state here
float voltageFactor = 1.0; //Factor used to calculate difference between 5 volts and real 5 volt rail. Initialize to 1

// Declare relays

#define RELAY_ON 1 //relay module active HIGH
#define RELAY_OFF 0

void setup()
{
  Serial.begin(9600);
  digitalWrite(relaySolar, RELAY_OFF); //Deafult relay off
  digitalWrite(relayGrid, RELAY_OFF); //Default relay off
  pinMode(statusLEDPin, OUTPUT); //led pin as output
  pinMode(relaySolar, OUTPUT); //Solar relay pin as output
  pinMode(relayGrid, OUTPUT); //Grid relay pin as output
  voltageFactor = (5.0 / voltageReference);
  delay(100);
}

void loop()
{
  digitalWrite(statusLEDPin, HIGH); //turn on status indicator LED

  //Start reading voltage
  analogRead(battAnalogPin); //Read Voltage pin
  delay(10); // Wait for pin to settle
  voltReading = analogRead(battAnalogPin); //Read more accurate voltage pin reading
  voltReading = (voltReading + lastVoltReading) / 2; //Average last two results
  lastVoltReading = voltReading;
  battVoltage = (voltReading * voltageFactor * 25.0 / 1024.0); //calculate the voltage

  //Print information to Serial
  Serial.print("Voltage: ");
  Serial.print(battVoltage); //print the voltge
  Serial.println("V");

  //Switch relays
  if (battVoltage <= battLow)
  {
    digitalWrite(relaySolar, RELAY_OFF); //OFF Solar supply
    delay(500); //wait half a second
    digitalWrite(relayGrid, RELAY_ON); //ON Grid supply

    //Print information to Serial
    Serial.print("Voltage: ");
    Serial.print(battVoltage); //print the voltge
    Serial.println("V");
    previousState = 0; //Set previous mode to Grid supply
    delay(500);
  }

  else if (battVoltage > battLow && battVoltage < battFull && previousState == 0) //Battery voltage higher than battLow voltage but less than battFull voltage and previous state was charge - keep charging.
  {
    digitalWrite(relaySolar, RELAY_OFF); //OFF Solar supply
    delay(500); //wait half a second
    digitalWrite(relayGrid, RELAY_ON); //ON Grid supply

    //Print information to Serial
    Serial.print("Voltage: ");
    Serial.print(battVoltage); //print the voltge
    Serial.println("V");
    previousState = 0; //Set previous mode to Grid supply
    delay(500);
  }

  else if (battVoltage >= battFull) //Battery high than battery full voltage - go to Solar supply.
  {
    digitalWrite(relaySolar, RELAY_ON); //ON Solar supply
    delay(500); //wait half a second
    digitalWrite(relayGrid, RELAY_OFF); //OFF Grid supply

    //Print information to Serial
    Serial.print("Voltage: ");
    Serial.print(battVoltage); //print the voltge
    Serial.println("V");
    previousState = 1; //Set previous mode to Solar supply
    delay(500);
  }

  else if (battVoltage <= battNo) //No Voltage reading - protect system and disconnect.
  {
    digitalWrite(relaySolar, RELAY_OFF); //OFF Solar supply
    delay(500); //wait half a second
    digitalWrite(relayGrid, RELAY_OFF); //OFF Grid supply
    digitalWrite(statusLEDPin, HIGH); //Turn on status LED
    delay(100); //wait a tenth of a second
    digitalWrite(statusLEDPin, LOW); //Turn off status LED
    delay(100); //wait a tenth of a second
  }
}

Let's start with the basics. WTF is cyfox3ph and what message did you get?

I have a project to turn on the transfer switch by using two relays

What project?
what transfer switch?
what are the relays for?

You are not new to the Forum so you should know that we can't read minds.

...R

my problem sir, i dont know how to put or code to add a time limit to stop the relay ON to save power consumption in relay. what i want if the relay will ON it will stop after 30 seconds enough to change. the transfer switch

Which relay? Totally confusing, Jim, can you get a wiring diagram and more detail out of cyfox3ph?

Robin2:
Let's start with the basics. WTF is cyfox3ph and what message did you get?

Turns out cyfox3ph is forum member: https://forum.arduino.cc/index.php?action=profile;u=1173503

gfvalvo:
Turns out cyfox3ph is forum member:

Can you clone your crystal ball and send the copy to me?

...R

Yeah he's (She's?) some delightfully confused new member that sent this PM to me. So I cleaned it up some {Formatted & code tags} and reposted it for them. Then I sent them a link to this thread. We'll see if they every come back to find it.

-jim lee

jimLee:
Yeah he's (She's?) some delightfully confused new member that sent this PM to me. So I cleaned it up some {Formatted & code tags} and reposted it for them.

That was kind of you. I just send them a standard message telling them to do it themselves.

Next time, please make it clearer that you are acting on behalf of xxx :slight_smile:

...R

Robin2:
Let's start with the basics. WTF is cyfox3ph and what message did you get?
What project?
what transfer switch?
what are the relays for?

You are not new to the Forum so you should know that we can't read minds.

...R

thanks Robin2

I copy this code from other and i study and make some modification.

My project is to control the relays in Auto Transfer Switch for solar and grid supply.
Auto Transfer Switch has 2 relays (220V Supply) to turn on/off small motor bi-directional that will control the switching of two Circuit breaker 220V.
Circuit Breaker 1 = Solar Supply (Inverter 220V)
Circuit Breaker 2 = Grid Supply (Commercial power supply)

sample link for auto transfer switch: https://www.aliexpress.com/item/2P-63A-230V-MCB-type-Dual-Power-Automatic-transfer-switch-ATS/32308090228.html

My project,

  1. when battery voltage reading is above battery low voltage the relaySolar is ON and relayGrid is OFF I'm using the Solar system with Inverter.

  2. when battery voltage reading is below battery low voltage the relaySolar is OFF and relayGrid is ON I'm using the Grid Supply (Commercial power supply).

  3. when battery voltage reading is above battery low voltage and below the battery full voltage relaySolar is OFF and relayGrid is ON I'm still using the Grid Supply so that battery continue charging until full voltage.

  4. when battery voltage reading is above battery Full voltage the relaySolar is ON and relayGrid is OFF I'm using again the Solar system with Inverter.

Now I need help how to put time or timer on two relays (relaySolar and relayGrid) after the function relaySolar ON or relayGrin ON. I need to put time 30 seconds after relaySolar/ Grid is ON because this function ON is use only a little seconds to control the Auto Transfer Switch relays N/O to run the small motor for switching the Auto Transfer Switch then after the switching is done the function relaySolar ON or relayGrin ON will become function relaySolar OFF or relayGrin OFF.

I hope you get my project.

Thank you!

cyfox3ph:
My project is to control the relays in Auto Transfer Switch for solar and grid supply.

I would be very surprised if your Electricity Utility will accept a DIY switching system for feeding power into the grid.

There is a great risk of electrocuting Utility staff.

Don't waste your time on this until you have written approval from your Utility.

...R

Robin2:
I would be very surprised if your Electricity Utility will accept a DIY switching system for feeding power into the grid.

There is a great risk of electrocuting Utility staff.

Don't waste your time on this until you have written approval from your Utility.

...R

Not to mention controlling AC Mains switching using code written by an obvious novice and running on a hobbyist-quality microcontroller board.

What could go wrong?

Robin2:
I would be very surprised if your Electricity Utility will accept a DIY switching system for feeding power into the grid.

There is a great risk of electrocuting Utility staff.

Don't waste your time on this until you have written approval from your Utility.

...R

Sir Robin2 thank you for your reply..

My solar system will not supply power to GRID...
My project is use to supply power to my Server with UPS system in my store.
This project is use to determine which source of power to be use in my Server station according to the voltage reading of my battery connected to solar.

cyfox3ph:
My solar system will not supply power to GRID...

Based on this (from your Original Post) I'm sure you will understand why I was worried that you were "My project is to control the relays in Auto Transfer Switch for solar and grid supply."

My project is use to supply power to my Server with UPS system in my store.

Can you confirm that your UPS (and everything connected to it) will be completely disconnected from the Utility Grid BEFORE you attempt to connect to the solar system and vice versa when switching back to Grid?

...R

i will make schematic diagram