serial relay to toggle push button

hi, i need some help setting up this.. 1st its only used for serial relay switch.. now i'm gonna setup in a way so that both serial and button works.

the code below does not really work like i wanted. the relay dosent stay in as commanded by serial. and the button only works if pushed down..

i'm gonna be using it as a light switch. so that i can turn off and on using serial and physical button

Your help is appriciated. Thank you.

/*
simple LED test
*/
char val;         // variable to receive data from the serial port
char val2;
const byte Relay1 = 2;  // Relay Connected to pin 2
const byte Relay2 = 3;  // Relay Connected to pin 3
const byte Relay3 = 4;  // Relay Connected to pin 4
const byte Relay4 = 5;  // Relay Connected to pin 5
const byte Relay5 = 6;  // Relay Connected to pin 6
const byte Relay6 = 7;  // Relay Connected to pin 7
int pushButton = 9; // Push button connected to pin 9

void setup()
{
  pinMode(Relay1, OUTPUT); // pin 2 (Relay) as OUTPUT
  pinMode(Relay2, OUTPUT); // pin 3 (Relay) as OUTPUT
  pinMode(Relay3, OUTPUT); // pin 4 (Relay) as OUTPUT
  pinMode(Relay4, OUTPUT); // pin 5 (Relay) as OUTPUT
  pinMode(Relay5, OUTPUT); // pin 6 (Relay) as OUTPUT
  pinMode(Relay6, OUTPUT); // pin 7 (Relay) as OUTPUT
  pinMode(pushButton, INPUT); // pin 2 (button) as INPUT
  digitalWrite(Relay1, HIGH); // relay start at HIGH
  digitalWrite(Relay2, HIGH); // relay start at HIGH
  digitalWrite(Relay3, HIGH); // relay start at HIGH
  digitalWrite(Relay4, HIGH); // relay start at HIGH
  digitalWrite(Relay5, HIGH); // relay start at HIGH
  digitalWrite(Relay6, HIGH); // relay start at HIGH
  Serial.begin(9600);       // start serial communication at 115200bps
}

