I need to add a piece of code to activate alarm trigger only when condition met

Hi Arduino Team

I have a working Relay control project using arduino Nano,HC-05 Bluetooth Module & 4ch relay Module. I installed it in my Ebike to turn ON and OFF my ebike. I use my android phone app to turn relays turn on and off, (all working)
Now I am extending the project to have a security alarm trigger on vibration. what i need is pin 6 need to be act as a trigger for alarm when all relays are OFF
My trigger is a simple sensitive spring, on vibration that will swing and touch the ground pin and pin6(pull up)
The trigger should only work when all 4 relays are in “OFF” and trigger action is turn on all relays except Ignition relay.(one of the relay have sound alarm that will alert me and since ignition relay will be OFF vehicle is immobilised.
My Project code is as follows:

//Nitz Ebike remote system with security alarm system using Adrino Nano,HC-05 Bluetooth Module,4 Channel 5v relay Module,Battery Pack and 5v boost convertor
//charger(12v to 5v)buck convertor or 7805 Linear Regulator
#include <SoftwareSerial.h>

SoftwareSerial btSerial(10, 11); //Pin10 RX, Pin 11 TX connected to -->Bluetooth TX,RX
char BluetoothData;         // store the Bluetooth data received
int Interval = 1000;        // general delay
int BuzzerInterval = 80;    // buzzer clicking delay (when system on 2 times, 1time when system off)
int PowerRelay1 = 2;        // declaring relay to digital pin2
int IgnRelay = 3;           // declaring Ignition relay at pin3
int BuzzerRelay = 4;        // declaring buzzer relay at pin4
int PowerRelay2 = 5;        // declaring powerRelay2 at pin5
int TriggerPin = 6; // triggerPin for activating security alarm (INPUT_PULLUP)
int LedPin = 7;     // declared led pin
void setup() {
  Serial.begin(115200);     // starting serial monitor at baud 115200
  btSerial.begin(115200);   //  starting bluetooth serial at baud 115200

  //Set Digital Pins 2 to 5 as Output
  pinMode(PowerRelay1, OUTPUT);   // declaring "PowerRelay1" as OUTPUT
  pinMode(IgnRelay, OUTPUT);      // declaring "IgnRelay" as OUTPUT
  pinMode(BuzzerRelay, OUTPUT);   // declaring "BuzzerRelay" as OUTPUT
  pinMode(PowerRelay2, OUTPUT);   // declaring "PowerRelay2" as OUTPUT
  pinMode(LedPin, OUTPUT);        // declaring "ledPin" as OUTPUT
  pinMode(TriggerPin, INPUT_PULLUP);  // declaring TriggerPin as INPUT (in case of vibration GND AND pin6 will short)
  //used sensitive spring and plate nearby,  on vibration spring will swing and touch the  plate(GND)

  //Set State of Pins all to  (HIGH)** in relay module HIGH means off state, visa versa.
  digitalWrite(PowerRelay1, HIGH);  //
  digitalWrite(IgnRelay, HIGH);
  digitalWrite(BuzzerRelay, HIGH);
  digitalWrite(PowerRelay2, HIGH);
  // Set state of trigger pin as low as it is HIGH When not grounded.
 digitalWrite(TriggerPin, LOW);  //

}

void loop() {

  if (btSerial.available()) {
    BluetoothData = btSerial.read(); //Get next character from bluetooth
    Serial.println(BluetoothData);
    if (BluetoothData == '1') {
      // below listed what ever action need to be executed upon receiving "1" on btSerial .(in My case any Android Phone bluetooth terminal app, device must be paired.)
      digitalWrite(PowerRelay1, LOW); // BOTH PowerRelays goes "ON"
      digitalWrite(PowerRelay2, LOW); // BOTH PowerRelays goes "ON"
      delay(Interval);                // General delay ( for internal capacitors to charge)E Bike
      digitalWrite(IgnRelay, LOW);    // Ignition "ON"
      digitalWrite(BuzzerRelay, LOW); // Buzzer "ON"
      delay(BuzzerInterval);
      digitalWrite(BuzzerRelay, HIGH);  //  Buzzer should sound twice (as we unlock a car)
      delay(BuzzerInterval);
      digitalWrite(BuzzerRelay, LOW);   //Buzzer "ON"
      delay(BuzzerInterval);
      digitalWrite(BuzzerRelay, HIGH);  //Buzzer "OFF"
    }
    if (BluetoothData == 'I') { // Turn digital out 2,3,4 & 5 HIGH( ALL RELAYS OFF)
      digitalWrite(BuzzerRelay, LOW);    // Buzzer "ON"
      delay(BuzzerInterval);
      digitalWrite(BuzzerRelay, HIGH);   // Buzzer "OFF"   buzzer sounds once ( as we lock car )
      digitalWrite(PowerRelay1, HIGH);   //  RELAY "OFF"
      digitalWrite(PowerRelay2, HIGH);   //  RELAY "OFF"
      digitalWrite(IgnRelay, HIGH);      //  RELAY "OFF"
      delay(Interval);                   // General Delay

    }
    if (BluetoothData == '3') { //Turn digital out 4 to LOW (BuzzerRelay ON)   //  for testing connection (Only when power relays on)
      digitalWrite(BuzzerRelay, LOW);
    }
    if (BluetoothData == 'C') { // Turn digital out 4 to High (BuzzerRelay OFF) ) for testing connection (Only when power relays on)
      digitalWrite(BuzzerRelay, HIGH);
    }
    if (BluetoothData == '9') { //Turn digital out 2,3,4,5 LOW (ALL RELAYS ON) (Trigger Case) (
      digitalWrite(PowerRelay1, LOW);
      digitalWrite(PowerRelay2, LOW);
      delay(Interval);
      digitalWrite(IgnRelay, HIGH);
      digitalWrite(BuzzerRelay, LOW);  //siren pin

    }
   int Trigger = digitalRead(6);
    
     if ( (PowerRelay1 == HIGH) && 
        (PowerRelay2 == HIGH) && 
        (IgnRelay == HIGH) && 
        (Trigger == HIGH ))              //( in this part i need help 
  
   { digitalWrite(PowerRelay1, LOW);
      digitalWrite(PowerRelay2, LOW);    this is the action i needed upon trigger
      delay(Interval);
      digitalWrite(IgnRelay, HIGH);
      digitalWrite(BuzzerRelay, LOW);  //siren pin  
  }
}

}

please help to resolve this

Thanking you

Nitz

pinMode(TriggerPin, INPUT_PULLUP);

Enable the internal pullup.

digitalWrite(TriggerPin, LOW);  //

Disable the internal pullup.

(Trigger == HIGH ))              //( in this part i need help

