File tree 1 file changed +51
-15
lines changed 1 file changed +51
-15
lines changed Original file line number Diff line number Diff line change 1
1
#include < Servo.h>
2
2
3
- #define START 0
4
- #define STOP 180
3
+ #define START_ANGLE 45
4
+ #define STOP_ANGLE 150
5
+ #define LOOP_DELAY 250
6
+ #define SRV_PIN 9
7
+ #define SPD_PIN A0
5
8
6
- #define STEP 5
7
- #define DELAY 100
9
+ const int startAngle = START_ANGLE;
10
+ const int stopAngle = STOP_ANGLE;
11
+ const int loopDelay = LOOP_DELAY;
12
+
13
+ const int srvPin = SRV_PIN;
14
+ const int spdPin = SPD_PIN;
15
+
16
+ const int stepAngle = 10 ;
8
17
9
18
Servo srv;
10
19
11
20
void setup () {
12
- srv.attach (9 );
13
- srv.write (START);
21
+ Serial.begin (9600 );
22
+
23
+ srv.attach (srvPin);
24
+ srv.write (startAngle);
25
+ }
26
+
27
+ int getSpd () {
28
+ int a = analogRead (spdPin);
29
+
30
+ if (a < 24 ) {
31
+ return 0 ;
32
+ }
33
+
34
+ return ((a - 24 ) / 100 );
14
35
}
15
36
16
- int pos = START;
37
+ int getStepDelay (int spd) {
38
+ return 100 - 10 *spd;
39
+ }
40
+
41
+
42
+ int pos = startAngle;
43
+ int spd = 0 ;
17
44
bool fw = true ;
18
45
46
+
19
47
void loop () {
48
+ spd = getSpd ();
49
+
50
+ if (spd == 0 ) {
51
+ srv.write (startAngle);
52
+ delay (loopDelay);
53
+ return ;
54
+ }
55
+
20
56
if (fw) {
21
- pos += STEP ;
57
+ pos += stepAngle ;
22
58
} else {
23
- pos-= STEP ;
59
+ pos -= stepAngle ;
24
60
}
25
61
26
62
srv.write (pos);
27
63
28
- if (pos >= STOP ) {
29
- delay (200 );
64
+ if (pos >= stopAngle ) {
65
+ delay (loopDelay );
30
66
fw = false ;
31
67
} else {
32
- if (pos <= START ) {
33
- delay (200 );
68
+ if (pos <= startAngle ) {
69
+ delay (loopDelay );
34
70
fw = true ;
35
71
}
36
72
}
37
-
38
- delay (DELAY );
73
+
74
+ delay (getStepDelay (spd) );
39
75
}
You can’t perform that action at this time.
0 commit comments