-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathTurnExample.ino
115 lines (92 loc) · 2.06 KB
/
TurnExample.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <Wire.h>
#include <Zumo32U4.h>
#include "TurnSensor.h"
Zumo32U4LCD lcd;
L3G gyro;
Zumo32U4Motors motors;
Zumo32U4ButtonA buttonA;
Zumo32U4ButtonB buttonB;
Zumo32U4ButtonC buttonC;
int turnSpeed = 150;
int motorSpeed = 250;
void setup() {
turnSensorSetup();
delay(500);
turnSensorReset();
lcd.clear();
}
// Turn left
void turnLeft(int degrees) {
turnSensorReset();
motors.setSpeeds(-turnSpeed, turnSpeed);
int angle = 0;
do {
delay(1);
turnSensorUpdate();
angle = (((int32_t)turnAngle >> 16) * 360) >> 16;
lcd.gotoXY(0, 0);
lcd.print(angle);
lcd.print(" ");
} while (angle < degrees);
motors.setSpeeds(0, 0);
}
// Turn right
void turnRight(int degrees) {
turnSensorReset();
motors.setSpeeds(turnSpeed, -turnSpeed);
int angle = 0;
do {
delay(1);
turnSensorUpdate();
angle = (((int32_t)turnAngle >> 16) * 360) >> 16;
lcd.gotoXY(0, 0);
lcd.print(angle);
lcd.print(" ");
} while (angle > -degrees);
motors.setSpeeds(0, 0);
}
void loop() {
turnSensorUpdate();
int angle = (((int32_t)turnAngle >> 16) * 360) >> 16;
lcd.gotoXY(0, 0);
lcd.print(angle);
lcd.print(" ");
// If we press A, turn left.
bool buttonPress = buttonA.getSingleDebouncedPress();
if(buttonPress) {
delay(500);
turnLeft(90);
}
// If we press B, turn right
buttonPress = buttonB.getSingleDebouncedPress();
if(buttonPress) {
delay(500);
turnRight(90);
}
// If we press C, dance a jig
buttonPress = buttonC.getSingleDebouncedPress();
if(buttonPress) {
delay(500);
motors.setSpeeds(motorSpeed, motorSpeed);
delay(1000);
motors.setSpeeds(0, 0);
delay(500);
turnRight(90);
delay(500);
motors.setSpeeds(motorSpeed, motorSpeed);
delay(1000);
turnLeft(90);
delay(500);
motors.setSpeeds(motorSpeed, motorSpeed);
delay(1000);
turnLeft(90);
delay(500);
motors.setSpeeds(motorSpeed, motorSpeed);
delay(1000);
turnLeft(90);
delay(500);
motors.setSpeeds(motorSpeed, motorSpeed);
delay(2000);
turnRight(180);
}
}