/************************************************************ * Sketch per control per bluetooth d'un cotxe teledirit. * Data: 01/04/2016 * Autor: T.Salas (salas.toni@gmail.com) ***********************************************************/
#include <Servo.h> #include <Ultrasonic.h>
String readStr=""; String marxa; String gir; long intGir; char c;
int servoPin = 9; int claxonPin = 7;
int trigPin = 4; int echoPin = 3;
int avantPin = 10; int enreraPin = 11; int potenciaPin = 5;
long distAturada = 40; //distancia a la que se aturarà quan detecte un obstable.
Servo servoMotor; Ultrasonic ultrasonic(trigPin,echoPin); // (Trig PIN,Echo PIN)
boolean modeAutomatic = false; //boolean modeAutomatic = true;
void setup(){ pinMode( avantPin, OUTPUT); pinMode( enreraPin, OUTPUT); pinMode( claxonPin, OUTPUT ); pinMode( trigPin, OUTPUT ); pinMode( echoPin, INPUT ); pinMode( potenciaPin, OUTPUT ); analogWrite( avantPin, 0 ); //inicialmet el cotxe estarà aturat analogWrite( enreraPin, 0 ); analogWrite( potenciaPin, 0 );
digitalWrite( claxonPin, LOW );
Serial.flush(); delay(100); servoMotor.attach( servoPin ); Serial.begin(9600); delay(100);
servoMotor.write(90); }
bool HihaObstable() { long dist1; dist1 = ultrasonic.Ranging(CM); //Serial.println(dist1); delay(60); //evita que es pugui tornar a solicitar la distància en un temps prudencial perquè funcioni correntamen el sr04 return( dist1 <= distAturada ); }
void pilotAutomatic() {
if( HihaObstable() ) { //Atura digitalWrite(avantPin,LOW); digitalWrite(enreraPin,LOW); analogWrite( potenciaPin, 0 ); delay(250);
//reculam servoMotor.write(90); //reculam 1 segon digitalWrite(avantPin,LOW); digitalWrite(enreraPin,HIGH); analogWrite( potenciaPin, 255 ); delay(500); //giram a esquerra servoMotor.write(0); //reculam 1 segon digitalWrite(avantPin,LOW); digitalWrite(enreraPin,HIGH); analogWrite( potenciaPin, 255 ); delay(1000); //aturam servoMotor.write(90); digitalWrite(avantPin,LOW); digitalWrite(enreraPin,LOW); analogWrite( potenciaPin, 0 ); delay(250);
} else { //ves envant servoMotor.write(90); digitalWrite(avantPin,HIGH); digitalWrite(enreraPin,LOW); analogWrite( potenciaPin, 255 ); } }
void loop() { if (Serial.available() > 0) { c = char(Serial.read()); if(c=='*'){
if( readStr.equals( "Claxon_on" ) ) { digitalWrite( claxonPin, HIGH ); } else if( readStr.equals( "Claxon_off" ) ) { digitalWrite( claxonPin, LOW ); } else if( readStr.equals( "Automatic_on" ) ) { modeAutomatic = true; } else if( readStr.equals( "Automatic_off" ) ) { modeAutomatic = false;
} else if( modeAutomatic ) { pilotAutomatic(); } else if( !modeAutomatic ) { //marxa#gir*
marxa = readStr.substring(0, readStr.indexOf('#'));
if( marxa.equals("Aturat")) { digitalWrite(avantPin,LOW); digitalWrite(enreraPin,LOW); analogWrite( potenciaPin, 0 ); } if( marxa.equals("Envant")) { digitalWrite(avantPin,HIGH); digitalWrite(enreraPin,LOW); analogWrite( potenciaPin, 255 ); } if( marxa.equals("Enrera")) { digitalWrite(avantPin,LOW); digitalWrite(enreraPin,HIGH); analogWrite( potenciaPin, 255 ); }
gir=readStr.substring( readStr.indexOf('#')+1, readStr.length() ); intGir = gir.toInt(); intGir = map(intGir, -90, 90, 0, 180); //passam a graus. servoMotor.write(intGir); } readStr=""; } else { readStr += c; } } else if( modeAutomatic ) { //En mode automàtic l'app android no envia dades per bluetooth pilotAutomatic(); } }
|