im programming a car controled with an HC-05. I've run into the problem of an if statement that shouls stop all the motors if the distance in less than 15, but it only works if the distance is less than 15 while i send another command. How can i make it to work all the time instead of only working when another command is sent to de car?
this is the code i have:
#include <SoftwareSerial.h>
#include <Servo.h>
Servo myservo;
SoftwareSerial CUCA (7, 8); //RX | TX0
char DATO = 0;
// define L293D
const int motorPin1 = 5; // 1r motor pin 2 L293D
const int motorPin2 = 6; //pin 7 L293D
const int motorPin3 = 12; // pin 10
const int motorPin4 = 13; // pin 14
// defines sensor pins numbers
const int trigPin = 9; // defineix una variable constant, una variable que no canbia de valor en tot el programa
const int echoPin = 10;
//Prototipos de las funciones
double medirDistancia();
// define LEDS
int LED_FRENOS = 2;
// defines variables
long duration; // variable converteix en un valor long la variable. var. que mesura la duració
int distance; // var. que mesura la distancia mesurada pel sensor
int safetyDistance;
void setup() {
//SENSOR
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
//pinMode(led, OUTPUT); // LED as an output
pinMode(motorPin1,OUTPUT); //motor 1
pinMode(motorPin2,OUTPUT);
pinMode(motorPin3, OUTPUT); //MOTOR 2
pinMode(motorPin4, OUTPUT);
// LEDS
pinMode(LED_FRENOS, OUTPUT); // LED frens
Serial.begin(9600); // Starts the serial communication
CUCA.begin(38400); // començament del HC-05
//SERVO
myservo.attach(3);
myservo.write (90);
}
void loop() {
distance = medirDistancia(); //Llama a medirDistancia
while (CUCA.available()){
DATO = CUCA.read();
//ENDAVANT
if (DATO == '1') {
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
}
//ENDARRERE
if (DATO == '2'){
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
}
//STOP
if (DATO == 'p'){
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
}
if (DATO == '3')
myservo.write (40);
if (DATO == '4')
myservo.write (140);
if (DATO == 's')
myservo.write (90);
if (safetyDistance <= 15) {
digitalWrite (LED_FRENOS, HIGH);
digitalWrite(motorPin1, LOW); //motor 1 OFF
digitalWrite(motorPin2, LOW); // direcció OFF
digitalWrite(motorPin3, LOW); //motor 2 OFF
digitalWrite(motorPin4, LOW); // direcció OFF
//ESPERA 2s
delay(2000);
// DIRECCIÓ CONTRARIA 1s
digitalWrite(motorPin1, LOW); //motor 1 OFF
digitalWrite(motorPin2, HIGH); // direcció OFF
digitalWrite(motorPin3, LOW); //motor 2 OFF
digitalWrite(motorPin4, HIGH); // direcció OFF
delay(1000);
// GIRAR SERVO- ENDEVANT
myservo.write (40);
delay(500);
digitalWrite(motorPin1, HIGH); //motor 1 OFF
digitalWrite(motorPin2, LOW); // direcció OFF
digitalWrite(motorPin3, HIGH); //motor 2 OFF
digitalWrite(motorPin4, LOW);
delay(1500);
//APAGA MOTORS - TORNAR SERVO A 90
digitalWrite(motorPin1, LOW); //motor 1 OFF
digitalWrite(motorPin2, LOW); // direcció OFF
digitalWrite(motorPin3, LOW); //motor 2 OFF
digitalWrite(motorPin4, LOW); // direcció OFF
myservo.write(90);
// digitalWrite(LED_FRENOS, LOW);
delay(1000);
digitalWrite (LED_FRENOS, LOW);
safetyDistance = 100;
}
}
}
//medirDistancia: mide la distancia con un sensor de ultrasonidos
double medirDistancia(){
int distance = 0;
long duration = 0;
digitalWrite(trigPin, LOW);
delayMicroseconds(4);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
//Mide el tiempo entre pulsos (microsegundos)
duration = pulseIn(echoPin, HIGH)/2;
//Pasamos de tiempo a distancia (cm)
distance = duration /29.2;
safetyDistance = distance;
return safetyDistance;
}
Any tips would be greatly appreciated