Problems with running multiple ESC I've written a program which allows me to use my Arduino Uno to send a PWM to an Electronic Speed Controller. 5 seconds after startup the ESC is sent a config via PWM to signal the low to high PWM range to be used by the ESC. Then I can type into the Serial Monitor a single digit 0-9 to pick the PWM I want to send to my ESC. (n*2 = pwm)
This works great when running code for one ESC, but adding code for a second ESC causes significant issues (I only have one ESC plugged in). When I send a serial command to specify PWM rate, my ESC only receives the PWM change sometimes. About 2/3 times it ignores the PWM change and keeps spinning at the same rate it was already at.
What's going on here?
Code
WorkingESCSoftware (main file of .eno)
#include &lt;SoftwareServo.h&gt;
#include &quot;TestClass.h&quot;

int val;    // variable to read the value from the analog pin
int stage = 0;

TestClass t, t2;

void setup()
{
  Serial.begin(115200);

  t = TestClass();
  t.setPin(3);
  t2 = TestClass();
  t2.setPin(5);
}

void loop()
{
  t.loop();
  t2.loop();
}

TestClass.h
class TestClass {
  private:
    SoftwareServo servo;
    bool _initialized = false;
    
    int PWM_MAX = 180;
    int PWM_HIGH = 170;
    int PWM_MEDIUM = 80;
    int PWM_LOW = 40;
    int PWM_OFF = 25; // has to be between esc config off (stop) and low (slow spin)
  public:
    TestClass();
    void setPin(int);
    void loop();
};

TestClass::TestClass() {
  Serial.println(&quot;cycle lipo power to esc&quot;);
}

void TestClass::setPin(int pin) {
  this-&gt;servo.attach(pin);
}

void TestClass::loop() {
  //  Serial.println(&quot;loop&quot;);
  
  // 5 seconds after power, send PWM range to ESC
  if(millis() &gt; 5000 &amp;&amp; this-&gt;_initialized == false) {
    this-&gt;_initialized = true;

    Serial.println(&quot;esc setup&quot;);
    this-&gt;servo.write(PWM_MAX);
    delay(5);
    this-&gt;servo.write(PWM_OFF);
    delay(5);
    Serial.println(&quot;esc ready&quot;);
  }
  
  char r = Serial.read();
  String inStr = String(r);
  if(r &gt; -1) {
    Serial.print(&quot;serial in: &quot;);
    Serial.println(inStr);
  }

  use h, m, l, o, or 0-9 to control PWM of ESC
  if(r &lt;= -1) {
    // void
  } else if(inStr == &quot;h&quot;) { // high
    Serial.print(&quot;HIGH: &quot;);
    Serial.println(PWM_HIGH);
    this-&gt;servo.write(PWM_HIGH); // highest spinning (same as 160?)
  } else if(inStr == &quot;m&quot;) { // medium
    Serial.print(&quot;MED: &quot;);
    Serial.println(PWM_MEDIUM);
    this-&gt;servo.write(PWM_MEDIUM);
  } else if (inStr == &quot;l&quot;) { // low
    Serial.print(&quot;LOW: &quot;);
    Serial.println(PWM_LOW);
    this-&gt;servo.write(PWM_LOW);
  } else if (inStr == &quot;o&quot;) {
    Serial.println(&quot;OFF&quot;);
    this-&gt;servo.write(PWM_OFF);
  } else if (inStr.toInt() &gt;= 0 &amp;&amp; inStr.toInt() &lt;= 9) {
    int val = inStr.toInt() * 20;
    Serial.println(val);
    this-&gt;servo.write(val);
  }

//  SoftwareServo::refresh();
  this-&gt;servo.refresh();
}
You have two copies of your program sharing one physical resource, the serial port input to the Arduino.  It is by chance which of the two copies will receive and process the input data.  So this approach will not work dependably.

Semiphoras are often used when sharing hardware among several programs.  However you are not only sharing hardware, but also the data being sent to the Arduino.  And the data will only be sent once.

In this case, you need to handle the data once in code that is separate from the 2 instantiations of the ESP class.  That is, you need to process the received data once and pass that processed data to the appropriate instantiation of the ESP class.