Advertisement
RuiViana

GiraStep

Dec 19th, 2017
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.16 KB | None | 0 0
  1. #include <Servo.h>                              // incluir a biblioteca servo
  2. Servo myServo;
  3.  
  4. byte myServoPos = 90;                           // começa nos 90º
  5. byte myServoPin = 3;                            // servo no pin 3
  6. byte theButton = 2;                             // buttao no pin 2
  7. //--------------------------------------
  8. enum                                            // enumeração para switch case
  9. {
  10.   no_move_state,                                // estado parado
  11.   from_90_to_0,                                 // primeiro movimento
  12.   from_0_to_90,                                 // segundo movimento
  13.   from_90_to_180,                               // terceiro movimento
  14.   from_180_to_90                                // quarto movimento
  15. }
  16. currentState = no_move_state;                   // estado atual sem movimento
  17. unsigned long currentMillis;                    // tempo atual
  18. unsigned long previousMillis;                   // tempo anterior
  19. int myServoInterval = 28;                       // 2500/90=27.8 = 2.5 segundos a dividir por 90 angulos -> 27.5 milles/angulo
  20. int flag = 0;                                   // Controle de giro
  21. //--------------------------------------
  22. void setup()
  23. {
  24.   Serial.begin(9600);
  25.   pinMode (theButton, INPUT_PULLUP);            // para evitar o estado de alta inpedancia
  26.   myServo.attach(myServoPin);                   // associar pin 3 ao motor
  27.   myServo.write(myServoPos);                    // motor vai para aposicao atual
  28. }
  29. //--------------------------------------
  30. void loop()
  31. {
  32.   if (digitalRead(theButton) == LOW)            // Se theButton foi pressionado
  33.   {
  34.     delay(30);                                  // Debouncing
  35.     if (digitalRead(theButton) == LOW)          // Se theButton continua pressionado
  36.     {
  37.       flag = 1;                                 // Permite giro do motor
  38.     }
  39.   }
  40.   if (flag == 1)                                // Se permitiu giro, faca
  41.   {
  42.     switch (currentState)                       //Switch case function
  43.     {
  44.       case no_move_state:                       //case em que o motor esta parado
  45.         currentState = from_90_to_0;            //mover motor para de 90ºpara  0º
  46.         break;
  47.  
  48.       case from_90_to_0:                        // caso em que o servo esta a 0º
  49.         currentMillis = millis();
  50.         if (currentMillis - previousMillis >= myServoInterval)   //tempo atual - tempo previo é maior ou igual a velocidade do mutor
  51.         {
  52.           myServoPos = myServoPos - 1;          // retirar 1 angulo a cada millie
  53.           myServo.write(myServoPos);            // mover servo para posicao atual = 0
  54.           previousMillis = currentMillis;       //fazer seter ao valor dos millies
  55.         }
  56.         if (myServoPos == 0)                    //se o servo esta na posicao 0º entao...
  57.         {
  58.           currentState = from_0_to_90;          // mover servo pata 90º
  59.         }
  60.         break;
  61.  
  62.       case from_0_to_90:
  63.         currentMillis = millis();
  64.         if (currentMillis - previousMillis >= myServoInterval)
  65.         {
  66.           myServoPos = myServoPos + 1;
  67.           myServo.write(myServoPos);
  68.           previousMillis = currentMillis;
  69.         }
  70.         if (myServoPos == 90)
  71.         {
  72.           currentState = from_90_to_180;
  73.         }
  74.         break;
  75.  
  76.       case from_90_to_180:
  77.         currentMillis = millis();
  78.         if (currentMillis - previousMillis >= myServoInterval)
  79.         {
  80.           myServoPos = myServoPos + 1;
  81.           myServo.write(myServoPos);
  82.           previousMillis = currentMillis;
  83.         }
  84.         if (myServoPos == 180)
  85.         {
  86.           currentState = from_180_to_90;
  87.         }
  88.         break;
  89.  
  90.       case from_180_to_90 :
  91.         currentMillis = millis();
  92.         if (currentMillis - previousMillis >= myServoInterval)
  93.         {
  94.           myServoPos = myServoPos - 1;
  95.           myServo.write(myServoPos);
  96.           previousMillis = currentMillis;
  97.         }
  98.         if (myServoPos == 90)
  99.         {
  100.           currentState = no_move_state;                      // fazer reset
  101.           flag = 0;                                          // Bloqueia giro
  102.         }
  103.         break;
  104.     }
  105.   }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement