Delayed and scrambled inputs on serial monitor(RC car project)

Hello,

I'm new to programming and I'm having problems with my project. I'm building an RC car that will be controlled with an android app via Bluetooth HC-05 module. When I look at the inputs from the app on the serial monitor I only get parts of the values displayed, and there is a slight delay on the output of the motors. I diagnosed the problem down to the line, state = BTMOD.read(), where if I remove it the inputs show up as normal. The images attached should show what I mean by normal and messed up inputs. Am i missing something with the logic? Also, when I unblock the ultrasonic sensor code the delay in output is even greater. Is that related? Here is the code:

#include <SoftwareSerial.h>
#include <Servo.h>


SoftwareSerial BTMOD(10, 11); //RX , TX
Servo myservo;
int state = 0;
int distance;
long duration;
const int enA = 8;
const int in1 = 3;
const int in2 = 2;
const int enB = 9;
const int in3 = 4;
const int in4 = 5;
const int trig = 6;
const int echo = 7;


void setup() {
  Serial.begin(9600);
  BTMOD.begin(9600); //9600,0,0
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  myservo.attach(12, 600, 2300);
  digitalWrite(enA, HIGH);
  digitalWrite(enB, HIGH);

}

void loop() {

  if (BTMOD.available())
    Serial.write(BTMOD.read());
    state = BTMOD.read(); // <---------------------------------------Problem Here

/* ULTRASONIC LOOP
  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);

  duration = pulseIn(echo, HIGH);
  distance = duration * 0.034 / 2;

  Serial.print("Distance: ");
  Serial.println(distance);
 */ 
  
  
  if (state == 'f') {  //forward

    digitalWrite(in1, HIGH);
    digitalWrite(in3, HIGH);
    digitalWrite(in2, LOW);
    digitalWrite(in4, LOW);
  }
  if (state == 'v') {  //reverse
    digitalWrite(in1, LOW);
    digitalWrite(in3, LOW);
    digitalWrite(in2, HIGH);
    digitalWrite(in4, HIGH);
  }
  if (state == 's') {  //stop
    digitalWrite(in1, LOW);
    digitalWrite(in3, LOW);
    digitalWrite(in2, LOW);
    digitalWrite(in4, LOW);
  }
}

Thank you.

wrong.png

Right.png

 if (BTMOD.available())
    Serial.write(BTMOD.read());
    state = BTMOD.read();

Check to see if there is at least one character to read, then go ahead and read both of them.
(And one of them is read whether or not there's something to read)

Does that seem sensible?

Yes, I think I get what you mean. I will post if I figure it out, thanks.

@Balladin, I wonder if you think that this

 if (BTMOD.available())
    Serial.write(BTMOD.read());
    state = BTMOD.read(); //

puts into the variable state the same value as was sent with Serial.write(). And the answer is NO. Every Serial.read() removes a character from the serial input buffer.

If you want to save the value and pass it on then do it like this

  if (BTMOD.available())
    state = BTMOD.read();
   Serial.write(state);

And have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

Ok, so after some research and tweaking I found out that the Arduino was running the loop program faster than what the serial.write can display. So adding a 50 ms delay slowed down the loop enough to process the inputs on the serial monitor.

Balladin:
So adding a 50 ms delay slowed down the loop enough to process the inputs on the serial monitor.

The technique in the link in Reply #3 does not need any delay()

...R