The triggerPin will be pulled LOW when activated (closed contact) if pinMode is INPUT_PULLUP and trigger switch wired from input to ground.

Hi thanks for reply , Sorry to say that it was not helpful , i just got my written code back :confused:
I can explain what is the issue once again , i think a logical code is need to be added to my existing code to
turn buzzer relay ON when vehicle detects vibration pull_up goes LOW only when all relays are HIGH,
(The trouble now is I turn on the vehicle using my android phone app,(bluetooth) and when i sit on the vehicle it activates buzzer alarm relay. since it detects pin 6 pull_up will be grounded on vibration . I don’t want that . else when i manually turn on the vehicle through bluetooth communication triggerpin input should be disabled or ignored. and when i turn off the vehicle through bluetooth communication trigger pin input should be considered or enabled to do actions upon vibration. (As said since it is INPUT_PULLUP it is HIGH when not grounded and if vibration detects it will go LOW.) please see my unfinished code and suggest an edit .

//Nithin's Ebike remote system with security alarm system using Adrino Nano,HC-05 Bluetooth Module,4 Channel 5v relay Module,Battery Pack and 5v boost convertor
//charger(12v to 5v)buck convertor or 7805 Linear Regulator
#include <SoftwareSerial.h>

SoftwareSerial btSerial(10, 11); //Pin10 RX, Pin 11 TX connected to -->Bluetooth TX,RX
char BluetoothData;         // store the Bluetooth data received
int Interval = 1000;        // general delay
int BuzzerInterval = 80;    // buzzer clicking delay (when system on 2 times, 1time when system off)
int PowerRelay1 = 2;        // declaring relay to digital pin2
int IgnRelay = 3;           // declaring Ignition relay at pin3
int BuzzerRelay = 4;        // declaring buzzer relay at pin4
int PowerRelay2 = 5;        // declaring powerRelay2 at pin5
int TriggerPin = 6; // triggerPin for activating security alarm (INPUT_PULLUP)
int LedPin = 7;     // declared led pin
void setup() {
  Serial.begin(115200);     // starting serial monitor at baud 115200
  btSerial.begin(115200);   //  starting bluetooth serial at baud 115200

  //Set Digital Pins 2 to 5 as Output
  pinMode(PowerRelay1, OUTPUT);   // declaring "PowerRelay1" as OUTPUT
  pinMode(IgnRelay, OUTPUT);      // declaring "IgnRelay" as OUTPUT
  pinMode(BuzzerRelay, OUTPUT);   // declaring "BuzzerRelay" as OUTPUT
  pinMode(PowerRelay2, OUTPUT);   // declaring "PowerRelay2" as OUTPUT
  pinMode(LedPin, OUTPUT);        // declaring "ledPin" as OUTPUT
  pinMode(TriggerPin, INPUT_PULLUP);  // declaring TriggerPin as INPUT (in case of vibration GND AND pin6 will short)
  //used sensitive spring and plate nearby,  on vibration spring will swing and touch the  plate(GND)

  //Set State of Pins all to  (HIGH)** in relay module HIGH means off state, visa versa.
  digitalWrite(PowerRelay1, HIGH);  //
  digitalWrite(IgnRelay, HIGH);
  digitalWrite(BuzzerRelay, HIGH);
  digitalWrite(PowerRelay2, HIGH);
  // Set state of trigger pin as low as it is HIGH When not grounded.
 digitalWrite(TriggerPin, LOW);  //

}

void loop() {

  if (btSerial.available()) {
    BluetoothData = btSerial.read(); //Get next character from bluetooth
    Serial.println(BluetoothData);
    if (BluetoothData == '1') {
      // below listed what ever action need to be executed upon receiving "1" on btSerial .(in My case any Android Phone bluetooth terminal app, device must be paired.)
      digitalWrite(PowerRelay1, LOW); // BOTH PowerRelays goes "ON"
      digitalWrite(PowerRelay2, LOW); // BOTH PowerRelays goes "ON"
      delay(Interval);                // General delay ( for internal capacitors to charge)E Bike
      digitalWrite(IgnRelay, LOW);    // Ignition "ON"
      digitalWrite(BuzzerRelay, LOW); // Buzzer "ON"
      delay(BuzzerInterval);
      digitalWrite(BuzzerRelay, HIGH);  //  Buzzer should sound twice (as we unlock a car)
      delay(BuzzerInterval);
      digitalWrite(BuzzerRelay, LOW);   //Buzzer "ON"
      delay(BuzzerInterval);
      digitalWrite(BuzzerRelay, HIGH);  //Buzzer "OFF"
    }
    if (BluetoothData == 'I') { // Turn digital out 2,3,4 & 5 HIGH( ALL RELAYS OFF)
      digitalWrite(BuzzerRelay, LOW);    // Buzzer "ON"
      delay(BuzzerInterval);
      digitalWrite(BuzzerRelay, HIGH);   // Buzzer "OFF"   buzzer sounds once ( as we lock car )
      digitalWrite(PowerRelay1, HIGH);   //  RELAY "OFF"
      digitalWrite(PowerRelay2, HIGH);   //  RELAY "OFF"
      digitalWrite(IgnRelay, HIGH);      //  RELAY "OFF"
      delay(Interval);                   // General Delay

    }
    if (BluetoothData == '3') { //Turn digital out 4 to LOW (BuzzerRelay ON)   //  for testing connection (Only when power relays on)
      digitalWrite(BuzzerRelay, LOW);
    }
    if (BluetoothData == 'C') { // Turn digital out 4 to High (BuzzerRelay OFF) ) for testing connection (Only when power relays on)
      digitalWrite(BuzzerRelay, HIGH);
    }
    if (BluetoothData == '9') { //Turn digital out 2,3,4,5 LOW (ALL RELAYS ON) (Trigger Case) (
      digitalWrite(PowerRelay1, LOW);
      digitalWrite(PowerRelay2, LOW);
      delay(Interval);
      digitalWrite(IgnRelay, HIGH);
      digitalWrite(BuzzerRelay, LOW);  //siren pin

    }
   int Trigger = digitalRead(6);// do not consider the below lines these are my experiments 
    
     if ( (PowerRelay1 == HIGH) && 
        (PowerRelay2 == HIGH) && 
        (IgnRelay == HIGH) && 
        (Trigger == HIGH ))              //
  
   { digitalWrite(PowerRelay1, LOW);
      digitalWrite(PowerRelay2, LOW);
      delay(Interval);
      digitalWrite(IgnRelay, HIGH);
      digitalWrite(BuzzerRelay, LOW);  //siren pin
  }
}

}

Any help will be highly appreciated
Thank You

Nitz

If TriggerPin is an input with an internal pullup to keep it HIGH why are you writing a LOW to it?

Steve

Use a global boolean to indicate whether the vehicle has been turned on or off: VehicleIsOn perhaps. Set it true in the '1' case and false in the 'I' case.

Use it to check whether to trigger an alarm on vibration.

When you write LOW to an input configured with INPUT_PULLUP, the pin does not go a LOW state. Writing LOW to the input only disables the internal pullup causing the pin to float in an indeterminate state.

While you're there, change all those repetitive if () statements to a single switch () block... There'll probably be some simplification of your output control then - as well as being shorter and more readable. cheers

slipstick: If TriggerPin is an input with an internal pullup to keep it HIGH why are you writing a LOW to it?

Steve

Thanks Steve for that information

I am now working on

wildbill: Use a global boolean to indicate whether the vehicle has been turned on or off: VehicleIsOn perhaps. Set it true in the '1' case and false in the 'I' case.

Use it to check whether to trigger an alarm on vibration.

I know this is the concept applicable don't how to include global boolean, I am trying it by looking similar Posts and playground example, its been a week finding solutions Any help assigning global boolean is appreciated

Thank you all

Nithin

To ‘create’ a global variable, simply declare it outside (usually at the top - before) your setup() function.
Then the variable contents will be visible to all your code in this module. (i.e. globally)

bool variables normally have the pre-defined values of True or False.

Hi

lastchancename: While you're there, change all those repetitive if () statements to a single switch () block... There'll probably be some simplification of your output control then - as well as being shorter and more readable. cheers

I simplified my project code , (switch case) it was helpful, and interesting, and more readable.

Thank You.

Nithin

Happy to help!

I have simplified my code . problem remains , can anybody add global boolean to set my input sensor pin does function only if all 4 relays are in OFF state. in the sense it should not trigger buzzer when any of the relays are in ON state.

case ‘a’ : trigger should be ignored or deactivated (vehicle on)

case ‘b’ : trigger must carry on its function; (buzzer) (vehicle off)

Such a headache on figuring a simple logic solution, please help if possible. I am attaching the new code ,all working . able to turn on ,off , relays; but trigger is ON all the time. if i want to unlock vehicle disconnect trigger wire and go :’(

#include <SoftwareSerial.h>
SoftwareSerial btSerial(10, 11); //Pin10 RX, Pin 11 TX connected to -->Bluetooth TX,RX
char bluetoothData;
//boolean trigger = false;   // declaring global variable (boolean)don't know function.
int buzzerTime = 70;
int relay1 = 2;   // main power
int relay2 = 3;   // key switch
int relay3 = 4;   // secondary power
int relay4 = 5;   // buzzer
int triggerPin = 7;   // vibration sensor ( pin7 + GND)
int ledPin = 13;
int interval = 200;
void setup() {
  // initialize serial communication:
  Serial.begin(115200);
  btSerial.begin(115200);
  pinMode(relay1, OUTPUT);    // main power
  pinMode(relay2, OUTPUT);    // key
  pinMode(relay3, OUTPUT);    // secondary power
  pinMode(relay4, OUTPUT);    // buzzer
  pinMode(triggerPin, INPUT_PULLUP); // vibration sensor( pin7 + GND)
  pinMode(ledPin, OUTPUT);
  //Set State of Pins all to  (HIGH)** in relay module HIGH means off state, visa versa.
  digitalWrite(relay1, HIGH);
  digitalWrite(relay2, HIGH);
  digitalWrite(relay3, HIGH);
  digitalWrite(relay4, HIGH);
}

void loop() {
  int sensorVal = digitalRead(triggerPin);
  //Serial.println(sensorVal);
  //delay(interval);
  if ( (sensorVal == LOW)) {     //upon Vibration (trigger)
    digitalWrite(ledPin, HIGH);
    digitalWrite(relay1, LOW);  // main
    digitalWrite(relay2, HIGH);  //key
    digitalWrite(relay3, LOW);  // secondary power
    digitalWrite(relay4, LOW);  //buzzer
  }

  // read the sensor:
  if (btSerial.available()) {
    int btData = btSerial.read(); //Get next character from bluetooth
    //Serial.println(btData);

    switch (btData) {
      case 'a':                     //vehicle on
        digitalWrite(relay1, LOW);
        delay(interval);
        digitalWrite(relay2, LOW);
        delay(interval);
        digitalWrite(relay3, LOW);
        delay(buzzerTime);
        digitalWrite(relay4, LOW);  // 1 beep
        delay(buzzerTime);
        digitalWrite(relay4, HIGH);
        delay(buzzerTime);
        digitalWrite(relay4, LOW);  // 2 beep
        delay(buzzerTime);
        digitalWrite(relay4, HIGH);
        break;
      case 'b':                        // vehicle off
        digitalWrite(relay4, LOW);  //1 beep
        delay(buzzerTime);
        digitalWrite(relay4, HIGH);
        delay(buzzerTime);
        digitalWrite(relay1, HIGH);
        delay(interval);
        digitalWrite(relay2, HIGH);
        delay(interval);
        digitalWrite(relay3, HIGH);
        digitalWrite(ledPin, LOW);

        break;
      case 'c':                       // buzzer relay for connection testing
        digitalWrite(relay4, LOW);
        break;
      case 'd':                       // buzzer relay for connection testing
        digitalWrite(relay4, HIGH);
        digitalWrite(ledPin, LOW);
        break;
      case 'e' :                        // All  instant off (Emergency case)
        digitalWrite(relay1, HIGH);
        digitalWrite(relay2, HIGH);
        digitalWrite(relay3, HIGH);
        digitalWrite(relay4, HIGH);
        digitalWrite(ledPin, LOW);
      default:
        digitalWrite(ledPin, LOW);
    }

  }
}

I need the help with the code . any experts who can solve this for me , please help ;

Thank You

Nitz

You reinstantiate int sensorPin each time the code passes around loop()

Two options... - put it ‘outside’ - above setup() to become a global, then you can reference sesnsorPin anywhere in your code. - change it to static int sensorPin if it’s staying within loop(), then the most recent value becomes persistent, but is only available within loop()

In either case you may also like to change it from an int to a bool - which conveys its purpose more clearly. This may not solve your problem, but it makes your sensorPin variable persistent in the desired scope.

    int btData = btSerial.read(); //Get next character from bluetooth

And store it in an int. Why? I store integers in ints and characters in chars.

but trigger is ON all the time. if i want to unlock vehicle disconnect trigger wire and go

If the VALUE you read from the trigger pin is always HIGH (or LOW), you have wiring problem. You need to make CERTAIN that the pins is always HIGH (or LOW) when the switch is not pressed. The internal pullup resistor will take care of that IF the switch is wired correctly.

can anybody add global boolean

It is trivial to do that. Before setup() and loop(), add

boolean someName =  false;

to set my input sensor pin does function only if all 4 relays are in OFF state.

I don’t understand why you think you need a boolean variable for this.

byte s1 = digitalRead(relay1);
byte s2 = digitalRead(relay2);
byte s3 = digitalRead(relay3);
byte s4 = digitalRead(relay4);
if(s1 == LOW && s2 == LOW && s3 == LOW && s4 == LOW)
{
   // the relay pins are all LOW
}

Change LOW to HIGH if HIGH means off.

Hi , Hurray it worked. (I repeated to add boolean because one of the members suggested it as below)

wildbill:
Use a global boolean to indicate whether the vehicle has been turned on or off: VehicleIsOn perhaps. Set it true in the ‘1’ case and false in the ‘I’ case.

Use it to check whether to trigger an alarm on vibration.

but the below code helped me( not boolean)and now the sensor and the relays are working as planned. and my project is done.

PaulS:

    int btData = btSerial.read(); //Get next character from bluetooth

And store it in an int. Why? I store integers in ints and characters in chars.
If the VALUE you read from the trigger pin is always HIGH (or LOW), you have wiring problem. You need to make CERTAIN that the pins is always HIGH (or LOW) when the switch is not pressed. The internal pullup resistor will take care of that IF the switch is wired correctly.
It is trivial to do that. Before setup() and loop(), add

boolean someName =  false;

I don’t understand why you think you need a boolean variable for this.

byte s1 = digitalRead(relay1);

byte s2 = digitalRead(relay2);
byte s3 = digitalRead(relay3);
byte s4 = digitalRead(relay4);
if(s1 == LOW && s2 == LOW && s3 == LOW && s4 == LOW)
{
  // the relay pins are all LOW
}



Change LOW to HIGH if HIGH means off.

Thank You members taking your valuable time and knowledge to help me out with my problem in my project code,

Thank You once again

Nithin

// code for reference
#include <SoftwareSerial.h>
SoftwareSerial btSerial(10, 11); //Pin10 RX, Pin 11 TX connected to -->Bluetooth TX,RX
char btData;
//boolean trigger = false;   // declaring global variable (boolean)don't know function.
int buzzerTime = 80;
int relay1 = 2;   // main power
int relay2 = 3;   // key switch
int relay3 = 4;   // lights
int relay4 = 5;   // buzzer
int triggerPin = 7;   // vibration sensor ( pin7 + GND)
int ledPin = 13;
int interval = 300;
void setup() {
 // initialize serial communication:
 Serial.begin(115200);
 btSerial.begin(115200);
 pinMode(relay1, OUTPUT);    // main power
 pinMode(relay2, OUTPUT);    // key
 pinMode(relay3, OUTPUT);    // lights
 pinMode(relay4, OUTPUT);    // buzzer
 pinMode(triggerPin, INPUT_PULLUP); // vibration sensor( pin7 + GND)
 pinMode(ledPin, OUTPUT);    // trigger led indicator
 //Set State of Pins all to  (HIGH)** in relay module HIGH means off state, visa versa.
 digitalWrite(relay1, HIGH);
 digitalWrite(relay2, HIGH);
 digitalWrite(relay3, HIGH);
 digitalWrite(relay4, HIGH);
}

void loop() {
 byte s1 = digitalRead(relay1);
 byte s2 = digitalRead(relay2);
 byte s3 = digitalRead(relay3);
 byte s4 = digitalRead(relay4);
 byte s5 = digitalRead(triggerPin);

 if (s1 == HIGH && s2 == HIGH && s3 == HIGH && s4 == HIGH && s5 == LOW) // trigger only when //all relays are HIGH or(OFF)
 {
   // the relay pins are all LOW
   digitalWrite(ledPin, HIGH);  // trigger led indicator
   digitalWrite(relay1, LOW);  // main
   digitalWrite(relay2, HIGH);  //key
   digitalWrite(relay3, LOW);  //lights
   digitalWrite(relay4, LOW);  //buzzer
 }

 // read the sensor:
 if (btSerial.available()) {
   char btData = btSerial.read(); //Get next character from bluetooth
   Serial.println(btData);

   switch (btData) {
     case 'a':                               //vehicle on
       digitalWrite(relay1, LOW);            // main power
       delay(interval);
       digitalWrite(relay2, LOW);            // key
       delay(interval);
       //digitalWrite(relay3, LOW);
       //delay(interval);
       digitalWrite(relay4, LOW);
       delay(buzzerTime);
       digitalWrite(relay4, HIGH);
       delay(buzzerTime);
       digitalWrite(relay4, LOW);            // 2 beep // buzzer
       delay(buzzerTime);
       digitalWrite(relay4, HIGH);
       break;
     case 'b':                               // vehicle off
       digitalWrite(relay4, LOW);
       delay(buzzerTime);
       digitalWrite(relay4, HIGH);           // 1 beep // buzzer
       delay(buzzerTime);
       digitalWrite(relay1, HIGH);
       delay(interval);
       digitalWrite(relay2, HIGH);
       delay(interval);
       digitalWrite(relay3, HIGH);
       digitalWrite(ledPin, LOW);
       break;
     case 'c':                       // main power relay on
       digitalWrite(relay1, LOW);
       break;
     case 'd':                       // main power relay off
       digitalWrite(relay1, HIGH);
       break;
     case 'e':                       // key relay on
       digitalWrite(relay2, LOW);
       break;
     case 'f':                       // key relay off
       digitalWrite(relay2, HIGH);
       break;
     case 'g' :                      //  lights relay on
       digitalWrite(relay3, LOW);
       break;
     case 'h' :                      //  lights relay off
       digitalWrite(relay3, HIGH);
       break;
     case 'i' :                        // buzzer relay on
       digitalWrite(relay4, LOW);
       break;
     case 'j' :                        // buzzer relay off
       digitalWrite(relay4, HIGH);
       break;
     case 'k' :                        //  All relays  instant off (Emergency case)
       digitalWrite(relay1, HIGH);
       digitalWrite(relay2, HIGH);
       digitalWrite(relay3, HIGH);
       digitalWrite(relay4, HIGH);
       break;
       //default:

   }

 }
}

Thank you all once again

bye