Advertisement
RuiViana

C

Jan 22nd, 2018
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.97 KB | None | 0 0
  1. // http://labdegaragem.com/forum/topics/motor-de-passo-com-comandos-por-botoes
  2. // Programa : Driver motor de passo A4988
  3. // Autor : Arduino e Cia
  4.  
  5. #include <AccelStepper.h>
  6.  
  7. int velocidade_motor1 = 100;        //  MOTOR   1
  8. int aceleracao_motor1 = 100;
  9. int sentido_horario1 = 0;
  10. int sentido_antihorario1 = 0;
  11.  
  12. int velocidade_motor2 = 100;        //  MOTOR    2
  13. int aceleracao_motor2 = 100;
  14. int sentido_horario2 = 0;
  15. int sentido_antihorario2 = 0;
  16. int numero = 0;
  17. int pino_enable = 10;               // Definicao pino ENABLE
  18. AccelStepper motor1(1, 12, 11 );    // Definicao pinos STEP e DIR
  19. AccelStepper motor2(1, 9, 8 );
  20. #define botA 4
  21. #define botB 5
  22. #define botC 6
  23. #define botD 7
  24. int flag = 0;
  25. //-------------------------------------------------------
  26. void setup()
  27. {
  28.   Serial.begin(9600);
  29.   pinMode(pino_enable, OUTPUT);
  30.  
  31.   motor1.setMaxSpeed(velocidade_motor1);        // Configuracoes iniciais motor de passo 1
  32.   motor1.setSpeed(velocidade_motor1);
  33.   motor1.setAcceleration(aceleracao_motor1);
  34.  
  35.   motor2.setMaxSpeed(velocidade_motor2);        // Configuracoes iniciais motor de passo 2
  36.   motor2.setSpeed(velocidade_motor2);
  37.   motor2.setAcceleration(aceleracao_motor2);
  38.  
  39.   Serial.println("Digite 1, 2, 3, 4 e 5 e clique em ENVIAR...");
  40.   pinMode(botA, INPUT_PULLUP);
  41.   pinMode(botB, INPUT_PULLUP);
  42.   pinMode(botC, INPUT_PULLUP);
  43.   pinMode(botD, INPUT_PULLUP);
  44. }
  45. //-------------------------------------------------------
  46. void loop()
  47. {
  48.   if (digitalRead(botA) == LOW)
  49.   {
  50.     delay(30);
  51.     if (digitalRead(botA) == LOW)
  52.     {
  53.       numero = 1;
  54.       flag = 1;
  55.     }
  56.   }
  57.   if (digitalRead(botB) == LOW)
  58.   {
  59.     delay(30);
  60.     if (digitalRead(botB) == LOW)
  61.     {
  62.       numero = 2;
  63.     }
  64.   }
  65.   if (digitalRead(botC) == LOW)
  66.   {
  67.     delay(30);
  68.     if (digitalRead(botC) == LOW)
  69.     {
  70.       numero = 3;
  71.     }
  72.   }
  73.   if (digitalRead(botD) == LOW)
  74.   {
  75.     delay(30);
  76.     if (digitalRead(botD) == LOW)
  77.     {
  78.       numero = 4;
  79.     }
  80.   }
  81.  
  82.   //  if (Serial.available() > 0)     // Aguarda os caracteres no serial monitor
  83.   //  {
  84.   //    numero = Serial.read();
  85.   //    {
  86.   if (numero == 1)
  87.   {
  88.     Serial.println("Numero 1 recebido - Girando motor1 sentido horario e motor2 sentido antihorario,  FRENTE");
  89.     digitalWrite(pino_enable, LOW);
  90.     sentido_horario1 = 1;
  91.     sentido_antihorario1 = 0;
  92.     sentido_horario2 = 0;
  93.     sentido_antihorario2 = 1;
  94.     numero = 0;
  95.   }
  96.   if (flag == 1)
  97.   {
  98.     if (sentido_horario1 == 1)              // Move o motor no sentido horario
  99.     {
  100.       motor1.moveTo(10000);
  101.     }
  102.     if (sentido_antihorario1 == 1)        // Move o motor no sentido anti-horario
  103.     {
  104.       motor1.moveTo(-10000);
  105.     }
  106.     if (sentido_horario2 == 1)
  107.     {
  108.       motor2.moveTo(10000);
  109.     }
  110.     if (sentido_antihorario2 == 1) // Move o motor no sentido anti-horario
  111.     {
  112.       motor2.moveTo(-10000);
  113.     }
  114.     motor1.run();  // Comando para acionar o motor no sentido especificado
  115.     motor2.run();
  116.     flag = 0;
  117.   }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement