Advertisement
Guest User

Piscina_Sketch

a guest
Apr 8th, 2020
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.34 KB | None | 0 0
  1. #include<UnicViewAD.h>
  2. #include<Servo.h>
  3.  
  4. LCM Lcm(Serial);
  5. Servo ServoMotor;
  6.  
  7. /***********************************************************************/
  8. /***********************************************************************/
  9.  
  10. const int ledPinRed = 2;
  11. const int ledPinGreen = 3;
  12. const int ledPinBlue = 4;
  13.  
  14. const int ledOn = 1;
  15. const int ledOff = 0;
  16.  
  17. const int ledsOff[] = {LOW, LOW, LOW};
  18. const int ledRedOnly[] = {HIGH, LOW, LOW};
  19. const int ledGreenOnly[] = {LOW, HIGH, LOW};
  20. const int ledBlueOnly[] = {LOW, LOW, HIGH};
  21. const int ledPurpleOnly[] = {HIGH, HIGH, LOW};
  22. const int ledCyanOnly[] = {LOW, HIGH, HIGH};
  23. const int ledYellowOnly[] = {HIGH, HIGH, LOW};
  24.  
  25. const int colorsDisplayOff[] = {0, 0, 0, 0, 0, 0};
  26. const int colorsDisplayRed[] = {1, 0, 0, 0, 0, 0};
  27. const int colorsDisplayGreen[] = {0, 1, 0, 0, 0, 0};
  28. const int colorsDisplayBlue[] = {0, 0, 1, 0, 0, 0};
  29. const int colorsDisplayPurple[] = {1, 0, 1, 1, 0, 0};
  30. const int colorsDisplayCyan[] = {0, 1, 1, 0, 1, 0};
  31. const int colorsDisplayYellow[] = {1, 1, 0, 0, 0, 1};
  32.  
  33. LcmVar ledButtonRed(15);
  34. LcmVar ledButtonGreen(16);
  35. LcmVar ledButtonBlue(17);
  36. LcmVar ledButtonPurple(18);
  37. LcmVar ledButtonCyan(19);
  38. LcmVar ledButtonYellow(20);
  39.  
  40. /***********************************************************************/
  41.  
  42. const int servoPin = 9;
  43.  
  44. int positionCurrent = 0;
  45. int tempB = 0;
  46.  
  47. bool updatePosition = true;
  48.  
  49. long int nextPosition = 0;
  50. const int nextWriteIntervalInMs = 20;
  51.  
  52. LcmVar temperatureButton(1000);
  53. LcmVar temperatura(1001);
  54.  
  55. void setup()
  56. {
  57.   Lcm.begin(115200);
  58.   pinMode(ledPinRed, OUTPUT);
  59.   pinMode(ledPinGreen, OUTPUT);
  60.   pinMode(ledPinBlue, OUTPUT);
  61.  
  62.   ServoMotor.attach(servoPin);
  63.   ServoMotor.write(20);
  64.   pinMode(servoPin, OUTPUT);
  65. }
  66.  
  67. void loop()
  68. {
  69. //MOTOR
  70.  
  71.  
  72.  
  73.   if(temperatureButton.available())
  74.   {
  75.     tempB = temperatureButton.getData();
  76.     updatePosition = true;    
  77.   }
  78.  
  79.  if (updatePosition == true)                    
  80.  {
  81.     if (millis() > nextPosition)        
  82.     {
  83.       if (tempB > positionCurrent)    
  84.       {
  85.         positionCurrent++;                      
  86.       }
  87.       else if (tempB < positionCurrent)
  88.       {
  89.         positionCurrent--;                        
  90.       }
  91.  
  92.       ServoMotor.write(positionCurrent);
  93.       temperatura.write(positionCurrent);                              
  94.       nextPosition = millis() + nextWriteIntervalInMs;
  95.     }
  96.   }
  97.  
  98.   if(tempB == positionCurrent)
  99.   {
  100.     updatePosition = false;
  101.   }
  102.  
  103.  
  104.  
  105. /************************************************************************/
  106. //LEDS
  107.  
  108.          if(ledButtonRed.available())
  109.          {
  110.           int led_estadoRed = ledButtonRed.getData();
  111.           if(led_estadoRed == ledOn)
  112.           {
  113.             Update(ledRedOnly, colorsDisplayRed);
  114.           }
  115.        
  116.           if(led_estadoRed == ledOff)
  117.           {
  118.             Update(ledsOff, colorsDisplayOff);
  119.           }
  120.          }
  121.        
  122.          if(ledButtonGreen.available())
  123.          {
  124.           int led_estadoGreen = ledButtonGreen.getData();
  125.           if(led_estadoGreen == ledOn)
  126.           {
  127.             Update(ledGreenOnly, colorsDisplayGreen);
  128.           }
  129.        
  130.           if(led_estadoGreen == ledOff)
  131.           {
  132.             Update(ledsOff, colorsDisplayOff);
  133.           }
  134.          }
  135.        
  136.           if(ledButtonBlue.available())
  137.          {
  138.           int led_estadoBlue = ledButtonBlue.getData();
  139.           if(led_estadoBlue == ledOn)
  140.           {
  141.             Update(ledBlueOnly, colorsDisplayBlue);
  142.           }
  143.        
  144.           if(led_estadoBlue == ledOff)
  145.           {
  146.             Update(ledsOff, colorsDisplayOff);
  147.           }
  148.          }
  149.  
  150.          if(ledButtonPurple.available())
  151.          {
  152.           int led_estadoPurple = ledButtonPurple.getData();
  153.           if(led_estadoPurple == ledOn)
  154.           {
  155.             Update(ledPurpleOnly, colorsDisplayPurple);
  156.           }
  157.          
  158.           if(led_estadoPurple == ledOff)
  159.           {
  160.             Update(ledsOff, colorsDisplayOff);
  161.           }
  162.          }
  163.          
  164.          if(ledButtonCyan.available())
  165.          {
  166.           int led_estadoCyan = ledButtonCyan.getData();
  167.           if(led_estadoCyan == ledOn)
  168.           {
  169.             Update(ledCyanOnly, colorsDisplayCyan);
  170.           }
  171.          
  172.           if(led_estadoCyan == ledOff)
  173.           {
  174.             Update(ledsOff, colorsDisplayOff);
  175.           }
  176.          }
  177.  
  178.          if(ledButtonYellow.available())
  179.          {
  180.           int led_estadoYellow = ledButtonYellow.getData();
  181.           if(led_estadoYellow == ledOn)
  182.           {
  183.             Update(ledYellowOnly, colorsDisplayYellow);
  184.           }
  185.          
  186.           if(led_estadoYellow == ledOff)
  187.           {
  188.             Update(ledsOff, colorsDisplayOff);
  189.           }
  190.          }
  191. }
  192.  
  193. void Update(int ledState[], int colorsDisplay[])
  194. {
  195.   EstadoLed(ledState);
  196.   AtualizarDisplay(colorsDisplay);
  197. }
  198.  
  199.  
  200. void EstadoLed(int ledState[])
  201. {
  202.     digitalWrite(ledPinRed, ledState[0]);
  203.     digitalWrite(ledPinGreen, ledState[1]);
  204.     digitalWrite(ledPinBlue, ledState[2]);
  205. }
  206.  
  207. void AtualizarDisplay(int colorsDisplay[])
  208. {
  209.     ledButtonRed.write(colorsDisplay[0]);
  210.     ledButtonGreen.write(colorsDisplay[1]);
  211.     ledButtonBlue.write(colorsDisplay[2]);
  212.     ledButtonPurple.write(colorsDisplay[3]);
  213.     ledButtonCyan.write(colorsDisplay[4]);
  214.     ledButtonYellow.write(colorsDisplay[5]);
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement