-
Notifications
You must be signed in to change notification settings - Fork 0
/
AlexaPi-Arduino.ino
85 lines (70 loc) · 1.59 KB
/
AlexaPi-Arduino.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
#include <Arduino.h>
const byte PIN_LED_PLAYBACK = 2;
const byte PIN_LED_RECORDING = 3;
const byte PIN_BUTTON_TRIGGER = 5;
enum states {
IDLE,
PLAYBACK,
RECORDING,
PROCESSING,
};
states state = IDLE;
states statePrevious = IDLE;
byte buttonState = false;
byte buttonStatePrevious = false;
char serialData;
void setup()
{
Serial.begin(9600);
pinMode(PIN_LED_PLAYBACK, OUTPUT);
pinMode(PIN_LED_RECORDING, OUTPUT);
pinMode(PIN_BUTTON_TRIGGER, INPUT_PULLUP);
// wait for serial port to connect. Needed for Leonardo only
while (!Serial) {
;
}
}
void loop()
{
if (Serial.available()) {
statePrevious = state;
serialData = (char) Serial.read();
if (serialData == 'l') {
state = PLAYBACK;
} else if (serialData == 'r') {
state = RECORDING;
} else if (serialData == 'p') {
state = PROCESSING;
} else if (serialData == 'L' || serialData == 'R' || serialData == 'P') {
state = IDLE;
}
}
if (statePrevious != state) {
digitalWrite(PIN_LED_PLAYBACK, 0);
digitalWrite(PIN_LED_RECORDING, 0);
switch (state) {
case PLAYBACK:
digitalWrite(PIN_LED_PLAYBACK, 1);
break;
case RECORDING:
digitalWrite(PIN_LED_RECORDING, 1);
break;
case PROCESSING:
digitalWrite(PIN_LED_PLAYBACK, 1);
digitalWrite(PIN_LED_RECORDING, 1);
break;
default:
break;
}
}
// since we use an internal PULLUP resistor, we have to invert the state
buttonStatePrevious = buttonState;
buttonState = !digitalRead(PIN_BUTTON_TRIGGER);
if (buttonState != buttonStatePrevious) {
if (buttonState) {
Serial.write('t');
} else {
Serial.write('T');
}
}
}