Buenas a todos! Resulta que llevo días intentando unir dos códigos de Arduino en uno pero es algo que me resulta imposible de hacer.
Primero de nada, mi idea es montar un radar detector de objetos mediante un sensor HC-SR04, donde este sensor girará 180º gracias a un servo-motor. La información del objeto se mostrará en pantalla mediante un esquema.
Mi problema llega cuando quiero conectar un módulo bluetooth y que se vea la distancia del objeto en la pantalla de mi teléfono. Utilizo la aplicación "BLUETOOTH ELECTRONICS" de la Google Play y detecta el módulo bluetooth pero no me muestra nada en la pantalla. También tengo un pin en mi Arduino Uno que tengo que utilizarlo para conectar dos cables (GND, sé que tengo dos pero el otro está en funcionamiento y solo tengo este para conectar dos cables).
El radar me funciona correctamente pero me falla el módulo bluetooth porque no me muestra la distancia en la aplicación aunque el teléfono lo detecte.
Cualquier ayuda sería de mucha Ayuda. Os dejo los vídeos que tomé como referencia y los códigos empleados. Muchas gracias foreros!
Arduino Radar System. Detector. (Ultrasonic sensor and Servo Motor) - YouTube (RADAR DETECTOR)
How to use Ultrasonic sensor with Android phone | Arduino Ultrasonic sensor project - YouTube (MÓDULO BLUETOOTH)
Mi idea es unir el módulo bluetooth al primer proyecto del radar, ya que ese me funciona. Los códigos vienen en la descripción de los vídeos pero aún así los dejo por aqui.
CÓDIGO DEL RADAR DETECTOR: ----------------------------------------------------------
// Includes the Servo library
#include <Servo.h>.
// Defines Tirg and Echo pins of the Ultrasonic Sensor
const int trigPin = 10;
const int echoPin = 11;
const int led1 = 9;
const int led2 = 8;
// Variables for the duration and the distance
long duration;
int distance;
Servo myServo; // Creates a servo object for controlling the servo motor
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
Serial.begin(9600);
myServo.attach(12); // Defines on which pin is the servo motor attached
}
void loop() {
// rotates the servo motor from 15 to 165 degrees
for(int i=15;i<=165;i++){
myServo.write(i);
delay(30);
distance = calculateDistance();// Calls a function for calculating the distance measured by the Ultrasonic sensor for each degree
Serial.print(i); // Sends the current degree into the Serial Port
Serial.print(","); // Sends addition character right next to the previous value needed later in the Processing IDE for indexing
Serial.print(distance); // Sends the distance value into the Serial Port
Serial.print("."); // Sends addition character right next to the previous value needed later in the Processing IDE for indexing
}
// Repeats the previous lines from 165 to 15 degrees
for(int i=165;i>15;i--){
myServo.write(i);
delay(30);
distance = calculateDistance();
Serial.print(i);
Serial.print(",");
Serial.print(distance);
Serial.print(".");
}
}
// Function for calculating the distance measured by the Ultrasonic sensor
int calculateDistance(){
if (distance < 30){
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
}
else{
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
}
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); // Reads the echoPin, returns the sound wave travel time in microseconds
distance= duration*0.034/2;
return distance;
}
CÓDIGO DEL MÓDULO BLUETOOTH: -----------------------------------------------------------
#define trigPin 10
#define echoPin 9
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
Serial.print(distance);
Serial.println(" cm");
delay(50);
}