-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial.ino
139 lines (122 loc) · 3.44 KB
/
serial.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
char serialInput[100];
int serialPrompt = 0;
/**
* Listen serial event
**/
void serialEvent()
{
while (Serial.available())
{
char inChar = (char)Serial.read();
switch (inChar)
{
case '0'...'9':
serialInput[serialPrompt] = inChar - '0';
serialPrompt++;
break;
case '\n':
serialExecute();
break;
default:
serialInput[serialPrompt] = inChar;
serialPrompt++;
break;
}
}
}
/**
* Execute serial instruction on `\n` char
**/
void serialExecute()
{
switch (serialInput[0])
{
// case 'r':
// case 'R': // Rotation
// if (serialInput[1] == 'L' || serialInput[1] == 'l') {
// rotateLeft(serialInput[2] * 100 + serialInput[3] * 10 + serialInput[4]);
// Serial.print("Step left! -> ");
// Serial.println(serialInput[2] * 100 + serialInput[3] * 10 + serialInput[4]);
// }
// else {
// rotateRight(serialInput[2] * 100 + serialInput[3] * 10 + serialInput[4]);
// Serial.print("Step right! -> ");
// Serial.println(serialInput[2] * 100 + serialInput[3] * 10 + serialInput[4]);
// }
// break;
case 'f':
case 'F': // Front direction
Serial.print("Go! -> ");
Serial.println(serialInput[1] * 1000 + serialInput[2] * 100 + serialInput[3] * 10 + serialInput[4]);
goForward(30, serialInput[1] * 1000 + serialInput[2] * 100 + serialInput[3] * 10 + serialInput[4]);
goBackward(1); // Break!
delay(500);
stopMotors();
break;
case 'b':
case 'B': // Back direction
Serial.print("Revert revert revert! -> ");
Serial.println(serialInput[1] * 100 + serialInput[2] * 10 + serialInput[3]);
goBackward(60);
delay(serialInput[1] * 100 + serialInput[2] * 10 + serialInput[3]);
goBackward(1);
delay(100);
stopMotors();
break;
case 1: // Recalage
Serial.print("Recalage -> ");
Serial.println(serialInput[1] * 10 + serialInput[2]);
goBackward(serialInput[1] * 10 + serialInput[2]);
delay(3000);
stopMotors();
break;
case 's':
case 'S': // Starting block
Serial.println("Lock'n'load!");
prepareToStart();
break;
case 'g':
case 'G': // GO !!!
Serial.println("Smooth splosh");
smoothSploch();
break;
case 'W':
case 'w': // Emergy stop
Serial.println("Emergency Stop!!");
stopMotors();
break;
case 'd':
case 'D':
Serial.println("Right on");
setRightSpeed(30);
break;
// TELECOMMAND API
case 'l':
Serial.println("left back");
setLeftSpeed(- serialInput[1] * 100 + serialInput[2] * 10 + serialInput[3]);
break;
case 'L':
Serial.println("left front");
setLeftSpeed(serialInput[1] * 100 + serialInput[2] * 10 + serialInput[3]);
break;
case 'r':
Serial.println("right back");
setRightSpeed(- serialInput[1] * 100 + serialInput[2] * 10 + serialInput[3]);
break;
case 'R':
Serial.println("right front");
setRightSpeed(serialInput[1] * 100 + serialInput[2] * 10 + serialInput[3]);
break;
case 'V':
Serial.println("PUG");
break;
default:
Serial.print("Unkwnown command (");
Serial.print(serialInput[0]);
Serial.println(")");
}
// Reset var for next instruction
for (int i = 0; i < 10; i++)
serialInput[i] = 0;
serialPrompt = 0;
}