//UKHB version. Data driven program i.e., extension is easy because you can just use an array to add more LEDs
#define OFF LOW //set these HGH or LOW to suit your LED wiring
#define ON HIGH
struct ledData //data needed for each LED
{
byte pinNum;
byte currentState;
char key;
int intensity;
};
ledData leds[] = { // initialize LED data
{ 2, OFF, 'A', HIGH},
{ 3, OFF, 'B', HIGH},
{ 4, OFF, 'C', HIGH},
{ 5, OFF, 'D', HIGH},
{ 6, OFF, 'E', HIGH},
{ 7, OFF, 'F', HIGH},
{ 8, OFF, 'G', HIGH},
{ 9, OFF, 'H', HIGH},
{ 10, OFF, 'I', HIGH},
{ 11, OFF, 'J', HIGH},
{ 12, OFF, 'K', HIGH},
{ 13, OFF, 'L', HIGH}
};
const byte LED_COUNT = sizeof(leds) / sizeof(leds[0]); //calculate the number of LEDs
void setup()
{
Serial.begin(115200);
Serial.println();
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
showLeds(); //show LEDs in there initial state
}
void loop()
{
if (Serial.available()) //wait for Serial input
{
char c = toupper(Serial.read()); //get a character
for (int x = 0; x < LED_COUNT; x++) //search for the character in the LED data
{
if (leds[x].key == c) //if there is a match
{
leds[x].currentState = !leds[x].currentState; //change the state of matching LED
}
}
showLeds(); //display the LEDs in their current state
}
}
void showLeds()
{
for (int c = 0; c < LED_COUNT; c++) //for each LED
{
if (leds[c].currentState == ON) //if the current State of the LED is ON,
{
digitalWrite(leds[c].pinNum, leds[c].intensity);
// assign to each LED the given intensity in the array
}
else // if the current State is OFF, then leave it OFF
{
digitalWrite(leds[c].pinNum, 0);
}
}
}