Advertisement
desentcare

Untitled

Dec 16th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.59 KB | None | 0 0
  1. //version ovs14 с фонариком на красную и 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.     lightsOff();
  34.     if (digitalRead(inputPinBlue) == LOW) sos();
  35.  
  36.    
  37. }
  38.  
  39. void sos()
  40. {
  41.     for(;;)
  42.     {
  43.         blink(300);
  44.         blink(800);
  45.         blink(300);
  46.         delay(1000);
  47.     }
  48. }
  49.  
  50. void blink(int length)
  51. {
  52.     for(int i = 0; i < 3; i++)
  53.         {
  54.             delay(300);
  55.             lightsOn();
  56.             delay(length);
  57.             lightsOff();
  58.         }
  59. }
  60.  
  61. void lightsOn()
  62. {
  63.     analogWrite(outputPinRed, 0);
  64.     analogWrite(outputPinBlue, 0);
  65.     analogWrite(outputPinGreen, 0);
  66. }
  67.  
  68. void lightsOff()
  69. {
  70.     analogWrite(outputPinRed, 255);
  71.     analogWrite(outputPinBlue, 255);
  72.     analogWrite(outputPinGreen, 255);
  73. }
  74.  
  75. void lantern()
  76. {
  77.     for(;;);
  78. }
  79.  
  80. void loop()
  81. {
  82.     checkButtons();
  83.     changeDutyCycles();
  84.     delay(200);
  85. }
  86.  
  87. void checkButtons()
  88. {
  89.     checkButton(&buttonPressedRed, &inputPinRed, &upDutyCycleRed);
  90.     checkButton(&buttonPressedBlue, &inputPinBlue, &upDutyCycleBlue);
  91.     checkButton(&buttonPressedGreen, &inputPinGreen, &upDutyCycleGreen);
  92. }
  93.  
  94. void checkButton(int* buttonPressed, int* inputPin, int* upDutyCycle)
  95. {
  96.     int buttonPressedBefore = *buttonPressed;
  97.     *buttonPressed = digitalRead(*inputPin) == LOW;
  98.     if(!buttonPressedBefore && *buttonPressed)
  99.         *upDutyCycle = !*upDutyCycle;
  100. }
  101.  
  102. void changeDutyCycles()
  103. {
  104.     if (buttonPressedRed) changeDutyCycle(&dutyCycleRed, &upDutyCycleRed, &outputPinRed);
  105.     if (buttonPressedBlue) changeDutyCycle(&dutyCycleBlue, &upDutyCycleBlue, &outputPinBlue);
  106.     if (buttonPressedGreen) changeDutyCycle(&dutyCycleGreen, &upDutyCycleGreen, &outputPinGreen);
  107. }
  108.  
  109. void changeDutyCycle(int* dutyCycle, int* upDutyCycle, int* outputPin)
  110. {
  111.     *dutyCycle += *upDutyCycle ? 4 : -4;
  112.     if (*dutyCycle > 255) *dutyCycle = 255;
  113.     if (*dutyCycle < 0) *dutyCycle = 0;
  114.     analogWrite(*outputPin, 255-*dutyCycle);
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement