Advertisement
AntonioVillanueva

Control Motor PAP nema 11 con A4988

Mar 1st, 2024 (edited)
857
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*        Control MOTOR NEMA 11 con A4988 y Arduino
  2.  *                Antonio Villanueva Segura
  3.  *                          A4988
  4.  *                    /ENABLE    VMOT +8V
  5.  *                    MS1       GND GND
  6.  *                    MS2       2B   AZUL (NEMA 11)
  7.  *                    MS3       2A   ROJO (NEMA 11)
  8.  *     cable a SLEEP  RESET     1A   NEGRO (NEMA 11)
  9.  *     cable a RESET  SLEEP     1B   VERDE (NEMA 11)
  10.  *    (PIN 2 ARDUINO )STEP      VDD +5V_ARDUINO
  11.  *    (PIN 5 ARDUINO) DIR       GND GND GND_ARDUINO
  12.  */
  13. /**************************************************************************************************************/
  14. //Definiciones de PINs en el ARDUINO uno
  15. #define EN 8 /* Habilita A4988 */
  16. #define DIR 5 /* Sentido de giro */
  17. #define STEP 2 /* Pasos */
  18. /**************************************************************************************************************/
  19. //Variables globales
  20. float angulo(-1); //Seleccion
  21. int pasos(0);
  22. /**************************************************************************************************************/
  23. void setup()//Configuracion
  24. {
  25.   Serial.begin(9600);//Velocidad puerto serie
  26.  
  27.   pinMode(EN, OUTPUT);//PIN ENABLE como salida
  28.   pinMode(DIR, OUTPUT);//DIR como salida
  29.   pinMode(STEP, OUTPUT);//STEP como salida
  30.   digitalWrite(EN, HIGH); //Ponemos a nivel bajo ENABLE , habilita A4988
  31. }
  32. /**************************************************************************************************************/
  33. void sentidoGiro(boolean sentido){//Sentido de giro
  34.     digitalWrite(DIR, sentido);
  35. }
  36. /**************************************************************************************************************/
  37. void unPaso(){//Un paso
  38.   digitalWrite(EN, LOW); //LOW a4988 activo
  39.  
  40.   digitalWrite(STEP, HIGH);//Crea el paso
  41.   delay(1);
  42.   digitalWrite(STEP, LOW);
  43.   delay(1);  
  44. }
  45. /**************************************************************************************************************/
  46. //Traduce el Angulo a una secuencia de pasos
  47. int AnguloPasos(float angulo_new){//Paso entero
  48.   int mult(1);
  49.   return (abs (angulo_new) / 1.8)*mult;
  50. }
  51. /**************************************************************************************************************/
  52. //Lee una cadena de texto del puerto serie
  53. String leeCadena(){
  54.   String cadena="";
  55.   if (Serial.available()) {cadena = Serial.readStringUntil('\n');}
  56.   return cadena;
  57. }
  58. /**************************************************************************************************************/
  59. //Menu lectura Serie, devuelve un int valor de Angulo
  60. float menu(){
  61.   Serial.print ("Angulo :");
  62.   String lectura="";
  63.   while (lectura== "" ){lectura=leeCadena();}//Espera un valor numerico dee grados
  64.   Serial.println (lectura);//Muestra el numero de pasos
  65.   return lectura.toFloat();
  66. }
  67. /**************************************************************************************************************/
  68. /**************************************************************************************************************/
  69. /**************************************************************************************************************/
  70. void loop()//Bucle principal ARDUINO
  71. {
  72.  
  73.   angulo = menu();//Muestra mensaje y espera un Angulo
  74.   pasos=AnguloPasos(angulo);//Traduce el angulo a numero de pasos
  75.  
  76.   sentidoGiro (angulo >0? true :false);//Selecciona sentido angulo + o -
  77.  
  78.   while (pasos--){unPaso();}//Ejecuta el numero de pasos
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement