Advertisement
AntonioVillanueva

Test Arduino

Dec 8th, 2018
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.08 KB | None | 0 0
  1. #include <Servo.h>
  2.  
  3.  
  4. /*
  5.  * sw pulsa gnd
  6.  */
  7.  
  8. //A0 giro brazo servo1
  9. //A1 LATERAL A servo2 elevacion pinza
  10. //A2 LATERAL B servo3
  11. //A3 PINZA servo4
  12. Servo servo1; Servo servo2;Servo servo3; Servo servo4;
  13. Servo servo_n[4]={servo1,servo2,servo3,servo4};
  14.  
  15. int vrx1,vry1;vrx2,vry2;//Lectura sensores analogicos
  16.  
  17. /********************************************************************************/
  18. void imprime(char* cadena,int v){//Funcion impresion un string y un valor decimal
  19.         Serial.println(cadena);
  20.         Serial.print(v,DEC);
  21.         Serial.println();
  22. }
  23. /********************************************************************************/
  24. void pinzaServo(int servo){//Control pinza 90 - 120 grados
  25.     for (int bucle=0;bucle<4;bucle ++){//Repite x veces
  26.       for (int grados=90 ;grados<=130;grados+=5){
  27.       servo_n[servo].write(grados);
  28.         delay(500);//Retardo 1seg.
  29.     }
  30.   }
  31.  
  32.   servo_n[servo].write(10);
  33.  
  34. }
  35. /********************************************************************************/
  36. void autoServo(int servo){//Movimiento automatico de un servo
  37.   for (int grados=0 ;grados<=180;grados+=10){
  38.     servo_n[servo].write(grados);
  39.       delay(1000);//Retardo 1seg.
  40.   }
  41.  
  42. }
  43. /********************************************************************************/
  44. void conectaServos(){//Conecta todos los servos
  45.   servo1.attach(14); //A0 analog pin 0
  46.   servo2.attach(15); //A1 analog pin 1
  47.   servo3.attach(16); //A1 analog pin 2
  48.   servo4.attach(17); //A1 analog pin 3  
  49. }
  50.  
  51. /********************************************************************************/
  52. void desconectaServos(){//Desconectta los servos
  53.   servo1.detach();; //A0 analog pin 0
  54.   servo2.detach(); //A1 analog pin 1
  55.   servo3.detach(); //A1 analog pin 2
  56.   servo4.detach(); //A1 analog pin 3  
  57. }
  58. /********************************************************************************/
  59. // the setup function runs once when you press reset or power the board
  60. void setup() {
  61.   // initialize digital pin LED_BUILTIN as an output.
  62.   pinMode(LED_BUILTIN, OUTPUT);
  63.   pinMode(1,OUTPUT);//LED
  64.  
  65.   servo1.attach(14); //A0 analog pin 0
  66.   //servo1.setMaximumPulse(2000);
  67.   //servo1.setMinimumPulse(700);
  68.  
  69.   servo2.attach(15); //A1 analog pin 1
  70.   servo3.attach(16); //A1 analog pin 2
  71.   servo4.attach(17); //A1 analog pin 3    
  72.  
  73.   Serial.begin(19200);
  74.   Serial.println(" Preparado ");
  75.  
  76.  // Servo servo_n[2]={servo1,servo2};
  77.  
  78. }
  79.  
  80. // the loop function runs over and over again forever
  81. void loop() {
  82.  
  83. static int v = 0;
  84. /*
  85.   digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  86.   delay(1000);                       // wait for a second
  87.   digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  88.   delay(1000);                       // wait for a second
  89. */
  90.   if ( Serial.available()) {//Si hay datos en el serie los lee , lectura grados servo y accion
  91.     char ch = Serial.read();//lee un caracter 0..9 ,s,w,d,a
  92.  
  93.     switch(ch) {
  94.       case '0'...'9'://Compone la posicion del servo en grados
  95.         v = v * 10 + ch - '0';
  96.         Serial.println ("Lectura ch");
  97.         Serial.print(ch);
  98.         break;
  99.  
  100.         //Giro del brazo 110 centro 0 90
  101.       case 's'://Servo conectado en A0 servo1
  102.        // servo1.write(v);//Envia el valor de grados al servo
  103.         servo_n[0].write(v);
  104.         imprime ("v=",v);//Debug servo
  105.         v = 0;
  106.         break;
  107.        
  108.       case 'w':
  109.              imprime ("sube baja ",v);//Debug servo
  110.         servo_n[1].write(v);//Eleva baja la pinza 0 alto 110 bajo
  111.         servo_n[2].write(v);// 110 subiendo .... abajo    
  112.         v = 0;
  113.         break;
  114.        
  115.        
  116.       case 'r'://Movimiento automamtico servo
  117.         imprime (" AUTOMATICO ",0);
  118.         autoServo(0);
  119.         v=0;
  120.         break;  
  121.  
  122.  
  123.       case 'p'://Mueve pinza
  124.         imprime (" pinza ",3);
  125.         pinzaServo(3);
  126.         break;              
  127.  
  128.  
  129.       case 'd'://Desconecta
  130.         desconectaServos();
  131.         break;
  132.        
  133.       case 'a'://Conecta
  134.         conectaServos();
  135.         break;
  136.        
  137.     }
  138.   }
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement