Rafa Juárez
Published © GPL3+

3 Wheels autonomous vehicle

A simple Arduino vehicle with a chassis made of wood and hard cardboard.

BeginnerShowcase (no instructions)12 hours1,620
3 Wheels autonomous vehicle

Things used in this project

Hardware components

Dual H-Bridge motor drivers L298
SparkFun Dual H-Bridge motor drivers L298
×1
DC motor (generic)
×1
Arduino UNO
Arduino UNO
×1
canvas cardbo
×1
prototype PCB
×1
HC-06 Bluetooth Module
×1
Buzzer
Buzzer
×1

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
driller

Story

Read more

Schematics

Chassis

Code

Arduino code

Arduino
#include "pitches.h"
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX del arduino a los contrarios en el HC06. 
int Trig=5;
int Echo=3;
int remoto=12;
int aux;
int INA=2;
int INB=4;
//int ENA=9;
int INC=7;
int IND=8;
//int ENB=10;
//Variable en la que se va a almacenar el valor correspondiente a la distancia
int Dist,Dist1,Dist2;
float LOutput,ROutput;
char blueToothVal;           //value sent over via bluetooth
char lastValue;              //stores last state of device (on/off)
// notes in the melody:
int melody[] = {
  NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
  4, 8, 8, 4, 4, 4, 4, 4
};


void setup() {
  pinMode (Trig, OUTPUT);
  pinMode (Echo, INPUT);
  pinMode (remoto, INPUT);
  pinMode(INA,OUTPUT);
  pinMode(INB,OUTPUT);
  pinMode(INC,OUTPUT);
  pinMode(IND,OUTPUT);
  Serial.begin(9600);
  mySerial.begin(9600);

}

void loop() {
ultrasonido (Dist);
Dist1=Dist;

aux=digitalRead(remoto);
if (aux==LOW)
{
  movto();
}
else
{
  Bluetooth();
 delay(500);
}


 

}

void movto()
{
  if (Dist>45)
{
  //Adelante
      LOutput=-1;
      ROutput=1;
      PWMControl();
      }
       
else
{
  //Hacia atrs
      LOutput=1;
      ROutput=-1;
      PWMControl();
      delay(1500);
      //Gira en sentido horario, hacia la derecha
      LOutput=1;
      ROutput=1;
      PWMControl();
      delay(500);
     
} 
}
void PWMControl(){
  
  if(LOutput > 0){
    digitalWrite(INA, HIGH);
    digitalWrite(INB, LOW);
  }
  else if(LOutput < 0){
    digitalWrite(INA, LOW);
    digitalWrite(INB, HIGH);
  }
  else{
    digitalWrite(INA, HIGH);
    digitalWrite(INB, HIGH);
  }
  if(ROutput > 0){
    digitalWrite(INC, HIGH);
    digitalWrite(IND, LOW);
  }
  else if(ROutput < 0){   
    digitalWrite(INC, LOW);
    digitalWrite(IND, HIGH);
  }
  else{
    digitalWrite(INC, HIGH);
    digitalWrite(IND, HIGH);
  }
    //analogWrite(ENA, min(255, abs(LOutput) + LMotor_offset));
    //analogWrite(ENB, min(255, abs(ROutput) + RMotor_offset));
    
}


//Este mdulo calcula y devuelve la distancia en cm.
/*
Puedes poner el cdigo del mdulo directamente en el loop o utilizar el mdulo
para reducir el nmero de lneas de cdigo del loop o reutilizar el cdigo
*/
void ultrasonido (int &Distancia){
 
//Para estabilizar el valor del pin Trig se establece a LOW
digitalWrite (Trig, LOW);
delay(10);
 
//Se lanzan los 8 pulsos
digitalWrite (Trig, HIGH);
delay(10);
digitalWrite (Trig, LOW);
 
/*
Se mide el tiempo que tarda la seal en regresar y se calcula la distancia.
 
Observa que al realizar pulseIn el valor que se obtiene es tiempo, no distancia
 
Se est reutilizando la variable Distancia.
*/
 
Distancia= pulseIn (Echo, HIGH);
Distancia=Distancia/58;
 
delay(10);
  
}


void Bluetooth(){

if(mySerial.available())
  {//if there is data being recieved
    blueToothVal=mySerial.read(); //read it
  
    switch (blueToothVal) {
       case 'f':
      LOutput=-1;
      ROutput=1;
      PWMControl();
     mySerial.println("f = forward"); 
      musica_inicio();
       break;
       
       case 'r':
       LOutput=1;
       ROutput=-1;
       PWMControl();
      mySerial.println("r = reverse"); 
       break;
       
        case 'i':
        LOutput=-1;
        ROutput=-1;
        PWMControl();
      mySerial.println("i = Izquierda"); 

       break;
        case 'd':
        LOutput=1;
        ROutput=1;
        PWMControl();
        mySerial.println("d = Derecha"); 

       break;
     
     default: 
      mySerial.println("f = forward , r = reverse , d = Derecha , i = Izquierda. Programa --> coche1.ino"); 
      LOutput=0;
        ROutput=0;
        PWMControl();
     break;
   }  
  }
 }

