Advertisement
desentcare

Untitled

Dec 16th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.69 KB | None | 0 0
  1. //version ovs15 с фонариком на красную и SOS на синюю кнопку
  2. int inputPinRed = 2;
  3. int inputPinGreen = 3;
  4. int inputPinBlue = 4;
  5.  
  6. int outputPinRed = 9;
  7. int outputPinGreen = 10;
  8. int outputPinBlue = 11;
  9.  
  10. int dutyCycleRed = 0;
  11. int dutyCycleGreen = 0;
  12. int dutyCycleBlue = 0;
  13.  
  14. int upDutyCycleRed = 0;
  15. int upDutyCycleGreen = 0;
  16. int upDutyCycleBlue = 0;
  17.  
  18. int buttonPressedRed = 0;
  19. int buttonPressedGreen = 0;
  20. int buttonPressedBlue = 0;
  21.  
  22. void setup()
  23. {
  24.     pinMode(inputPinRed, INPUT);
  25.     pinMode(inputPinGreen, INPUT);
  26.     pinMode(inputPinBlue, INPUT);
  27.  
  28.     pinMode(outputPinRed, OUTPUT);
  29.     pinMode(outputPinGreen, OUTPUT);
  30.     pinMode(outputPinBlue, OUTPUT);
  31.  
  32.     if (digitalRead(inputPinRed) == LOW) lantern();
  33.     else if (digitalRead(inputPinBlue) == LOW) sos();
  34.     else normalMode();
  35. }
  36.  
  37. void normalMode()
  38. {
  39.     lightsOff();
  40.     for(;;)
  41.     {
  42.         checkButtons();
  43.         changeDutyCycles();
  44.         delay(200);
  45.     }
  46. }
  47.  
  48. void sos()
  49. {
  50.     lightsOff();
  51.     for(;;)
  52.     {
  53.         blink(300);
  54.         blink(800);
  55.         blink(300);
  56.         delay(1000);
  57.     }
  58. }
  59.  
  60. void blink(int length)
  61. {
  62.     for(int i = 0; i < 3; i++)
  63.         {
  64.             delay(300);
  65.             lightsOn();
  66.             delay(length);
  67.             lightsOff();
  68.         }
  69. }
  70.  
  71. void lightsOn()
  72. {
  73.     analogWrite(outputPinRed, 0);
  74.     analogWrite(outputPinBlue, 0);
  75.     analogWrite(outputPinGreen, 0);
  76. }
  77.  
  78. void lightsOff()
  79. {
  80.     analogWrite(outputPinRed, 255);
  81.     analogWrite(outputPinBlue, 255);
  82.     analogWrite(outputPinGreen, 255);
  83. }
  84.  
  85. void lantern()
  86. {
  87.     for(;;);
  88. }
  89.  
  90. void loop()
  91. {
  92. }
  93.  
  94. void checkButtons()
  95. {
  96.     checkButton(&buttonPressedRed, &inputPinRed, &upDutyCycleRed);
  97.     checkButton(&buttonPressedBlue, &inputPinBlue, &upDutyCycleBlue);
  98.     checkButton(&buttonPressedGreen, &inputPinGreen, &upDutyCycleGreen);
  99. }
  100.  
  101. void checkButton(int* buttonPressed, int* inputPin, int* upDutyCycle)
  102. {
  103.     int buttonPressedBefore = *buttonPressed;
  104.     *buttonPressed = digitalRead(*inputPin) == LOW;
  105.     if(!buttonPressedBefore && *buttonPressed)
  106.         *upDutyCycle = !*upDutyCycle;
  107. }
  108.  
  109. void changeDutyCycles()
  110. {
  111.     if (buttonPressedRed) changeDutyCycle(&dutyCycleRed, &upDutyCycleRed, &outputPinRed);
  112.     if (buttonPressedBlue) changeDutyCycle(&dutyCycleBlue, &upDutyCycleBlue, &outputPinBlue);
  113.     if (buttonPressedGreen) changeDutyCycle(&dutyCycleGreen, &upDutyCycleGreen, &outputPinGreen);
  114. }
  115.  
  116. void changeDutyCycle(int* dutyCycle, int* upDutyCycle, int* outputPin)
  117. {
  118.     *dutyCycle += *upDutyCycle ? 4 : -4;
  119.     if (*dutyCycle > 255) *dutyCycle = 255;
  120.     if (*dutyCycle < 0) *dutyCycle = 0;
  121.     analogWrite(*outputPin, 255-*dutyCycle);
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement