Advertisement
RuiViana

JAN_Slider_V08.ino

May 23rd, 2019
575
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.60 KB | None | 0 0
  1. // Versão StepMotor
  2. #include <AccelStepper.h>
  3.  
  4. //#define releK1 2                      // Port para o rele K1 (CW)
  5. //#define releK2 3                      // Port para o rele K2 (CCW)
  6.  
  7. #define motorStep 2                     // Port para o rele K1 (CW)
  8. #define motorDir 3                      // Port para o rele K2 (CCW)
  9. #define pinEnable 9
  10. // Criação do instante do motor
  11. AccelStepper motor(AccelStepper::DRIVER, motorStep, motorDir);
  12.  
  13. #define bot1 4                        // Port para o botao b1 liga ou desliga
  14. #define bot2 5                        // Port para o botao b2 temporario para o rele K1 (CW)
  15. #define bot3 6                        // Port para o botao b2 temporario para o rele K1 (CW)
  16. #define fc1  7                        // Port para o botao fim de curso (CW)
  17. #define fc2  8                        // Port para o botao fim de curso (CCW)
  18. #define ANALOG_IN A0
  19. int analogico = 0;
  20. int velocidade = 0;
  21. #define LIGA LOW
  22. #define DESLIGA HIGH
  23.  
  24. unsigned long tempoBot = 0;
  25. unsigned long Ovrfw = 1000;
  26. byte systemControl = 0b10010000;      // Inicia desligado e CW
  27. // bit 0 = CC/CCW  = 0b0000 000X;
  28. // bit 1 = bot2    = 0b0000 00X0;
  29. // bit 2 = bot3    = 0b0000 0X00;
  30. // bit 3 =         = 0b0000 X000;
  31. // bit 4 = FC1     = 0b000X 0000;
  32. // bit 5 = FC2     = 0b00X0 0000;
  33. // bit 6 =         = 0b0X00 0000;
  34. // bit 7 = Desligado   = 0bX000 0000;
  35. //-----------------------------------------------
  36. void setup()
  37. {
  38.   Serial.begin(115200);
  39.   //  digitalWrite(releK1, DESLIGA);          // Desliga o port evitando movimento indesejado
  40.   //  digitalWrite(releK2, DESLIGA);          // Desliga o port evitando movimento indesejado
  41.   //  pinMode( releK1, OUTPUT);               // Difinicao de port como saida
  42.   //  pinMode( releK2, OUTPUT);               // Difinicao de port como saida
  43.  
  44.   pinMode( motorStep, OUTPUT);              // Difinicao de port como saida
  45.   pinMode( motorDir, OUTPUT);               // Difinicao de port como saida
  46.   pinMode(pinEnable, OUTPUT);
  47.   pinMode( bot1, INPUT_PULLUP);             // Difinicao de ports como entrada
  48.   pinMode( bot2, INPUT_PULLUP);
  49.   pinMode( bot3, INPUT_PULLUP);
  50.   pinMode( fc1,  INPUT_PULLUP);
  51.   pinMode( fc2,  INPUT_PULLUP);
  52.   tempoBot = millis();
  53.   motor.setMaxSpeed(1000);
  54.   motor.setSpeed(50);
  55.   digitalWrite(pinEnable, LOW);
  56. }
  57. //-----------------------------------------------
  58. void loop()
  59. {
  60.   statusBot();
  61.   statusFC();
  62.   ligaSystem();
  63.   analogico = analogRead(ANALOG_IN);
  64.   //Serial.println(analogico);
  65.   velocidade = map(analogico, 0 , 255, 24, 160);
  66.   //Serial.println(velocidade);
  67. }
  68. //-----------------------------------------------
  69. void statusBot()
  70. {
  71.   // --------------------------------------------------------------- botao 1
  72.   if (digitalRead(bot1) == LOW)         // Se o bot1 estiver pressionado   botao b1 liga ou desliga
  73.   {
  74.     //    delay(10);                          // Tempo de debouncing
  75.     tempoBot = millis();
  76.     while (digitalRead(bot1) == LOW)       // Se o bot1 continua pressionado
  77.     {
  78.       if (millis() - tempoBot >= Ovrfw)    // Tempo para decidir ligado ou desligado
  79.       {
  80.         bitSet(systemControl, 7);       // Permite desligar o sistema (bit 7)
  81.         bitClear(systemControl, 0);     // Permite desligar o sistema (bit 0)
  82.         bitSet(systemControl, 4);       // Permite ligar o sistema CW (bit 4)
  83.         bitClear(systemControl, 5);     // Permite ligar o sistema CW (bit 5)
  84.         while (digitalRead(bot1) == LOW)
  85.         {}
  86.       }
  87.       else                              // Se o bot1 foi liberado antes de 3 segundos
  88.       {
  89.         bitSet(systemControl, 0);       // Permite ligar o sistema (bit 0)
  90.         bitClear(systemControl, 7);     // Permite ligar o sistema (bit 7)
  91.       }
  92.     }
  93.   }
  94.   // --------------------------------------------------------------- botao 2
  95.   if ((systemControl != 0x10) and  (systemControl != 0x11))
  96.   {
  97.     if (digitalRead(bot2) == LOW)           // Se o bot2 estiver pressionado
  98.     {
  99.       //    delay(10);                            // Tempo de debouncing
  100.       if (digitalRead(bot2) == LOW)         // Se o bot2 continua pressionado
  101.       {
  102.         if (digitalRead(fc1) == HIGH)        // Se o fc1 estiver liberado
  103.         {
  104.           bitSet(systemControl, 1);         // Permite pulsar o sistema CW (bit 1)
  105.         }
  106.         else                                // Se o bot3 estiver liberado
  107.         {
  108.           bitClear(systemControl, 1);       // Permite desligar o CW (bit 1)
  109.         }
  110.       }
  111.     }
  112.     else                                    // Se o bot3 estiver liberado
  113.     {
  114.       bitClear(systemControl, 1);           // Permite desligar o CW (bit 1)
  115.     }
  116.  
  117.     // --------------------------------------------------------------- botao 3
  118.     if (digitalRead(bot3) == LOW)           // Se o bot3 estiver pressionado
  119.     {
  120.       //    delay(10);                            // Tempo de debouncing
  121.       if (digitalRead(bot3) == LOW)         // Se o bot3 continua pressionado
  122.       {
  123.         if (digitalRead(fc2) == HIGH)        // Se o fc2 estiver liberado
  124.         {
  125.           bitSet(systemControl, 2);         // Permite pulsar o sistema CCW (bit 2)
  126.         }
  127.         else                                // Se o bot3 estiver liberado
  128.         {
  129.           bitClear(systemControl, 2);       // Permite desligar o CCW (bit 2)
  130.         }
  131.       }
  132.     }
  133.     else                                    // Se o bot3 estiver liberado
  134.     {
  135.       bitClear(systemControl, 2);           // Permite desligar o CCW (bit 2)
  136.     }
  137.   }
  138. }
  139. //----------------------------------------------
  140. void statusFC()
  141. {
  142.   // --------------------------------------------------------------- FC 1
  143.   if (digitalRead(fc1) == LOW)        // Se o fc1 estiver pressionado
  144.   {
  145.     //    delay(10);                        // Tempo de debouncing
  146.     if (digitalRead(fc1) == LOW)      // Se o fc1 continua pressionado
  147.     {
  148.       bitClear(systemControl, 0);     // Permite ligar o sistema CW (bit 0)
  149.     }
  150.   }
  151.   // --------------------------------------------------------------- FC 2
  152.   if (digitalRead(fc2) == LOW)        // Se o fc2 estiver pressionado
  153.   {
  154.     //    delay(10);                        // Tempo de debouncing
  155.     if (digitalRead(fc2) == LOW)      // Se o fc2 continua pressionado
  156.     {
  157.       bitSet(systemControl, 0);       // Permite ligar o sistema CCW (bit 0)
  158.     }
  159.   }
  160. }
  161. //----------------------------------------------
  162. void ligaSystem()
  163. {
  164.   //Serial.println(systemControl, HEX);
  165.   switch (systemControl)
  166.   {
  167.     case 0x10:                              //
  168.       //      digitalWrite(releK2, DESLIGA);        // Desliga K2 (evita curto circuito)
  169.       //      digitalWrite(releK1, LIGA);           // Liga K1 CW
  170.       motor.setSpeed(velocidade);
  171.       //      Serial.println(velocidade);
  172.       motor.runSpeed();
  173.       break;
  174.  
  175.     case 0x11:                              //
  176.       //      digitalWrite(releK1, DESLIGA);        // Desliga K1 (evita curto circuito)
  177.       //      digitalWrite(releK2, LIGA);           // Liga K2 CCW
  178.       motor.setSpeed(-velocidade);
  179.       //      Serial.println(velocidade);
  180.       motor.runSpeed();
  181.       break;
  182.  
  183.     case 0x90:                              //
  184.       //      digitalWrite(releK1, DESLIGA);        // Desliga K1
  185.       //      digitalWrite(releK2, DESLIGA);        // Desliga K2
  186.       motor.stop();
  187.       break;
  188.  
  189.     case 0x91:                              //
  190.       //      digitalWrite(releK1, DESLIGA);        // Desliga K1
  191.       //      digitalWrite(releK2, DESLIGA);        // Desliga K2
  192.       motor.stop();
  193.       break;
  194.  
  195.     case 0x92:                              //
  196.       //      digitalWrite(releK2, DESLIGA);        // Desliga K2 (evita curto circuito)
  197.       //      digitalWrite(releK1, LIGA);           // Liga K1 CW
  198.       motor.setSpeed(-velocidade);
  199.       motor.runSpeed();
  200.       break;
  201.  
  202.     case 0x93:                              //
  203.       //      digitalWrite(releK2, DESLIGA);        // Desliga K2 (evita curto circuito)
  204.       //      digitalWrite(releK1, LIGA);           // Liga K1 CW
  205.       motor.setSpeed(-velocidade);
  206.       motor.runSpeed();
  207.       break;
  208.  
  209.     case 0x94:                              //
  210.       //      digitalWrite(releK2, LIGA);           // Desliga K2 (evita curto circuito)
  211.       //      digitalWrite(releK1, DESLIGA);        // Liga K1 CW
  212.       motor.setSpeed(velocidade);
  213.       motor.runSpeed();
  214.       break;
  215.  
  216.     case 0x95:                              //
  217.       //      digitalWrite(releK2, LIGA);           // Desliga K2 (evita curto circuito)
  218.       //      digitalWrite(releK1, DESLIGA);        // Liga K1 CW
  219.       motor.setSpeed(velocidade);
  220.       motor.runSpeed();
  221.       break;
  222.   }
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement