const byte buttonPins[] = { A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15 };
const byte ledPins[] = { 16, 17, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
const int numTrigs = sizeof(buttonPins) / sizeof(buttonPins[0]);
char outString[numTrigs + 1];
void setup() {
for (uint8_t i = 0; i < numTrigs; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
pinMode(ledPins[i], OUTPUT);
digitalWrite(ledPins[i], LOW);
}
Serial.begin(9600);
Serial.setTimeout(10);
}
void loop() {
// Build output strting
for (uint8_t i = 0; i < numTrigs; i++) {
uint8_t state = digitalRead(buttonPins[i]);
outString[i] = (state == LOW) ? '1' : '0';
}
outString[sizeof(outString)-1] = '\0'; // Terminate
// Read input string
int availableBytes = Serial.available();
if (availableBytes > 0) {
if (availableBytes == numTrigs + 1) {
for (int i=0; i < numTrigs; i++) {
char c = (char)Serial.read();
uint8_t ledState = (c == '1') ? HIGH : LOW;
digitalWrite(ledPins[i], ledState);
}
}
while (Serial.available()) Serial.read(); // Clear incoming buffer
}
Serial.println(outString); // Print
delay((50));
}