0% found this document useful (0 votes)
49 views9 pages

Control de Coche Bluetooth con Arduino

Auto Bluetooth informe

Uploaded by

alego arandi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views9 pages

Control de Coche Bluetooth con Arduino

Auto Bluetooth informe

Uploaded by

alego arandi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

COCHE A BLUETOOTH

INTEGRANTES: GUZMAN OPORTO LUIS FERNANDO


VARGAS GARCIA DANTE ARIEL
PATIÑO GUZMAN NICOLE
CABRERA GOMEZ SEBASTIAN MATEO
CODIGO:
#include <Servo.h>
#include <SoftwareSerial.h>

// Instanciamos objeto SoftwareSerial para controlar el bluetooth


SoftwareSerial hc06(8, 9);

// Declaramos la variable para controlar el servo


Servo servoMotor;

// Definición de pines para control de velocidad y sensores


int VelocidadMotor1 = 2;
int VelocidadMotor2 = 7;
int Motor1A = 3;
int Motor1B = 4;
int Motor2C = 5;
int Motor2D = 6;
const int Trigger = 12;
const int Echo = 13;

// Definición de la velocidad inicial de los motores


int speedMotor = 255;

// Variable para capturar el comando que llega desde la app


String cmd = "";

void setup() {
[Link](9600);

[Link](9600);

// Configuraciones del servo


[Link](11);
[Link](90);

// Configuraciones de sensores y motores


pinMode(Trigger, OUTPUT);
pinMode(Echo, INPUT);
digitalWrite(Trigger, LOW);

pinMode(Motor1A, OUTPUT);
pinMode(Motor1B, OUTPUT);
pinMode(Motor2C, OUTPUT);
pinMode(Motor2D, OUTPUT);
pinMode(VelocidadMotor1, OUTPUT);
pinMode(VelocidadMotor2, OUTPUT);

analogWrite(VelocidadMotor1, speedMotor);
analogWrite(VelocidadMotor2, speedMotor);
}

void loop() {
long duration;
long distance;

digitalWrite(Trigger, HIGH);

delayMicroseconds(10);
digitalWrite(Trigger, LOW);
duration = pulseIn(Echo, HIGH);
distance = duration * 0.034 / 2;

[Link]("Distancia: ");
[Link](distance);
[Link]("cm");

delay(100);

while ([Link]() > 0) {


cmd += (char)[Link]();
}
if (distance > 16 && cmd != "") {
handleCommand(cmd[0]);
cmd = "";
} else if (distance <= 16) {
executeAvoidanceManeuver();
}

// Control de servo
[Link](65);
delay(2000);
[Link](115);
delay(1000);

void handleCommand(char command) {


switch (command) {
case 'S': stopCar(); break;
case 'F': moveForwardCar(); break;
case 'B': moveBackwardsCar(); break;
case 'L': turnLeftCar(); break;
case 'R': turnRightCar(); break;
case 'G': moveForwardLeft(); break;
case 'I': moveForwardRight(); break;
case 'H': moveBackwardsLeft(); break;
case 'J': moveBackwardsRight(); break;
default: adjustSpeed(command); break;
}
}

void adjustSpeed(char level) {


int speeds[] = {80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180};
if (level >= '0' && level <= '9') {
speedMotor = speeds[level - '0'];
} else if (level == 'q') {
speedMotor = 180;
}
analogWrite(VelocidadMotor1, speedMotor);
analogWrite(VelocidadMotor2, speedMotor);
}

void executeAvoidanceManeuver() {
moveBackwardsCar();
delay(2500);
stopCar();
}

void stopCar() {
digitalWrite(Motor1A, LOW);
digitalWrite(Motor1B, LOW);
digitalWrite(Motor2C, LOW);
digitalWrite(Motor2D, LOW);
}
void turnRightCar() {
digitalWrite(Motor1A, HIGH);
digitalWrite(Motor1B, LOW);
digitalWrite(Motor2C, LOW);
digitalWrite(Motor2D, LOW);
}

void turnLeftCar() {
digitalWrite(Motor1A, LOW);
digitalWrite(Motor1B, LOW);
digitalWrite(Motor2C, LOW);
digitalWrite(Motor2D, HIGH);
}

void moveForwardCar() {
digitalWrite(Motor1A, HIGH);
digitalWrite(Motor1B, LOW);
digitalWrite(Motor2C, LOW);
digitalWrite(Motor2D, HIGH);
}

void moveBackwardsCar() {
digitalWrite(Motor1A, LOW);
digitalWrite(Motor1B, HIGH);
digitalWrite(Motor2C, HIGH);
digitalWrite(Motor2D, LOW);
}
void moveForwardLeft() {
analogWrite(VelocidadMotor2, speedMotor + 60);
moveForwardCar();
delay(20);
analogWrite(VelocidadMotor2, speedMotor);
}

void moveForwardRight() {
analogWrite(VelocidadMotor1, speedMotor + 60);
moveForwardCar();
delay(20);
analogWrite(VelocidadMotor1, speedMotor);

void moveBackwardsLeft() {
analogWrite(VelocidadMotor2, speedMotor + 60);
moveBackwardsCar();
delay(20);
analogWrite(VelocidadMotor2, speedMotor);
}

void moveBackwardsRight() {
analogWrite(VelocidadMotor1, speedMotor + 60);
moveBackwardsCar();
delay(20);
analogWrite(VelocidadMotor1, speedMotor);
}

You might also like