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.