#include "Adafruit_TLC5947.h"
int pins[] = {
A0,
A1,
A2,
A3,
A4,
A5,
2,
3,
7,
8,
9,
10,
11,
12,
};
bool states[] = { false, false, false, false, false, false, false, false, false, false, false, false, false, false };
#define numButtons 14
#define NUM_TLC5974 2
#define data 4
#define clock 5
#define latch 6
Adafruit_TLC5947 tlc = Adafruit_TLC5947(NUM_TLC5974, clock, data, latch);
void setup()
{
Serial.begin(115200);
tlc.begin();
for (int i = 0; i < numButtons; i++) {
pinMode(pins[i], INPUT_PULLUP);
}
}
unsigned long elapsedTime = 0;
#define loopDelayTime 100
void loop()
{
unsigned long currentTime = millis();
if (currentTime - elapsedTime >= loopDelayTime) {
elapsedTime = currentTime;
checkButtons();
}
}
void checkButtons()
{
for (int i = 0; i < numButtons; i++) {
bool* currentValue = &states[i];
bool newValue = !digitalRead(pins[i]);
if (*currentValue != newValue) {
*currentValue = newValue;
sendUpdate(i, newValue);
}
}
};
void sendUpdate(int buttonId, bool state)
{
Serial.print(buttonId);
Serial.println(state);
};
const int BUFFER_SIZE = 42;
char buf[BUFFER_SIZE];
void handleUpdate()
{
for (int i = 0; i < BUFFER_SIZE; i += 3) {
tlc.setLED(i, buf[i], buf[i + 1], buf[i + 2]);
tlc.write();
}
}
void serialEvent()
{
while (Serial.available()) {
int outputId = Serial.read();
Serial.readBytesUntil('\n', buf, BUFFER_SIZE);
Serial.print(F("Arduino-Update Output: "));
Serial.println(outputId);
handleUpdate();
}
}