View difference between Paste ID: yXy28KFV and T05spHHu
SHOW: | | - or go back to the newest paste.
1-
//version ovs17 с фонариком на красную и SOS на зелёную кнопку
1+
//version ovs18 с фонариком на красную и 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 longDelay 600
10+
11-
#define shortDelay 300
11+
12-
#define blinkOffDelay 200
12+
13-
#define wordsSpaceDelay 300
13+
#define MorsePointDuration 300
14-
#define betweenSosDelay 1000
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 normalMode();
41
}
42
43
void normalMode()
44
{
45
    lightsOff();
46
    for(;;)
47
    {
48
        checkButtons();
49
        changeDutyCycles();
50-
        delay(200);
50+
        delay(checkButtonDelay);
51
    }
52
}
53
54
void sos()
55
{
56
    lightsOff();
57
    for(;;)
58
    {
59-
        blink(shortDelay);
59+
        blink(MorsePointDuration);
60-
		delay(wordsSpaceDelay);
60+
		delay(3 * MorsePointDuration);
61
62-
        blink(longDelay);
62+
        blink(3 * MorsePointDuration);
63-
		delay(wordsSpaceDelay);
63+
		delay(3 * MorsePointDuration);
64
65-
        blink(shortDelay);
65+
        blink(MorsePointDuration);
66-
        delay(betweenSosDelay);
66+
        delay(7 * MorsePointDuration);
67
    }
68
}
69
70
void blink(int length)
71
{
72
    for(int i = 0; i < 3; i++)
73
        {
74-
            delay(blinkOffDelay);
74+
            delay(MorsePointDuration);
75
            lightsOn();
76
            delay(length);
77
            lightsOff();
78
        }
79
}
80
81
void lightsOn()
82
{
83
    analogWrite(outputPinRed, 0);
84
    analogWrite(outputPinBlue, 0);
85
    analogWrite(outputPinGreen, 0);
86
}
87
88
void lightsOff()
89
{
90
    analogWrite(outputPinRed, 255);
91
    analogWrite(outputPinBlue, 255);
92
    analogWrite(outputPinGreen, 255);
93
}
94
95
void lantern()
96
{
97
    for(;;);
98
}
99
100
void loop()
101
{
102
}
103
104
void checkButtons()
105
{
106
    checkButton(&buttonPressedRed, inputPinRed, &upDutyCycleRed);
107
    checkButton(&buttonPressedBlue, inputPinBlue, &upDutyCycleBlue);
108
    checkButton(&buttonPressedGreen, inputPinGreen, &upDutyCycleGreen);
109
}
110
111
void checkButton(int* buttonPressed, int inputPin, int* upDutyCycle)
112
{
113
    int buttonPressedBefore = *buttonPressed;
114
    *buttonPressed = digitalRead(inputPin) == LOW;
115
    if(!buttonPressedBefore && *buttonPressed)
116
        *upDutyCycle = !*upDutyCycle;
117
}
118
119
void changeDutyCycles()
120
{
121
    if (buttonPressedRed) changeDutyCycle(&dutyCycleRed, &upDutyCycleRed, outputPinRed);
122
    if (buttonPressedBlue) changeDutyCycle(&dutyCycleBlue, &upDutyCycleBlue, outputPinBlue);
123
    if (buttonPressedGreen) changeDutyCycle(&dutyCycleGreen, &upDutyCycleGreen, outputPinGreen);
124
}
125
126
void changeDutyCycle(int* dutyCycle, int* upDutyCycle, int outputPin)
127
{
128-
    *dutyCycle += *upDutyCycle ? 4 : -4;
128+
    *dutyCycle += *upDutyCycle ? dutyCycleSpeed : -dutyCycleSpeed;
129
    if (*dutyCycle > 255) *dutyCycle = 255;
130
    if (*dutyCycle < 0) *dutyCycle = 0;
131
    analogWrite(outputPin, 255-*dutyCycle);
132-
}
132+
133
134
135
//#define longDelay 600
136
//#define shortDelay 300
137
//#define blinkOffDelay 200
138
//#define wordsSpaceDelay 300
139
//#define betweenSosDelay 1000