-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathMotorEncoders.ino
64 lines (53 loc) · 1.28 KB
/
MotorEncoders.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <Wire.h>
#include <Zumo32U4.h>
Zumo32U4LCD lcd;
Zumo32U4Motors motors;
Zumo32U4ButtonA buttonA;
Zumo32U4Encoders encoders;
const uint16_t motorSpeed = 250;
void setup() {
lcd.clear();
}
void forward(long count) {
encoders.getCountsAndResetLeft();
encoders.getCountsAndResetRight();
long countsLeft = 0;
long countsRight = 0;
motors.setSpeeds(motorSpeed, motorSpeed);
while(countsLeft < count) {
countsLeft += encoders.getCountsAndResetLeft();
countsRight += encoders.getCountsAndResetRight();
lcd.gotoXY(0, 1);
lcd.print(countsLeft);
lcd.print(" ");
delay(2);
};
motors.setSpeeds(0, 0);
}
void reverse(long count) {
encoders.getCountsAndResetLeft();
encoders.getCountsAndResetRight();
long countsLeft = 0;
long countsRight = 0;
motors.setSpeeds(-motorSpeed, -motorSpeed);
while(countsLeft < count) {
countsLeft -= encoders.getCountsAndResetLeft();
countsRight -= encoders.getCountsAndResetRight();
lcd.gotoXY(0, 1);
lcd.print(countsLeft);
lcd.print(" ");
delay(2);
};
motors.setSpeeds(0, 0);
}
void loop() {
// Wait for a button press
bool buttonPress = buttonA.getSingleDebouncedPress();
if (buttonPress) {
// Wait
delay(1000);
forward(5000);
delay(1000);
reverse(5000);
}
}