En el Proyecto 26 vimos como hacer un sensor de aparcamiento y una vez terminado lo instalamos sobre un coche para comprobar su funcionamiento.
A continuación pongo a vuestra disposición toda la documentación necesaria -esquemas, topográficos y código- para hacer el coche de radiocontrol con sensor de aparcamiento.
Este coche está hecho con el shield para motores de Adafruit y un módulo bluetooth HC-05 como el utilizado en los proyectos 19 y 20.
Incluye el arduino principal, los leds del coche, el zumbador, un relé para activar y desactivar el sensor de aparcamiento y el sensor hecho con un arduino Nano v3.
Como veis el conjunto se compone de tres tarjetas: el Arduino principal, sobre el que va pinchado el shield para los motores, una pequeña tarjeta de interface para controlar el sensor, los leds, y el zumbador, y por último la tarjeta sobre la que va montado el sensor de aparcamiento.
Al ir montado el shield sobre el Arduino principal, tenemos que conectar el módulo bluetooth, las conexiones A0, A1 y A2 y la alimentación de 5 voltios directamente sobre los pines del shield.
La parte de control mediante bluetooth está basada en el proyecto original de Andi.Co.
/*
Caravana Radiocontrol Bluetooth with Parking Sensor.
Version On Change mode
*/
#include <AFMotor.h>
AF_DCMotor motor4(4, MOTOR12_1KHZ); // create motor #4, 1KHz pwm
AF_DCMotor motor3(3, MOTOR12_1KHZ); // create motor #3, 1KHz pwm
char command = 'S';
boolean warning = 0;
int cont = 0;
void setup() {
pinMode(A0, OUTPUT); // Caravan Lights
pinMode(A1, OUTPUT); // Parking Sensor
pinMode(A2, OUTPUT); // Piezo Speaker
Serial.begin(9600);
}
// MOTION FUNCTIONS
// Number 1
// if the command is 'G' the car go forward left
void goForwardLeft()
{
motor3.setSpeed(224);
motor4.setSpeed(255);
motor3.run(FORWARD);
motor4.run(BACKWARD);
}
// Number 2
// if the command is 'F' the car go forward
void goForward()
{
motor3.setSpeed(255);
motor4.setSpeed(255);
motor3.run(FORWARD);
motor4.run(BACKWARD);
}
// Number 3
// if the command is 'I' the car go forward right
void goForwardRight()
{
motor3.setSpeed(255);
motor4.setSpeed(224);
motor3.run(FORWARD);
motor4.run(BACKWARD);
}
// Number 4
// if the command is 'L' the car twist left slow
void twistLeft()
{
motor3.setSpeed(128);
motor4.setSpeed(128);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
}
// Number 5
// if the command is 'S' the motors will turn off
void stopMotors()
{
motor3.run(RELEASE);
motor4.run(RELEASE);
}
// Number 6
// if the command is 'R' the car twist right slow
void twistRight()
{
motor3.setSpeed(128);
motor4.setSpeed(128);
motor3.run(FORWARD);
motor4.run(FORWARD);
}
// Number 7
// if the command is 'H' the car go backward left
void goBackwardLeft()
{
motor3.setSpeed(176);
motor4.setSpeed(255);
motor3.run(BACKWARD);
motor4.run(FORWARD);
}
// Number 8
// if the command is 'B' the car go backward
void goBackward()
{
motor3.setSpeed(255);
motor4.setSpeed(255);
motor3.run(BACKWARD);
motor4.run(FORWARD);
}
// Number 9
// if the command is 'J' the car go backward right
void goBackwardRight()
{
motor3.setSpeed(255);
motor4.setSpeed(176);
motor3.run(BACKWARD);
motor4.run(FORWARD);
}
// LIGHTS FUNCTIONS
// if the command is 'W' the car leds will turn ON
void carLightsON()
{
digitalWrite(A0, HIGH); // set all leds ON
}
// if the command is 'w' the car leds will turn OFF
void carLightsOFF()
{
digitalWrite(A0, LOW); // set all leds OFF
}
// if the command is 'X' the warning leds will turn ON for ten times
void warningON() {
digitalWrite(A0, HIGH); // set leds ON
delay(500);
digitalWrite(A0, LOW); // set leds OFF
delay(500);
warning = 1;
}
// if the command is 'x' the warning leds will turn OFF
void warningOFF() {
digitalWrite(A0, LOW); // set leds OFF
}
// if the command is 'V' the speaker will turn ON
void piezoSpeakerON()
{
digitalWrite(A2, HIGH); // set speaker ON
}
// if the command is 'v' the speaker will turn OFF
void piezoSpeakerOFF()
{
digitalWrite(A2, LOW); // set speaker OFF
}
// if the command is 'U' the Parking Sensor will turn ON
void parkingSensorON() {
digitalWrite(A1, HIGH); // set parking sensor ON
}
// if the command is 'u' the Parking Sensor will turn OFF
void parkingSensorOFF() {
digitalWrite(A1, LOW); // set parking sensor OFF
}
// if the command is 'D' all Off
void allOff(){
motor3.run(RELEASE); // set Motor 3 OFF
motor4.run(RELEASE); // set Motor 4 OFF
digitalWrite(A0, LOW); // set all leds OFF
digitalWrite(A1, LOW); // set parking sensor OFF
digitalWrite(A2, LOW); // set speaker OFF
}
void loop()
{
//if some date is sent, reads it and saves in command
if(Serial.available() > 0){
command = Serial.read();
switch(command){
case 'S':
stopMotors();
break;
case 'F':
goForward();
break;
case 'I':
goForwardRight();
break;
case 'G':
goForwardLeft();
break;
case 'B':
goBackward();
break;
case 'J':
goBackwardRight();
break;
case 'H':
goBackwardLeft();
break;
case 'R':
twistRight();
break;
case 'L':
twistLeft();
break;
case 'U':
parkingSensorON();
break;
case 'u':
parkingSensorOFF();
break;
case 'W':
carLightsON();
break;
case 'w':
carLightsOFF();
break;
case 'V':
piezoSpeakerON();
break;
case 'v':
piezoSpeakerOFF();
break;
case 'X': warningON(); break;
//case 'x': warningOFF(); break;
case 'D':
allOff();
break;
}
if (warning == 1) {
for (cont=0; cont<9; cont++){
warningON();
}
}
if (cont >=9) {
cont = 0;
warning = 0;
warningOFF();
}
}
}
Espero que disfrutéis tanto montando vuestro proyecto, como yo he disfrutado en su desarrollo.
¡Hasta pronto!