Skip to content

Commit b3ffa1a

Browse files
committed
Added mode selection to arduino sketch
1 parent 59b4cd1 commit b3ffa1a

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

diyemg_sketch/diyemg_sketch.ino

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,47 @@ inline void __attribute__((optimize("O3"))) update_output(const int level) {
7575

7676
void setup(void) {
7777
Serial.begin(115200);
78-
78+
String mode;
79+
BYTE channel_select = 0;
80+
7981
// indicator
8082
pinMode(LED_BUILTIN, OUTPUT);
8183
digitalWrite(LED_BUILTIN, LOW);
82-
83-
// setup analog input A0 for operation
84+
85+
// run until we get a valid input mode string from the computer
86+
while(true) {
87+
88+
// Wait for input from the capture program to determine which
89+
while(!Serial.available()) {
90+
mode = Serial.readString(); // using C++ strings for input
91+
mode.trim();
92+
}
93+
94+
// use input from serial to select ADC channel
95+
if(mode == "NORMAL") {
96+
channel_select = 0;
97+
break;
98+
}
99+
else if (mode == "WAVE") {
100+
channel_select = 1;
101+
break;
102+
} else {
103+
104+
// input mode string was invalid, send error message to program,
105+
// clear buffers, and try again
106+
Serial.println("INVALID");
107+
while(!Serial.available()) {
108+
Serial.read();
109+
}
110+
continue;
111+
}
112+
}
113+
114+
// setup analog input A0 or A1 for operation
84115
// optimized for high-performance
85-
ADMUX = (1 << REFS0);
116+
// channel_select determines which ADC channel is active
117+
// 0 = A0, 1 = A1
118+
ADMUX = (1 << REFS0) | channel_select;
86119
ADCSRA = 0x82;
87120
}
88121

0 commit comments

Comments
 (0)