Advertisement
esquilo10

Untitled

Jul 30th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.31 KB | None | 0 0
  1. #include <Servo.h>
  2.  
  3. /*//armário 1
  4. //servo 1
  5. const int pinoServo1 = 5; //PINO DIGITAL UTILIZADO PELO SERVO1
  6. Servo s1; //OBJETO DO TIPO SERVO1
  7. String comando1 = "1";//string que comanda os armários
  8. //botão 1
  9. const int bt1 = 4;//botão 1
  10. int bt1State = 0;//variável que lê estado do botão 1*/
  11.  
  12.  
  13. //armário 2
  14. //servo 2
  15. const int pinoServo2 = 6; //PINO DIGITAL UTILIZADO PELO SERVO1
  16. Servo s2; //OBJETO DO TIPO SERVO1
  17. String comando2 = "2";//string que comanda os armários
  18. //botão 2
  19. const int bt2 = 7;//botão 1
  20. int bt2State = 0;//variável que lê estado do botão 1
  21.  
  22. bool flag = 0;//evita que você abra um armário em quanto outro estiver aberto tem que ser 0 para abrir e 1 para fechar
  23.  
  24.  
  25. void setup ()
  26. {
  27.   delay(5000);
  28.   Serial.begin(115200);//inicia a comunicação serial
  29.   Serial.println();//dá um espaço no serial monitor
  30.  
  31.   /*//armário 1 setup
  32.   s1.attach(pinoServo1); //ASSOCIAÇÃO DO PINO DIGITAL AO OBJETO DO TIPO SERVO
  33.   s1.write(90); //INICIA O MOTOR NA POSIÇÃO de abertura
  34.   pinMode(bt1, INPUT);*/
  35.  
  36.   //armário 2 setup
  37.   s2.attach(pinoServo2); //ASSOCIAÇÃO DO PINO DIGITAL AO OBJETO DO TIPO SERVO
  38.   s2.write(90); //INICIA O MOTOR NA POSIÇÃO de fechamento
  39.   pinMode(bt2, INPUT);//define botão como input
  40. }
  41.  
  42. void loop()
  43. {
  44.   //verifica armário 1
  45.   //flag = abre(comando1, flag, s1);
  46.  // flag = fecha(bt1State, flag, s1, bt1);
  47.   //verifica armário 2
  48.   flag = abre(comando2, flag, s2);//abre armários voltados para a direita
  49.   flag = fecha(bt2State, flag, s2, bt2);
  50. }
  51.  
  52. bool abre(String comandox, bool flag, Servo s)
  53. {
  54.   String comando = "";
  55.   if(Serial.available())
  56.   {
  57.     comando = Serial.readStringUntil('\n'); //lê serial monitor até o fim de linha
  58.   }
  59.   if(comando == comandox && flag == 0) //se a string digitada no serial monitor for igual a string de abertura de um armário x ele abre o armário
  60.   {
  61.       s.write(180);//coloca servo na posição 180 para abrir o armário
  62.       flag = 1;//flag vai para 1, após ser aberto
  63.    }
  64.  
  65.   return flag;
  66. }
  67.  
  68. bool fecha(int btState, bool flag, Servo s, const int bt)
  69. {
  70.   btState = digitalRead(bt);//lê botão
  71.   if(btState == HIGH && flag == 1) //fecha
  72.   {
  73.     s.write(90);//coloca o servo na posição 90 graus para fechar o armário
  74.     flag = 0;//flag vai para 0 após ser fechado
  75.   }
  76.   return flag;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement