So I am trying to make an RC car controlled by a mobile app. I made a joystick and sended the x and y of the joystick to an HC-05 to classify it as the angle of the servo (x) and the motor's potency (y).
As seen in the atachments, i receive this data every 0.2 seconds as i made a timer to try and avoid this problem in case it was stack overflow but it seems that after a certain quantity of bytes sended the app just starts lagging really hard making it imposible to send new data.
Here is the code:
#include <Servo.h>
#define mySerial Serial
#define rxPin 0
#define txPin 1
int enA = 10;
int in1 = 9;
int in2 = 8;
Servo myServo;
const int servoControlNumber = 0;
const int servoMin = 70;
const int servoMax = 110;
const int servoPin = 5;
const int motorMin = -100;
const int motorMax = 100;
int8_t PotenciaMotor;
// Usamos canal A
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
// mySerial.println("AT");
myServo.attach( servoPin );
pinMode(enA, OUTPUT );
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT );
}
void loop() {
// Cuando tengamos 4 bytes listos, los leemos de golpe
if (mySerial.available() >= 4) {
int8_t AnguloServo = mySerial.read();
int8_t BytesSobrantes = mySerial.read();
int8_t PotenciaMotor = mySerial.read();
BytesSobrantes = mySerial.read();
Serial.println("Servo: " + String(AnguloServo) );
// Serial.println("Rubbish: " + String(BytesSobrantes) );
Serial.println("Motor: " + String(PotenciaMotor) );
// int String(AnguloServo) = map( valor, 0, 100, servoMin, servoMax );
// myServo.write( servoVal );
// }
PotenciaMotor = constrain(PotenciaMotor, -25, 125);
PotenciaMotor = map(PotenciaMotor, -25, 125, motorMax ,motorMin);
Serial.println("Potencia real: " + String(PotenciaMotor));
// digitalWrite( motorDireccion, PotenciaMotor > 0 );
if (PotenciaMotor > 0) {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
//Serial.println("1");
}
if (PotenciaMotor < 0) {
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
// Serial.println("2");
}
if (PotenciaMotor == 0) {
// Serial.println("3");
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
}
}
}