Skip to content

Commit 90ef20f

Browse files
committed
feat(rocking-servo): Add potentiometre support
1 parent 13fec55 commit 90ef20f

File tree

1 file changed

+51
-15
lines changed

1 file changed

+51
-15
lines changed

rocking-servo/rocking-servo.ino

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,75 @@
11
#include <Servo.h>
22

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
58

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;
817

918
Servo srv;
1019

1120
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);
1435
}
1536

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;
1744
bool fw = true;
1845

46+
1947
void loop() {
48+
spd = getSpd();
49+
50+
if (spd == 0) {
51+
srv.write(startAngle);
52+
delay(loopDelay);
53+
return;
54+
}
55+
2056
if (fw) {
21-
pos += STEP;
57+
pos += stepAngle;
2258
} else {
23-
pos-= STEP;
59+
pos -= stepAngle;
2460
}
2561

2662
srv.write(pos);
2763

28-
if (pos >= STOP) {
29-
delay(200);
64+
if (pos >= stopAngle) {
65+
delay(loopDelay);
3066
fw = false;
3167
} else {
32-
if (pos <= START) {
33-
delay(200);
68+
if (pos <= startAngle) {
69+
delay(loopDelay);
3470
fw = true;
3571
}
3672
}
37-
38-
delay(DELAY);
73+
74+
delay(getStepDelay(spd));
3975
}

0 commit comments

Comments
 (0)