-
Notifications
You must be signed in to change notification settings - Fork 2
/
recorder.ino
165 lines (148 loc) · 3.46 KB
/
recorder.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Complete the code so the commandTimes
// updatePlayCommands() function waits the
// correct amount of time before running the
// next command (look for uses of the commandTimes[] array).
// Add new commands to toggle each LED on and off.
int pins[] = {
2,
3,
4,
5,
6,
7,
8,
9,
};
bool playing = false;
bool recording = false;
// Try to remove const and see what happens
const int maxCommands = 20;
int numCommands = 0;
char commands[maxCommands];
unsigned long commandTimer = 0;
// The time between each command
unsigned long commandTimes[maxCommands];
void setup()
{
Serial.begin(9600);
for (int pin : pins)
{
pinMode(pin, OUTPUT);
}
Serial.println("Commands:");
Serial.println("p: Play recorded commands");
Serial.println("r: Record commands");
Serial.println("");
Serial.println("a: Turn all LEDs on");
Serial.println("b: Turn all LEDs off");
Serial.println("c: Turn all LEDs on one by one");
Serial.println("d: Turn all LEDs off one by one");
}
void runCommand(char command)
{
switch (command)
{
case 'a':
for (int pin : pins)
{
digitalWrite(pin, HIGH);
}
break;
case 'b':
for (int pin : pins)
{
digitalWrite(pin, LOW);
}
break;
case 'c':
for (int pin : pins)
{
digitalWrite(pin, HIGH);
delay(100);
}
break;
case 'd':
for (int pin : pins)
{
digitalWrite(pin, LOW);
delay(100);
}
break;
}
}
void processCommand(char command)
{
switch (command)
{
case 'p':
playing = !playing;
Serial.println(playing ? "Playing" : "Stopped playing");
break;
case 'r':
recording = !recording;
commandTimer = millis();
Serial.println(recording ? "Recording" : "Stopped recording");
if (recording)
numCommands = 0;
break;
default:
runCommand(command);
if (recording)
{
if (numCommands < maxCommands)
{
unsigned long duration = millis() - commandTimer;
Serial.print("[");
Serial.print(numCommands + 1);
Serial.print("/");
Serial.print(maxCommands);
Serial.print("/");
Serial.print(duration);
Serial.print("ms");
Serial.print("]: ");
Serial.println(command);
commandTimes[numCommands] = duration;
commands[numCommands] = command;
commandTimer = millis();
numCommands++;
}
if (numCommands >= maxCommands)
{
Serial.println("Max commands reached");
processCommand('r');
}
}
break;
}
}
void updateSerial()
{
while (Serial.available())
{
char command = Serial.read();
if (command == '\n')
{
continue;
}
processCommand(command);
}
}
void updatePlayCommands()
{
if (playing)
{
Serial.println("Playing commands: ");
for (int i = 0; i < numCommands; i++)
{
char command = commands[i];
Serial.print(command);
runCommand(command);
}
Serial.println("");
}
}
void loop()
{
updateSerial();
updatePlayCommands();
}