SHOW:
|
|
- or go back to the newest paste.
| 1 | - | //version ovs18 с фонариком на красную и SOS на зелёную кнопку |
| 1 | + | //version ovs19 с фонариком на красную кнопку, SOS на зелёную и бесконечное мигание на синюю |
| 2 | #define checkButtonDelay 200 | |
| 3 | #define dutyCycleSpeed 4 | |
| 4 | ||
| 5 | #define inputPinRed 2 | |
| 6 | #define inputPinGreen 3 | |
| 7 | #define inputPinBlue 4 | |
| 8 | ||
| 9 | #define outputPinRed 9 | |
| 10 | #define outputPinGreen 10 | |
| 11 | #define outputPinBlue 11 | |
| 12 | ||
| 13 | #define MorsePointDuration 300 | |
| 14 | ||
| 15 | ||
| 16 | int dutyCycleRed = 0; | |
| 17 | int dutyCycleGreen = 0; | |
| 18 | int dutyCycleBlue = 0; | |
| 19 | ||
| 20 | int upDutyCycleRed = 0; | |
| 21 | int upDutyCycleGreen = 0; | |
| 22 | int upDutyCycleBlue = 0; | |
| 23 | ||
| 24 | int buttonPressedRed = 0; | |
| 25 | int buttonPressedGreen = 0; | |
| 26 | int buttonPressedBlue = 0; | |
| 27 | ||
| 28 | void setup() | |
| 29 | {
| |
| 30 | pinMode(inputPinRed, INPUT); | |
| 31 | pinMode(inputPinGreen, INPUT); | |
| 32 | pinMode(inputPinBlue, INPUT); | |
| 33 | ||
| 34 | pinMode(outputPinRed, OUTPUT); | |
| 35 | pinMode(outputPinGreen, OUTPUT); | |
| 36 | pinMode(outputPinBlue, OUTPUT); | |
| 37 | ||
| 38 | if (digitalRead(inputPinRed) == LOW) lantern(); | |
| 39 | else if (digitalRead(inputPinGreen) == LOW) sos(); | |
| 40 | else if (digitalRead(inputPinBlue) == LOW) blinkForever(); | |
| 41 | else normalMode(); | |
| 42 | } | |
| 43 | ||
| 44 | void blinkForever() | |
| 45 | {
| |
| 46 | for(;;) | |
| 47 | {
| |
| 48 | delay(MorsePointDuration); | |
| 49 | lightsOff(); | |
| 50 | delay(MorsePointDuration); | |
| 51 | lightsOn(); | |
| 52 | } | |
| 53 | } | |
| 54 | ||
| 55 | void normalMode() | |
| 56 | {
| |
| 57 | lightsOff(); | |
| 58 | for(;;) | |
| 59 | {
| |
| 60 | checkButtons(); | |
| 61 | changeDutyCycles(); | |
| 62 | delay(checkButtonDelay); | |
| 63 | } | |
| 64 | } | |
| 65 | ||
| 66 | void sos() | |
| 67 | {
| |
| 68 | lightsOff(); | |
| 69 | for(;;) | |
| 70 | {
| |
| 71 | blink(MorsePointDuration); | |
| 72 | delay(3 * MorsePointDuration); | |
| 73 | ||
| 74 | blink(3 * MorsePointDuration); | |
| 75 | delay(3 * MorsePointDuration); | |
| 76 | ||
| 77 | blink(MorsePointDuration); | |
| 78 | delay(7 * MorsePointDuration); | |
| 79 | } | |
| 80 | } | |
| 81 | ||
| 82 | void blink(int length) | |
| 83 | {
| |
| 84 | for(int i = 0; i < 3; i++) | |
| 85 | {
| |
| 86 | delay(MorsePointDuration); | |
| 87 | lightsOn(); | |
| 88 | delay(length); | |
| 89 | lightsOff(); | |
| 90 | } | |
| 91 | } | |
| 92 | ||
| 93 | void lightsOn() | |
| 94 | {
| |
| 95 | analogWrite(outputPinRed, 0); | |
| 96 | analogWrite(outputPinBlue, 0); | |
| 97 | analogWrite(outputPinGreen, 0); | |
| 98 | } | |
| 99 | ||
| 100 | void lightsOff() | |
| 101 | {
| |
| 102 | analogWrite(outputPinRed, 255); | |
| 103 | analogWrite(outputPinBlue, 255); | |
| 104 | analogWrite(outputPinGreen, 255); | |
| 105 | } | |
| 106 | ||
| 107 | void lantern() | |
| 108 | {
| |
| 109 | for(;;); | |
| 110 | } | |
| 111 | ||
| 112 | void loop() | |
| 113 | {
| |
| 114 | } | |
| 115 | ||
| 116 | void checkButtons() | |
| 117 | {
| |
| 118 | checkButton(&buttonPressedRed, inputPinRed, &upDutyCycleRed); | |
| 119 | checkButton(&buttonPressedBlue, inputPinBlue, &upDutyCycleBlue); | |
| 120 | checkButton(&buttonPressedGreen, inputPinGreen, &upDutyCycleGreen); | |
| 121 | } | |
| 122 | ||
| 123 | void checkButton(int* buttonPressed, int inputPin, int* upDutyCycle) | |
| 124 | {
| |
| 125 | int buttonPressedBefore = *buttonPressed; | |
| 126 | *buttonPressed = digitalRead(inputPin) == LOW; | |
| 127 | if(!buttonPressedBefore && *buttonPressed) | |
| 128 | *upDutyCycle = !*upDutyCycle; | |
| 129 | } | |
| 130 | ||
| 131 | void changeDutyCycles() | |
| 132 | {
| |
| 133 | if (buttonPressedRed) changeDutyCycle(&dutyCycleRed, &upDutyCycleRed, outputPinRed); | |
| 134 | if (buttonPressedBlue) changeDutyCycle(&dutyCycleBlue, &upDutyCycleBlue, outputPinBlue); | |
| 135 | if (buttonPressedGreen) changeDutyCycle(&dutyCycleGreen, &upDutyCycleGreen, outputPinGreen); | |
| 136 | } | |
| 137 | ||
| 138 | void changeDutyCycle(int* dutyCycle, int* upDutyCycle, int outputPin) | |
| 139 | {
| |
| 140 | *dutyCycle += *upDutyCycle ? dutyCycleSpeed : -dutyCycleSpeed; | |
| 141 | if (*dutyCycle > 255) *dutyCycle = 255; | |
| 142 | if (*dutyCycle < 0) *dutyCycle = 0; | |
| 143 | analogWrite(outputPin, 255-*dutyCycle); | |
| 144 | } | |
| 145 | ||
| 146 | ||
| 147 | //#define longDelay 600 | |
| 148 | //#define shortDelay 300 | |
| 149 | //#define blinkOffDelay 200 | |
| 150 | //#define wordsSpaceDelay 300 | |
| 151 | //#define betweenSosDelay 1000 |