void loop()
{

   val2 = digitalRead(pushButton);   // read the pushbutton pin store in val2
   
   if (val2 == LOW)   // turn Relay off: 
    {
      //Serial.write ("pushOn");            //send serial of pushbutton
      digitalWrite(Relay1, HIGH);    // turn Off relay1
      delay (100);
    }

   else if (val2 == HIGH) //turn relay 1 on
    {
      //Serial.write ("pushOff");            //send serial of pushbutton
      digitalWrite(Relay1, LOW);    // turn On relay 1
      delay (100);
    }

  
  if ( Serial.available() > 0 )      // if data is available to read
  {
    val = Serial.read();         // read it and store it in 'val'
       
    if ( val == 'a' )              // if 'a' was received led 2 is switched off
    {
      Serial.write ("a");            //send serial 'a'
      digitalWrite(Relay1, HIGH);    // turn Off relay 1
    }

    if ( val == 'A' )              // if 'A' was received relay 1 on
    {
      Serial.write ("A");            //send serial 'A'
      digitalWrite(Relay1, LOW);  // turn ON relay 1
    }

    if ( val == 'b' )              // if 'b' was received relay 3 is switched off
    {
      Serial.write ("b");            //send serial 'b'
      digitalWrite(Relay2, HIGH);    // turn Off relay 2
    }

    if ( val == 'B' )              // if 'B' was received relay 2 on
    {
      Serial.write ("B");            //send serial 'B'
      digitalWrite(Relay2, LOW);  // turn ON relay 2
    } //else (ledpin = 3, LOW)        //set relay 2 to low state
    
      if ( val == 'c' )              // if 'c' was received relay 3 is switched off
    {
      Serial.write ("c");            //send serial 'c'
      digitalWrite(Relay3, HIGH);    // turn Off relay 3
    }

    if ( val == 'C' )              // if 'C' was received relay 3 on
    {
      Serial.write ("C");         //send serial 'C'
      digitalWrite(Relay3, LOW);  // turn ON relay 3
    }

    if ( val == 'd' )              
    {
      Serial.write ("d");
      digitalWrite(Relay4, HIGH);  
    }

    if ( val == 'D' )             
    {
      Serial.write ("D");
      digitalWrite(Relay4, LOW);  
    } //else (ledpin = 3, LOW)       
    //trying stuff

    if ( val == 'f' )              
    {
      Serial.write ("f");
      digitalWrite(Relay5, HIGH);    
    }

    if ( val == 'F' )              
    {
      Serial.write ("F");
      digitalWrite(Relay5, LOW);  
    }
     if ( val == 'g' )              
    {
      Serial.write ("g");
      digitalWrite(Relay6, HIGH);   
    }

    if ( val == 'G' )              
    {
      Serial.write ("G");
      digitalWrite(Relay6, LOW);  
    }
    if( val == 'e' )
    {
      Serial.write ("e");
      digitalWrite(Relay3, LOW);
      delay(34);
      digitalWrite(Relay3, HIGH);
      delay(400);
      digitalWrite(Relay3, LOW);
      delay(34);
      digitalWrite(Relay3, HIGH);
      delay(199);
      digitalWrite(Relay3, LOW);
      delay(34);
      digitalWrite(Relay3, HIGH);
      delay(202);
      digitalWrite(Relay3, LOW);
      delay(34);
      digitalWrite(Relay3, HIGH);
      delay(433);
      digitalWrite(Relay3, LOW);
      delay(34);
      digitalWrite(Relay3, HIGH);
      delay(367);
      digitalWrite(Relay3, LOW);
      delay(34);
      digitalWrite(Relay3, HIGH);
      delay(434);
      digitalWrite(Relay3, LOW);
      delay(34);
      digitalWrite(Relay3, HIGH);
    }
  }
}

So the problem in your code is that either the button OR the serial can turn it on and off. When the serial tries to turn it on, the button code turns it right back off. You need to track both things together somewhere. And you need to decide which one rules. If I have the button down and send it off via serial what should it do? There are several possible scenarios and you have to account for each. Once you do that in words it will be a lot easier to do in code.

:) is it possible to use the button to send serial? i'm quite lost, how can i setup so it toggles? even if i could setup the button to send serial. i still dont know how to set toggle. :( :(

Buttons can't send serial. They don't have a processor inside. You could code the Arduino to send serial when a button is pressed. That would be putting the code that sends the Serial in an if statement that checks for the button to be pressed.

I asked some important questions in the end of my last reply that you really can't move forward without answering. Why do you ignore that? Don't you want to get this fixed? I won't waste my time on you if you refuse to read replies and answer them.

the problem in my code is when i turn on the relay from serial.. the swith position turns it back off. to track both things i have setup val for serial and val2 for button. if u have the button down and it sends serial, the button would send A or a to toggle the relay.

Thank You.

WTF are you even trying to do?

Where does the Serial input come from?

The button is connected to the same Arduino as that receives the Serial commands and switches the relays, right? Then why would you want to have those buttons to send Serial? Who would they send it to?

Your problem - as pointed out above - is that it seems you have your priorities wrong (or simply never thought about it).

At all times you have TWO inputs for ONE action, that makes things complicated.

Which of the inputs (Serial command or the push button) overrides which?

Start with the simple scenarios. Relay is off. You press the push button to switch on the relay. Should it stay on until the next press or until the button is released again? (is the button debounced?)

Relay is off. Serial command comes in to switch it on. Should it stay on forever?

Relay is off. Button switches on relay, then Serial command comes in to switch it off. What should happen?

Relay is off. Serial command comes in to switch it on. Button is (of course) in the unpressed state. What should happen? Then you press the button as off-command. What should happen?

Without answering these questions explicitly first there is no way you can get your code to work the way you want it to.