Problem Joining Two sketches (slow reaction from serial) (SOLVED!!)

Hello,
I have been working on a robotic boat project and i finally got the motor and the servo programmed and controlled with keyboard inputs. Then i joined the two together and now i do not get instant responses from the motor or the servo. I have to press the keys more than once or hold them down before i get a response. Thanks to zoomkat for the start of code.

Here is my code:

// Based on zoomkat test 22
#include <Servo.h>
char val;         // Data received from the serial port
int motorpin = 12; // Wiring: Connect L293D Pin En1 connected to pin PWM 4
Servo myservo1;

int pos1 = 0;    
int increment = 50;
int decrement = 50;

void setup(){
  pinMode(12, OUTPUT); //Initiates Motor Channel A pin
  pinMode(9, OUTPUT); //Initiates Brake Channel A pin
  myservo1.writeMicroseconds(1600); //set initial servo position if desired
  myservo1.attach(9, 500, 2500);  //the pin for the servo control, and range if desired
  Serial.begin(9600); // Start serial communication at 9600 bps
}

void loop()
{
  if (Serial.available())         // If data is available,
  {
    val = Serial.read();          // read it and store it in val
    if (val == '2')               // If '2' was received,
    {
      digitalWrite(12, HIGH);  //Establishes forward direction of Channel A
      analogWrite(3, 63.75);   // turn the motor on at 42.5 speed
    }
    if (val == '3')            // If '3' was received,
    {
      digitalWrite(12, HIGH);  //Establishes forward direction of Channel A
      analogWrite(3, 85);      // turn the motor on at 85 speed
    }
    if (val == '4')            // If '4' was received,
    {
      digitalWrite(12, HIGH);  //Establishes forward direction of Channel A
      digitalWrite(3, 127.5);  // turn the motor on at 127.5 speed
    }
    if (val == '5')            // If '5' was received,
    {
      digitalWrite(12, HIGH);  //Establishes forward direction of Channel A
      analogWrite(3, 170);     // turn the motor on at 170 speed
    }
    if (val == '6')            // If '6' was received,
    {
      digitalWrite(12, HIGH);  //Establishes forward direction of Channel A
      analogWrite(3, 212);     // turn the motor on at 212 speed
    }
    if (val == '7')           // If '7' was received,
    {
      digitalWrite(12, HIGH); //Establishes forward direction of Channel A
      analogWrite(3, 255);    // turn the motor on at 255 speed
    }
    else if (val == '1')      // If '1' was received
    {
      analogWrite(3, 0);      // turn the motor off
    }
    if (val == '8')           // If '8' was received,
    {
      digitalWrite(12, LOW);  //Establishes backward direction of Channel A
      digitalWrite(3, 255);   // turn the motor on at 255 speed
  }
  
}
  if (Serial.available())     // If data is available,
  {   
    int dataByte = Serial.read(); 
    Serial.print(dataByte); 
        if(dataByte == 'A'){      
             Serial.print("writing Microseconds: ");
             myservo1.writeMicroseconds(1200); // Turns the servo 45 degrees to the left
             }                
             else if(dataByte == 'D') {   
             Serial.print("writing Microseconds: ");
             myservo1.writeMicroseconds(2000);   // Centers the servo
             }
             else            //otherwise
             { 
             Serial.print("writing Microseconds: ");
             myservo1.writeMicroseconds(1600);   // Turns the servo 45 degrees to the right
             }
}
}

Separately the code for the motor and the code for the servo both work perfectly. But when i join them together and run processing the servo and the motor are slow to respond.

Can anyone offer constructive help?

There shouldn’t be separate sections dealing with Serial data in. At this point, it’s a complete toss-up as to which section handles data when it is read.

Arrch do you have any ideas how to set it up

Driverx3: Arrch do you have any ideas how to set it up

Only have 1 call to Serial.read().

Arrch how can i join val = Serial.read(); and val = Serial.read();

Driverx3: Arrch how can i join val = Serial.read(); and val = Serial.read();

Don't "join" them. Get rid of one.

Arrch. Thanks i was turning my motor with with numbers and the servo with letters. I followed your advise used only one serial.read and used letters to control the motor and the servo. Thanks again