void musica_inicio()
{
    // iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < 8; thisNote++) {

    // to calculate the note duration, take one second
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(9, melody[thisNote], noteDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(8);
  }
}
void musica()
{
// funcion = tone(pin, frequency, duration)  Piratas del Caribe

 tone(9,293.66,200);
  delay(200);
  tone(9,293.66,100);
  delay(100);
  tone(9,293.66,200);
  delay(200);
  tone(9,293.66,100);
  delay(100);
  tone(9,293.66,200);
  delay(200);
  tone(9,293.66,100);
  delay(100);
  tone(9,293.66,100);
  delay(100);
  tone(9,293.66,100);
  delay(100);
  tone(9,293.66,100);
  delay(100);
  tone(9,293.66,200);
  delay(200);
  tone(9,293.66,100);
  delay(100);
  tone(9,293.66,200);
  delay(200);
  tone(9,293.66,100);
  delay(100);
  tone(9,293.66,200);
  delay(200);
  tone(9,293.66,100);
  delay(100);
  tone(9,293.66,100);
  delay(100);
  tone(9,293.66,100);
  delay(100);
  tone(9,293.66,100);
  delay(100);
  tone(9,293.66,200);
  delay(200);
  tone(9,293.66,100);
  delay(100);
  tone(9,293.66,200);
  delay(200);
  tone(9,293.66,100);
  delay(100);
  tone(9,293.66,200);
  delay(200);
  tone(9,293.66,100);
  delay(100);
  tone(9,293.66,100);
  delay(100);
  tone(9,440,100);
  delay(100);
  tone(9,523.25,100);
  delay(100);
  tone(9,587.33,100);
  delay(200);
  tone(9,587.33,100);
  delay(200);
  tone(9,587.33,100);
  delay(100);
  tone(9,659.25,100);
  delay(100);
  tone(9,698.45,100);
  delay(200);
  tone(9,698.45,100);
  delay(200);
  tone(9,698.45,100);
  delay(100);
  tone(9,783.99,100);
  delay(100);
  tone(9,659.25,100);
  delay(200);
  tone(9,659.25,100);
  delay(200);
  tone(9,587.33,100);
  delay(100);
  tone(9,523.25,100);
  delay(100);
  tone(9,523.25,100);
  delay(100);
  tone(9,587.33,100);
  delay(300);
  tone(9,440,100);
  delay(100);
  tone(9,523.25,100);
  delay(100);
  tone(9,587.33,100);
  delay(200);
  tone(9,587.33,100);
  delay(200);
  tone(9,587.33,100);
  delay(100);
  tone(9,659.25,100);
  delay(100);
  tone(9,698.45,100);
  delay(200);
  tone(9,698.45,100);
  delay(200);
  tone(9,698.45,100);
  delay(100);
  tone(9,783.99,100);
  delay(100);
  tone(9,659.25,100);
  delay(200);
  tone(9,659.25,100);
  delay(200);
  tone(9,587.33,100);
  delay(100);
  tone(9,523.25,100);
  delay(100);
  tone(9,587.33,100);
  delay(400);
  tone(9,440,100);
  delay(100);
  tone(9,523.25,100);
  delay(100);
  tone(9,587.33,100);
  delay(200);
  tone(9,587.33,100);
  delay(200);
  tone(9,587.33,100);
  delay(100);
  tone(9,698.45,100);
  delay(100);
  tone(9,783.99,100);
  delay(200);
  tone(9,783.99,100);
  delay(200);
  tone(9,783.99,100);
  delay(100);
  tone(9,880,100);
  delay(100);
  tone(9,932.33,100);
  delay(200);
  tone(9,932.33,100);
  delay(200);
  tone(9,880,100);
  delay(100);
  tone(9,783.99,100);
  delay(100);
  tone(9,880,100);
  delay(100);
  tone(9,587.33,100);
  delay(300);
  tone(9,587.33,100);
  delay(100);
  tone(9,659.25,100);
  delay(100);
  tone(9,698.45,100);
  delay(200);
  tone(9,698.45,100);
  delay(200);
  tone(9,783.99,100);
  delay(200);
  tone(9,880,100);
  delay(100);
  tone(9,587.33,100);
  delay(300);
  tone(9,587.33,100);
  delay(100);
  tone(9,698.45,100);
  delay(100);
  tone(9,659.25,100);
  delay(200);
  tone(9,659.25,100);
  delay(200);
  tone(9,698.45,100);
  delay(100);
  tone(9,587.33,100);
  delay(100);
  tone(9,659.25,100);
  delay(400);
  tone(9,880,100);
  delay(100);
  tone(9,1046.50,100);
  delay(100);
  tone(9,1174.66,100);
  delay(200);
  tone(9,1174.66,100);
  delay(200);
  tone(9,1174.66,100);
  delay(100);
  tone(9,1318.51,100);
  delay(100);
  tone(9,1396.91,100);
  delay(200);
  tone(9,1396.91,100);
  delay(200);
  tone(9,1396.91,100);
  delay(100);
  tone(9,1567.98,100);
  delay(100);
  tone(9,1318.51,100);
  delay(200);
  tone(9,1318.51,100);
  delay(200);
  tone(9,1174.66,100);
  delay(100);
  tone(9,1046.50,100);
  delay(100);
  tone(9,1046.50,100);
  delay(100);
  tone(9,1174.66,100);
  delay(300);
  tone(9,880,100);
  delay(100);
  tone(9,1046.50,100);
  delay(100);
  tone(9,1174.66,100);
  delay(200);
  tone(9,1174.66,100);
  delay(200);
  tone(9,1174.66,100);
  delay(100);
  tone(9,1318.51,100);
  delay(100);
  tone(9,1396.91,100);
  delay(200);
  tone(9,1396.91,100);
  delay(200);
  tone(9,1396.91,100);
  delay(100);
  tone(9,1567.98,100);
  delay(100);
  tone(9,1318.51,100);
  delay(200);
  tone(9,1318.51,100);
  delay(200);
  tone(9,1174.66,100);
  delay(100);
  tone(9,1046.50,100);
  delay(100);
  tone(9,1174.66,100);
  delay(400);
  tone(9,880,100);
  delay(100);
  tone(9,1046.50,100);
  delay(100);
  tone(9,1174.66,100);
  delay(200);
  tone(9,1174.66,100);
  delay(200);
  tone(9,1174.66,100);
  delay(100);
  tone(9,1396.91,100);
  delay(100);
  tone(9,1567.98,100);
  delay(200);
  tone(9,1567.98,100);
  delay(200);
  tone(9,1567.98,100);
  delay(100);
  tone(9,1760,100);
  delay(100);
  tone(9,1864.66,100);
  delay(200);
  tone(9,1864.66,100);
  delay(200);
  tone(9,1760,100);
  delay(100);
  tone(9,1567.98,100);
  delay(100);
  tone(9,1760,100);
  delay(100);
  tone(9,1174.66,100);
  delay(300);
  tone(9,1174.66,100);
  delay(100);
  tone(9,1318.51,100);
  delay(100);
  tone(9,1396.91,100);
  delay(200);
  tone(9,1396.91,100);
  delay(200);
  tone(9,1567.98,100);
  delay(200);
  tone(9,1760,100);
  delay(100);
  tone(9,1174.66,100);
  delay(300);
  tone(9,1174.66,100);
  delay(100);
  tone(9,1396.91,100);
  delay(100);
  tone(9,1318.51,100);
  delay(200);
  tone(9,1318.51,100);
  delay(200);
  tone(9,1174.66,100);
  delay(100);
  tone(9,1108.73,100);
  delay(100);
  tone(9,1174.66,100);
  delay(200);
  tone(9,1174.66,100);
  delay(200);
  tone(9,1318.51,100);
  delay(200);
  tone(9,1396.91,100);
  delay(200);
  tone(9,1396.91,100);
  delay(100);
  tone(9,1396.91,100);
  delay(100);
  tone(9,1567.98,100);
  delay(200);
  tone(9,1760,300);
  delay(400);
  tone(9,1396.91,100);
  delay(100);
  tone(9,1174.66,100);
  delay(100);
  tone(9,880,300);
  delay(600);
  tone(9,1864.66,300);
  delay(400);
  tone(9,1396.91,100);
  delay(100);
  tone(9,1174.66,100);
  delay(100);
  tone(9,932.33,300);
  delay(600);
  tone(9,587.33,100);
  delay(100);
  tone(9,440,100);
  delay(200);
  tone(9,587.33,100);
  delay(300);
  tone(9,554.36,100);
  delay(400);
  tone(9,1567.98,100);
  delay(100);
  tone(9,1567.98,100);
  delay(100);

}

Credits

Rafa Juárez

Rafa Juárez

18 projects • 39 followers
Very interested in prototyping of new ideas. 30 years experience in electronics.

Comments