Help with programming Home Automation Project

Hi,

i need help to program this:

I have two switches on 2 inputs, 3 relays for outputs (2 lamps, one in bath, one in WC and one relay for fan for both places).
Logic would be:
- when i turn switch on I turn on lamp in bath and turn on a fan.
- when turn off switch turns off lamp in that place, and fan lets say turns off with delay after 5 minutes.
- with other button turn on lamp in WC and turn on a fan too, when turn off switch the fan turns off with delay after 5 minutes.

I Started with this code:

const int  LightWCPin = 2; 
const int  LightBathPin = 4; 
const int LightWCOut = 7;
const int LightBathOut = 8;
const int FanOut = 12;

int buttonState = 0;         // current state of the button
int buttonState2 = 0;  

void setup() {
   // initialize the button pin as a input:
   pinMode(LightWCPin, INPUT);
   pinMode(LightBathPin, INPUT);

   // initialize outputs:
   pinMode(LightWCOut, OUTPUT);
   pinMode(LightBathOut, OUTPUT);
  pinMode(FanOut, OUTPUT);
   // initialize serial communication:
   Serial.begin(9600);
}

void loop() {
   // read the pushbutton input pin:
  buttonState = digitalRead(LightWCPin);
  buttonState2 = digitalRead(LightBathPin);

if (buttonState == LOW) {     
     // turn WC on:    
     digitalWrite(LightWCOut, HIGH);  
     digitalWrite(FanOut, HIGH);
   } 
   else {
     // turn off:
     digitalWrite(LightWCOut, LOW);
  
  digitalWrite(FanOut, LOW);

   }
   
if (buttonState2 == LOW) {     
     // turn on Bath:    
     digitalWrite(LightBathOut, HIGH);  
     digitalWrite(FanOut, HIGH);    
   } 
   else {
     // turn off:
     digitalWrite(LightBathOut, LOW); 
    digitalWrite(FanOut, LOW);
  
   }   

}

I Started with this code:

Which does something. You haven't explained what it actually does.

You want it to do something. How that differs from what you actually want isn't at all clear.

Therefore, it's impossible to tell you what to change.

For now when i turn on switch only turns on light relay and fan relay. When switch off turns off light relay and fan relay.

I want to upgrade code that when i turn off light, fan relay will turn off after 5 mins.
I think it could be posible with millis() function.

When the switch goes off you need to record the value of millis() into an unsigned long variable - let's call it startMillis.

Then each iteration of loop() you need to check

if (millis() - startMillis >= interval) {
    // switch stuff off
}

The variable "interval" holds the number of milliseconds that the fan should stay on after the switch is turned off.

...R