@@ -27,7 +27,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27
27
#define TRIGGER_LOW 150 // Low limit for trigger, adjust to change false-trigger rejection
28
28
#define TRIGGER_WAIT_TIME_MS 50 // Delay between triggers, lower to increase speed of triggering
29
29
typedef unsigned char BYTE;
30
-
30
+ BYTE channel_select = 0 ;
31
31
32
32
// Read analog input value and store in *level
33
33
// optimized for high-performance
@@ -76,40 +76,48 @@ inline void __attribute__((optimize("O3"))) update_output(const int level) {
76
76
void setup (void ) {
77
77
Serial.begin (115200 );
78
78
String mode;
79
- BYTE channel_select = 0 ;
80
79
81
80
// indicator
82
81
pinMode (LED_BUILTIN, OUTPUT);
83
82
digitalWrite (LED_BUILTIN, LOW);
84
83
85
84
// run until we get a valid input mode string from the computer
86
- while (true ) {
85
+ while (1 ) {
87
86
88
87
// Wait for input from the capture program to determine which
89
- while (!Serial.available ()) {
88
+ while (Serial.available () == 0 ) {}
89
+ if (Serial.available ()) {
90
90
mode = Serial.readString (); // using C++ strings for input
91
91
mode.trim ();
92
+ } else {
93
+ continue ;
92
94
}
93
-
95
+
94
96
// use input from serial to select ADC channel
95
97
if (mode == " NORMAL" ) {
96
98
channel_select = 0 ;
97
99
break ;
98
100
}
99
- else if (mode == " WAVE" ) {
101
+ else if (mode == " WAVE" ) {
100
102
channel_select = 1 ;
101
103
break ;
102
104
} else {
103
105
104
106
// input mode string was invalid, send error message to program,
105
107
// clear buffers, and try again
106
- Serial.println (" INVALID" );
108
+ Serial.print (" INVALID \" " );
109
+ Serial.print (mode);
110
+ Serial.println (" \" " );
111
+ Serial.flush ();
107
112
while (!Serial.available ()) {
108
113
Serial.read ();
109
114
}
110
115
continue ;
111
116
}
112
- }
117
+ }
118
+
119
+ Serial.println (" READY" );
120
+ Serial.flush ();
113
121
114
122
// setup analog input A0 or A1 for operation
115
123
// optimized for high-performance
@@ -123,6 +131,18 @@ void setup(void) {
123
131
// optimized for high-performance
124
132
void __attribute__ ((optimize(" O3" ), flatten)) loop(void ) {
125
133
int analog_level;
126
- update_reading (&analog_level);
127
- update_output (analog_level);
134
+ char output_buffer[6 ];
135
+ while (channel_select == 0 ) {
136
+ update_reading (&analog_level);
137
+ update_output (analog_level);
138
+ }
139
+ while (channel_select == 1 ) {
140
+ while (Serial.available () == 0 ) {}
141
+ while (Serial.available () > 0 ) {
142
+ Serial.read ();
143
+ }
144
+ update_reading (&analog_level);
145
+ sprintf (output_buffer, " $%03d\n " , analog_level);
146
+ Serial.print (output_buffer);
147
+ }
128
148
}
0 commit comments