Skip to content

Commit 209b0c2

Browse files
committed
Bugfix: SerialLoop() bug
1 parent bc1d24c commit 209b0c2

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

examples/ProductionSketch/SerialLoop.cpp

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,31 @@
44
#include <Arduino.h>
55

66
void serialLoop(CRGB* leds) {
7+
78
static int pixelIndex;
89
int idx = 0;
910
uint8_t buffer[3];
11+
uint8_t c;
12+
1013
while(true) {
11-
if (Serial.available()) {
12-
uint8_t c = Serial.read();
14+
if (Serial.available() > 0) {
15+
c = Serial.read();
1316
if (c == 255) {
14-
LEDS.show();
15-
pixelIndex = 0;
16-
idx = 0;
17-
// BUTTON_IN (D10): 07 - 0111
18-
// EXTRA_PIN_A(D7): 11 - 1011
19-
// EXTRA_PIN_B (D11): 13 - 1101
20-
// ANALOG_INPUT (A9): 14 - 1110
21-
22-
char c = (digitalRead(BUTTON_IN) << 3)
23-
| (digitalRead(EXTRA_PIN_A) << 2)
24-
| (digitalRead(EXTRA_PIN_B) << 1)
25-
| (digitalRead(ANALOG_INPUT) );
26-
Serial.write(c);
17+
LEDS.show();
18+
pixelIndex = 0;
19+
idx = 0;
20+
// BUTTON_IN (D10): 07 - 0111
21+
// EXTRA_PIN_A(D7): 11 - 1011
22+
// EXTRA_PIN_B (D11): 13 - 1101
23+
// ANALOG_INPUT (A9): 14 - 1110
24+
25+
char c = (digitalRead(BUTTON_IN) << 3)
26+
| (digitalRead(EXTRA_PIN_A) << 2)
27+
| (digitalRead(EXTRA_PIN_B) << 1)
28+
| (digitalRead(ANALOG_INPUT) );
29+
Serial.write(c);
2730
} else {
28-
buffer[idx++] = Serial.read();
31+
buffer[idx++] = c;
2932
if (idx == 3) {
3033
if(pixelIndex == LED_COUNT) break; // Prevent overflow by ignoring the pixel data beyond LED_COUNT
3134
leds[pixelIndex] = CRGB(buffer[0], buffer[1], buffer[2]);
@@ -35,4 +38,4 @@ void serialLoop(CRGB* leds) {
3538
}
3639
}
3740
}
38-
}
41+
}

0 commit comments

Comments
 (0)