use serial and button to perform operation

hi guys, i have this section of code. i am using a button to call 2 routines. when button is on arm is called, when button is off disarm is called.

now i want to use the serial monitor aswell as the button to call arm and disarm

void loop() 
{
////this code reads the command in the serial  
  while (Serial.available()) 
 {
    delay(5);  
    char c = Serial.read();
    readString += c; 
 }
  readString.trim();

//this code determines the state of button 1
   Value1 = digitalRead(Button1);  
   if(Value1 == HIGH && On1 == LOW)
   {
     ButtonState1 = 1 - ButtonState1;    
     delay(100);
   }      
   On1 = Value1;
   
///////this first section is arduino mfs button control///////

   //if button 1 is on,  alarm is armed    
   if(ButtonState1 == LOW) 
   {
       Arm();  //arm subroutine called
   }

    //if button 1 is off, alarm is disarmed
   else
    {
       DisArm();  //disarm subroutine called
    }
 
//////this next section is for arduino IDE serial control/////

  if (readString.length() >0) 
  {
    if (readString == "arm")
    {
      Arm();
    }
  
    if (readString == "disarm")
    {
      DisArm();
    }
    readString="";
  } 
}

i think that because the button is off the disarm is called and when i try send the arm command through the serial, the arduino detects that the button is off therefore not calling the arm routine. any way to get around this problem and use button and serial monitor to do my function.

someone help????

It kinda looks like you are toggling a variable with each button press, so your description is wrong slightly. Or I haven’t woken up fully yet. But you call it a button not a switch, what button are you using?

When you use the serial input to arm or disarm You have to pretend like the button was pressed, too.

Set the ButtonState1 variable accordionly.

Think of a new name for ButtonState1 that reflects its new role tracking which command was used, no matter the push of the button or serial input, to track the state.

a7

Hi mate, cheers for your reply and i totally agree with your advice. I have managed to use the button to arm and disarm. I realised when button is off the state is 0. so if i send a serial command arm, the button state will have to turn to 1 aswell.

if(readString == “arm”)
{
ButtonState1 = 1;
Arm();
}

kzaki786:
hi guys, i have this section of code. i am using a button to call 2 routines. when button is on arm is called, when button is off disarm is called.

Is it possible that you change your above statement as follows?

When the button is closed for the 1st time, it will call the Arm() routine and the DisArm() routine will be called upon when the button is closed next time.

I can't figure how one can implement your said logic as the button will always come to OFF state after being pressed down and released up by the user (considering a spring return PUSH type button).