Advertisement
pepi00

Untitled

Jun 11th, 2015
2,914
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
E 5.04 KB | None | 0 0
  1. /Programa prueba modulo bluetooth GP-GC021 o HC-06
  2. //Probado con programas para android Blueterm y Amarino 2.0
  3. //Probado con bluetooth portatil y software Octoplus Terminal
  4.  
  5. #include <AFMotor.h>
  6. #include <Servo.h>
  7. #include <Ultrasonic.h>
  8.  
  9. Ultrasonic ultrasonic(12,13); // (Trig PIN,Echo PIN)
  10.  
  11.  
  12. AF_DCMotor motorLeft(1);
  13. AF_DCMotor motorRight(2);
  14. AF_DCMotor pito(3);
  15. int potVelocidad = A0;            //potenciometro que varìa la velocida del motor
  16. int velocidad;                 //variable velocidad del motor (0 a 255)
  17. int random1, random2,random3;
  18. boolean automatico=true;
  19. int led1=52;
  20. int led2=53;
  21. //Servo servo1;
  22. //int val=90;    // variable to read the value from the analog pin
  23.  
  24.  
  25. void setup()
  26. {
  27.    //Configuracion de la velocidad del modulo 9600 por defecto, se puede cambiar
  28.    //mediante comandos AT
  29.    //Serial.begin(9600);
  30.    pinMode(potVelocidad, INPUT);
  31.    pinMode(led1, OUTPUT);
  32.    pinMode(led2, OUTPUT);  
  33.     // turn on servo
  34.  // servo1.attach(9);
  35.  
  36.  Serial.begin(9600);
  37.  Serial.begin(9600);
  38. }
  39.  
  40. void loop()
  41. {
  42.  
  43.  int x=analogRead(potVelocidad);
  44.  velocidad=map(x,0, 1023, 0, 255);
  45.  
  46.   motorLeft.setSpeed(velocidad);
  47.   motorRight.setSpeed(velocidad);
  48.   pito.setSpeed(250);
  49.   //servo1.write(val);
  50.  
  51.   if(automatico==true)
  52.  {
  53.    digitalWrite(led1, HIGH);
  54.       digitalWrite(led2, LOW);
  55.    autoControl();  //delay(500);
  56.  }
  57.  else if(automatico==false)
  58.   {
  59.     digitalWrite(led2, HIGH);
  60.         digitalWrite(led1, LOW);
  61.     controlRemoto();
  62.   }  
  63.  
  64.  
  65.      
  66. }
  67.  
  68.  
  69.  //***************************** AUTO CONTROL  *********************************
  70.  void autoControl()
  71.   {
  72.        delay(200);
  73.        random1= 2; //random(2000);
  74.        random2= random(5000, 30000);
  75.        random3= random(5000, 10000);
  76.        Serial.println("si");
  77.        switch(random1)
  78.                 {
  79.                   case 1:  
  80.                   circulos(5000);
  81.                   break;
  82.                  
  83.                   case 2:
  84.                   obstaculos(random3);
  85.                   break;
  86.                  
  87.                   case 3:
  88.                   zigZag();
  89.                   break;
  90.                          
  91.                   default:
  92.                   esquivar2();
  93.                   break;
  94.                 }
  95.                 controlRemoto();
  96.   }
  97.  
  98. //***************************  CONTROL REMOTO ***************************************
  99. void controlRemoto()
  100.   {
  101.    //Mientras el puerto serie del modulo bluetooth esta disponible
  102.    
  103.    //Serial.println(ultrasonic.Ranging(CM));
  104.    
  105.    while (Serial.available())
  106.    {
  107.      
  108.        //delay(100);
  109.       //Guardamos en la variable dato el valor leido por el modulo bluetooth
  110.        char dato= Serial.read();
  111.        
  112.        //Comprobamos el dato
  113.          if(dato=='1')
  114.            {
  115.              motorLeft.run(FORWARD);
  116.              motorRight.run(BACKWARD); automatico=false;
  117.            }
  118.          
  119.          if(dato=='2') atras();
  120.          if(dato=='3') izq();
  121.          if(dato=='4') der();
  122.          if(dato=='5') parar(300);
  123.          if(dato=='6') pitar();
  124.          if(dato=='9') {automatico=true; break;}
  125.          if(dato=='0') parar(30);
  126.          
  127.        
  128.        //delay(100);
  129.    
  130.    }
  131.   }
  132.  
  133. // ********************** DESPLAZAMIENTOS DE JASGBOT **********************
  134. void zigZag()
  135. {
  136.   //convierte numero de ciclos a segundos
  137.   int aux=5;
  138.   while(aux>0)
  139.   {
  140.     //controlRemoto();
  141.   //adelante();  delay(1000);  parar(300);
  142.   for(int x=0; x<4; x++)
  143.    {
  144.     izq();delay(100); parar(100);
  145.     esquivar();
  146.    }
  147.   for(int x=0; x<4; x++)
  148.   {
  149.     esquivar();
  150.     der();delay(100); parar(100);
  151.   }
  152.   aux--;
  153.   }
  154. }
  155.  
  156. void circulos(int time)
  157. {
  158.  
  159.   //convierte numero de ciclos a segundos
  160.   int aux=(time*(0.25));
  161.   while(aux>0)
  162.   {
  163.       der();
  164.    esquivar();
  165.       aux--;    
  166.   }  
  167.  
  168.    //der();
  169.    //esquivar();
  170.    //delay(6000);
  171.  
  172.   parar(300);
  173. }
  174.  
  175. void obstaculos(int time)
  176. {
  177.  // controlRemoto();
  178.   //convierte numero de ciclos a segundos
  179.   int aux=(time*(0.25));
  180.   while(aux>0)
  181.   {
  182.       adelante();
  183.       aux--;    
  184.   }    
  185. }
  186.  
  187.  
  188. //************************* FUNCIONES BASICAS  **********************
  189. void parar(int time)
  190. {
  191.    motorLeft.run(RELEASE);
  192.    motorRight.run(RELEASE);
  193.    delay(time);
  194. }
  195.  
  196. void adelante()
  197. {
  198.    motorLeft.run(FORWARD);
  199.    motorRight.run(BACKWARD);
  200.    esquivar();
  201. }
  202.  
  203. void izq()
  204. {
  205.    motorLeft.run(RELEASE);
  206.    motorRight.run(BACKWARD);
  207. }
  208.  
  209. void der()
  210. {
  211.   motorLeft.run(FORWARD);
  212.   motorRight.run(RELEASE);
  213. }
  214.  
  215. void atras()
  216. {
  217.   motorLeft.run(BACKWARD);
  218.   motorRight.run(FORWARD);
  219. }
  220.  
  221. void pitar()
  222. {
  223.   pito.run(BACKWARD);
  224.   delay(60);
  225.   pito.run(RELEASE);
  226. }
  227.  
  228. void esquivar()
  229. {
  230.   //
  231.   if(ultrasonic.Ranging(CM)<10)
  232.           {
  233.             atras();
  234.             delay(1000);
  235.              izq();
  236.              delay(500);
  237.              parar(100);
  238.           }  
  239. }
  240.  
  241. void esquivar2()
  242. {
  243.   if(ultrasonic.Ranging(CM)<30)
  244.           {
  245.             atras();
  246.             delay(500);
  247.              izq();
  248.              delay(500);
  249.              parar(10);
  250.           }  
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement