Arduino rocket test stand

Hello, I am working on a rocket motor test stand project.It consists of two parts: controller with five switches( arming, tank fill, fuel valve, ox valve, ignition), arduino and the test stand with a load cell, 3 servo operated valves, and ignition wired up through a mosfet. And i got a problem with sending switch states over software serial (hardware serial is needed to connect controller to a pc), it has a really long delay, around 5 seconds, i figured out that its on the sending/recieving part, not encoding/decoding.

I would apreciate if someone helps me figure it out.

controller code:

#include <SoftwareSerial.h>

// Define SoftwareSerial pins
SoftwareSerial mySerial(7, 8); // RX, TX (On Arduino 1, use pin 7 for RX and 8 for TX)

byte thrustBytes[4];
byte command;

bool arm;
bool fill;
bool ox;
bool fuel;
bool ignition;

float thrust;

void setup() {
  
  Serial.begin(19200);
  mySerial.begin(19200);

  delay(500);
  pinMode(2, INPUT_PULLUP); //ignition switch
  pinMode(3, INPUT_PULLUP); //fuel valve switch
  pinMode(4, INPUT_PULLUP); //ox valve switch
  pinMode(5, INPUT_PULLUP); //arming switch
  pinMode(6, INPUT_PULLUP); //tank fill valve switch
 
}

void loop() {

  
  if (digitalRead(2) == 0){      //turn all switch signals into boolian
    ignition = true;
  }
  else if (digitalRead(2) == 1){
    ignition = false;
  }
  if (digitalRead(3) == 0){
    fuel = true;
  }
  else if (digitalRead(3) == 1){
    fuel = false;
  }
  if (digitalRead(4) == 0){
    ox = true;
  }
  else if (digitalRead(4) == 1){
    ox = false;
  }
  if (digitalRead(5) == 0){
    arm = true;
  }
  else if (digitalRead(5) == 1){
    arm = false;
  }
  if (digitalRead(6) == 0){
    fill = true;
  }
  else if (digitalRead(6) == 1){
    fill = false;
  }
 
  command = (arm << 4) | ( fill << 3) | (fuel << 2) | (ox << 1) | (ignition << 0); //combine all bits into a byte
  
  mySerial.write(command);

  delay(20);


 
  
 




}

and stand code:

#include <SoftwareSerial.h>
#include <HX711.h>

// Define SoftwareSerial pins
SoftwareSerial mySerial(7, 8); // RX, TX (On Arduino 2, use pin 7 for RX and 8 for TX)

HX711 scale;
uint8_t dataPin = 6;
uint8_t clockPin = 5;
float thrust;


byte command;

bool arm;
bool fill;
bool ox;
bool fuel;
bool ignition;

void setup() {

  Serial.begin(19200);
  mySerial.begin(19200);
  scale.begin(dataPin, clockPin);

  
  scale.set_scale(66.6);    
  scale.tare();
}

void loop() {
  thrust = scale.get_units(1);
  delay(50);
  
  
  if (mySerial.available()) {  //recieve and decode the command
    command = mySerial.read();
    arm       = (command >> 4) & 1;
    fill      = (command >> 3) & 1;
    fuel      = (command >> 2) & 1;
    ox        = (command >> 1) & 1;
    ignition  = (command >> 0) & 1;
    
   
  }
}

Thanks

See how many times this runs, and consider removing it?

I missed this one... why the delays()?

I deleated all delays from the code

It didn't fix anything, and a new issue ocured, the output is now alternating. I was printing arm to serial monitor and i was getting ( still only in a couple of seconds after switch was flipped)

1
0
1
0
1
0

No doubt.

No, it was hidden by delay().

Do you have any ideas how to fix it?

why not transmit the GPIO values directly

command = (digitalRead(5) << 4) | ( digitalRead(6) << 3) | (digitalRead(3) << 2) | (digitalRead(4) << 1) | (digitalRead(2) << 0); //combine all bits into a byte
mySerial.write(command);
Serial.println(command, HEX);   // display reading
 delay(1000);    // try increasing the delay while testing
  

in the receiver display result

  command = mySerial.read();
Serial.println(command, HEX);

as recommended by @xfpd remove delay() in receiver

You are sending data faster than the receiver can process it. SoftwareSerial has a 64 byte buffer, if the receiver takes 100 ms to process than the backlog will take 100ms * 64 = 6.4 seconds to process.

To reduce latency you must either process received data quicker, or send it less often. I don't think the AVR version of SoftwareSerial has an onReceive() method.

SoftwareSerial is quite limiting, of course a hardware serial would be better. There are other ways you could sync the transmitter and receiver.

Thank you all so much, it works now, I ended up adding a delay and now it works perfectly fine!