Serial Communication

So, I am trying to communicate with to Arduino’s over UART. I have got everything to work with this code:

This code is sent to the the sender.

int anaTestVal[] = {0,0,0,0,0,0};

void setup()
{
  Serial.begin(9600);
  
  for (int i = 2; i >= 13; i++)
  {
    pinMode(i, OUTPUT); 
  }
  
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  pinMode(A3, INPUT);
  pinMode(A4, INPUT);
  pinMode(A5, INPUT);
   
  digitalWrite(2, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(7, HIGH);
  digitalWrite(8, HIGH);
  digitalWrite(12, HIGH);
  digitalWrite(13, HIGH);
 
  analogWrite(3, 255);
  analogWrite(5, 255);
  analogWrite(6, 255);
  analogWrite(9, 255);
  analogWrite(10, 255);
  analogWrite(11, 255);
    
}

void loop()
{
  //if(Serial.available())
  //{
  for (int i = 0; i <= 5; i++)
  {
    anaTestVal[i] = analogRead(i);
   /*
    Serial.print(anaTestVal[i]);
    Serial.print(" ");
    if(i >= 5)
    {
      Serial.println(""); 
    }
    delay(100);
    */
  }  
  Serial.flush();
  if (anaTestVal[0] <= 1024 && anaTestVal[0] >= 1000 && anaTestVal[1] <= 1024 && anaTestVal[1] >= 1000 && anaTestVal[2] <= 1024 && anaTestVal[2] >= 1000 && anaTestVal[3] <= 1024 && anaTestVal[3] >= 1000 && anaTestVal[4] <= 1024 && anaTestVal[4] >= 1000 && anaTestVal[5] <= 1024 && anaTestVal[5] >= 1000)
  {
    Serial.println('c');
  }
  else
  {
    Serial.println('b'); 
  } 
  //Serial.flush();
  delay(100); 
  //}
}

…and this code is sent to the receiver

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(13, 12, 11, 10, 9, 8);

char recievedChar = 'a';
void setup()
{
 Serial.begin(9600);
 Serial1.begin(9600); // set up Serial library at 9600 bps
 lcd.begin(16, 2);
 Serial.println(recievedChar); //Check to see if Monitor is working
 
 // Analog Test Master Side
  for (int i = 2; i <= 7; i++)
  {
    pinMode(i, OUTPUT);
  }
 
 for(int i = 2; i <= 7; i++)
  {
    analogWrite(i, 255);
  }
}

void loop()
{
 if (Serial1.available()){
 recievedChar = Serial1.read();
 if (recievedChar == 'c')
 {
 lcd.print("Pass");
 }
 else
 {
  //lcd.print("Fail"); 
 }
 Serial.print(recievedChar);
 }

}

This works perfectly with no issues.

but when I send it my new code to the receiver:

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(13, 12, 11, 10, 9, 8);

int buttonPin = 22;
int buttonVal = 0;

int buttonPin2 = 40;
int buttonVal2 = 0;

char incomingByte = 'a';

int PWMTestVal[] = {0,0,0,0,0,0};

// Digital Values for Digital Test
int dig1 = 0;
int dig2 = 0;
int dig3 = 0;
int dig4 = 0;
int dig5 = 0;
int dig6 = 0;

void setup()
{
  // Start Serial Communication
  Serial.begin(9600);
  Serial1.begin(9600);
  
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  
  // Start Button
  pinMode(buttonPin, INPUT);
  digitalWrite(buttonPin, HIGH);
  
  // Reset LCD button
  pinMode(buttonPin2, INPUT);
  digitalWrite(buttonPin2, HIGH);
  
  // Analog Test Master Side
  for (int i = 0; i <= 5; i++)
  {
    pinMode(i, INPUT);
  }
  
  // Digital Test Master Side
  for (int i = 23; i <= 42; i++)
  {
    pinMode(i, INPUT);
  }
  
  // Analog Test Master Side
  for (int i = 2; i <= 7; i++)
  {
    pinMode(i, OUTPUT);
  }
  
  // Set PWM pins to 255
  for(int i = 2; i <= 7; i++)
  {
    analogWrite(i, 255);
  }
  
  
}

void loop()
{
  buttonVal = digitalRead(buttonPin);
  buttonVal2 = digitalRead(buttonPin2);
  if ( buttonVal == 0 )
  {
     // Start Test
     //Serial.println("Button Pressed");
     
     while(buttonVal == 0)
     {
        // Keep reading buttonValue until the button is released
        buttonVal = digitalRead(buttonPin);
        // Do nothing as the button is being held down
     }
     
     // Start PWM Test
     for (int i = 0; i <= 5; i++)
     {
          PWMTestVal[i] = analogRead(i);
     }
     
     //Serial.print(PWMTestVal[0]);
     //Serial.print(PWMTestVal[1]);
     //Serial.print(PWMTestVal[2]);
     //Serial.print(PWMTestVal[3]);
     //Serial.print(PWMTestVal[4]);
     //Serial.print(PWMTestVal[5]);
     //Serial.println("");
     
     if (PWMTestVal[0] <= 1026 && PWMTestVal[0] >= 1010 && PWMTestVal[1] <= 1026 && PWMTestVal[1] >= 1010 && PWMTestVal[2] <= 1026 && PWMTestVal[2] >= 1010 && PWMTestVal[3] <= 1026 && PWMTestVal[3] >= 1010 && PWMTestVal[4] <= 1026 && PWMTestVal[4] >= 1010 && PWMTestVal[5] <= 1026 && PWMTestVal[5] >= 1010)
     {
       lcd.setCursor(0,0);
       lcd.print("Pass P ");
     }
     else
     {
       lcd.print("FAIL P "); 
     }
     // End PWM Test
     
     // Start Digital Test
     dig1 = digitalRead(23);
     dig2 = digitalRead(24);
     dig3 = digitalRead(25);
     dig4 = digitalRead(26);
     dig5 = digitalRead(35);
     dig6 = digitalRead(28);
     
     //Serial.println(dig1);
     //Serial.println(dig2);
     //Serial.println(dig3);
     //Serial.println(dig4);
     //Serial.println(dig5);
     //Serial.println(dig6);
     
     if (dig1 == 1 && dig2 == 1 && dig3 == 1 && dig4 == 1 && dig5 == 1 && dig6 == 1)
     {
       lcd.setCursor(8,0);
       lcd.print("Pass D ");
     }
     else
     {
       lcd.print("FAIL D "); 
     }
     // End Digital Test
     
     // Start Analog Test
     Serial.flush();
     if (Serial1.available())
     {
     incomingByte = Serial1.read();
     
     if (incomingByte == 'c')
     {
       lcd.setCursor(0,1);
       lcd.print("Pass A");
     }
     else
     {
       lcd.setCursor(0,1);
       lcd.print("FAIL A");
     }
     
     Serial.println(incomingByte);
     }
     Serial.flush();
     // End Analog Test    
  }
  if (buttonVal2 == 0)
  {
    lcd.clear();
    delay(100);
  }
}

I start to see the issue; my problem is I normally get my data later than expected for example the analog test will fail and I will still be receiving “c” when I run my test instead of the “b” that I would expect.

so my Serial Monitor on the receiving arduino looks like this:

c
c
c
c
c
c
c
c
…and then finally it goes to b’s
b
b
b
b
b
b
b

the data is being delayed for what ever reason I have tried to flush the data out with no success.

The receiving Arduino’s Serial monitor should look like this when it is passing:

c
c
c
c
c
c
c
c
c

and the when it is failing should see this:

b
b
b
b
b
b

the problem is the transition from passing to failing is to slow.

any help with this problem would be greatly appreciated.

thanks

It would probably be a good idea to give a breif overview of what you are trying to accomplish with that code; it gives context to what you are trying to do with each line of code.

Is all the commented out code related to the problem? Of course not. So, why did you post it? If your delete key is broken, fix it.