Serial Out Queuing, Sending Old Inputs

I'm working on a project where I have a Feather ESP-12S microcontroller running Arduino IDE sending data via serial to a 4D Systems Diablo display (which has an onboard microcontroller) to show graphics, but I've been running into a strange issue.

If I have the TX/RX hooked up when the system is powered up, the serial out is shifted back by one character. As an example, if I was to do:

Serial.write(1);
Serial.write(2);
Serial.write(3);

When I serial read on the Diablo, I get -1, then 1, then 2. If I were to serial read again, I would get a -1. However, the next time I write from the Feather to the Diablo, I would read a 3. The code on the Feather is not stopping anywhere and runs fine.

Interesting to note, if I plug the RX/TX cables in after powering up both screen and microcontroller, this problem doesn't happen.

The code below is what I was using to debug this. Essentially, the Neopixel ring should be green on even numbers and red on odds. My issue is causing these to be switched.

After a startup handshake (sending the value 9 back and forth), the Diablo just takes the value of i and prints it to the display.

Any help would be greatly appreciated, I'm completely stumped.

#include <Adafruit_NeoPixel.h>
#define PIN            14
#define NUMPIXELS      24
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
int i = 0;

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

//  Wait for serial
  while (! Serial) {
    delay(1);
  }

  delay(3000);
  while (Serial.read() != 9) {
    Serial.write(9);
    delay(100);
  }

  pixels.begin();
  for (int j = 0; j < NUMPIXELS; j++){
    pixels.setPixelColor(j,pixels.Color(0,0,0));
  }
  pixels.show();

}

void loop() {
  Serial.write(i);
  
  i = i + 1;

  if (i%2 == 1) {
    for (int j = 0; j < NUMPIXELS; j++){
      pixels.setPixelColor(j,pixels.Color(10,0,0));
    }
    pixels.show();
  }
  else if (i%2 == 0) {
    for (int j = 0; j < NUMPIXELS; j++){
      pixels.setPixelColor(j,pixels.Color(0,10,0));
    }
    pixels.show();
  }
  
  if(i>9) {
    i = 0;
  }
  delay(3000);
}

You didn't include the Diablo code, but...

When I serial read on the Diablo, I get -1, then 1, then 2. If I were to serial read again, I would get a -1. However, the next time I write from the Feather to the Diablo, I would read a 3. The code on the Feather is not stopping anywhere and runs fine.

Arduino (maybe Diablo is the same?) Serial.read() returns -1 if there is no data to read. Are you sure that you are not trying to read data that hasn't yet arrived and you are just getting -1?

You didn't show the reading code, but if this is any example...

  while (Serial.read() != 9) {
    Serial.write(9);
    delay(100);
  }

Then you're definitely reading when there's nothing to be read. That's when you get -1.

Learn to use Serial.available()

Also, you could drop the entire while(!Serial) loop. It does nothing, except on Arduinos with native USB such as the Leonardo and Due.

At 19200 baud, it takes roughly 500 us for a byte to be transferred. You however seem to read back within that time.

Not familiar with the feather but I assume that you can use Serial.available() to check if data is available to be read.

In a very simple form

void loop()
{
  static byte counter = 0;

  Serial.write(counter);
  while(Serial.available() == 0);
  int reply = Serial.read();

}

Note that this code blocks till a reply is received. The code assumes a single byte to be returned.

I'm working on a project where I have a Feather ESP-12S microcontroller running Arduino IDE

I do NOT believe that the ESP12S is capable of running the IDE OR that you have ported the IDE on the ESP12S.

Running code created using the IDE on a PC is a different story.