// ----[ Example by Gray Mack ]------------------------------
// ----[ configuration ]------------------------------------
#define CONSOLE_BAUD 9600
// ----[ includes ]------------------------------------------
#include "KeypadPlus4Led.h"
// ----[ pin definitions ]------------------------------------
const uint8_t PIN_KEYPAD_C0 = 2; // also LED0 cathode
const uint8_t PIN_KEYPAD_C1 = 3; // also LED1 cathode
const uint8_t PIN_KEYPAD_C2 = 4; // also LED2 cathode
const uint8_t PIN_KEYPAD_C3 = 5; // also LED3 cathode
const uint8_t PIN_KEYPAD_R0 = 6;
const uint8_t PIN_KEYPAD_R1 = 7;
const uint8_t PIN_KEYPAD_R2 = 8;
const uint8_t PIN_KEYPAD_R3 = 12;
const uint8_t PIN_LEDS_ANODE = A3; // could be any pin I just chose
// an analog one arbitrarily.
// This controls the LED anodes
// Note I use 1k resistors to protect
// the pin from over current since
// it is driving 4 LEDs
// ----[ constants ]------------------------------------
enum KeyActions : uint8_t {
NoInput = 0,
RedOn, GreenOn, BlueOn, OrangeOn,
RedOff, GreenOff, BlueOff, OrangeOff,
RedToggle, GreenToggle, BlueToggle, OrangeToggle,
RedRead, GreenRead, BlueRead, OrangeRead
};
const char KeyChar[1 + KEYPAD_ROWS * KEYPAD_COLS] = {
'-',
'1', '2', '3', 'A',
'4', '5', '6', 'B',
'7', '8', '9', 'C',
'*', '0', '#', 'D'
};
const uint8_t LED_RED = 0;
const uint8_t LED_GREEN = 1;
const uint8_t LED_BLUE = 2;
const uint8_t LED_ORANGE = 3;
// ----[ global variables ]------------------------------------
// ----[ code ]------------------------------------
void setup() {
Serial.begin(CONSOLE_BAUD);
KeypadPlus4Led::InitKeypad(
PIN_KEYPAD_C0, PIN_KEYPAD_C1, PIN_KEYPAD_C2, PIN_KEYPAD_C3,
PIN_KEYPAD_R0, PIN_KEYPAD_R1, PIN_KEYPAD_R2, PIN_KEYPAD_R3,
PIN_LEDS_ANODE);
}
void loop() {
char input = KeypadPlus4Led::ScanKey();
if(input == NoInput) return;
Serial.print("key: "); Serial.print((int)input);
Serial.print(" val: '"); Serial.print(KeyChar[input]); Serial.println("'");
switch(input)
{
case RedOn:
KeypadPlus4Led::LedWrite(LED_RED, HIGH);
Serial.println("Red On");
break;
case GreenOn:
KeypadPlus4Led::LedWrite(LED_GREEN, HIGH);
Serial.println("Green On");
break;
case BlueOn:
KeypadPlus4Led::LedWrite(LED_BLUE, HIGH);
Serial.println("Blue On");
break;
case OrangeOn:
KeypadPlus4Led::LedWrite(LED_ORANGE, HIGH);
Serial.println("Orange On");
break;
case RedOff:
KeypadPlus4Led::LedWrite(LED_RED, LOW);
Serial.println("Red Off");
break;
case GreenOff:
KeypadPlus4Led::LedWrite(LED_GREEN, LOW);
Serial.println("Green Off");
break;
case BlueOff:
KeypadPlus4Led::LedWrite(LED_BLUE, LOW);
Serial.println("Blue Off");
break;
case OrangeOff:
KeypadPlus4Led::LedWrite(LED_ORANGE, LOW);
Serial.println("Orange Off");
break;
case RedToggle:
KeypadPlus4Led::LedWrite(LED_RED, ! KeypadPlus4Led::LedRead(LED_RED));
Serial.println("Red Toggle");
break;
case GreenToggle:
KeypadPlus4Led::LedWrite(LED_GREEN, ! KeypadPlus4Led::LedRead(LED_GREEN));
Serial.println("Green Toggle");
break;
case BlueToggle:
KeypadPlus4Led::LedWrite(LED_BLUE, ! KeypadPlus4Led::LedRead(LED_BLUE));
Serial.println("Blue Toggle");
break;
case OrangeToggle:
KeypadPlus4Led::LedWrite(LED_ORANGE, ! KeypadPlus4Led::LedRead(LED_ORANGE));
Serial.println("Orange Toggle");
break;
case RedRead:
Serial.print("Red Read "); Serial.println(KeypadPlus4Led::LedRead(LED_RED));
break;
case GreenRead:
Serial.print("Green Read "); Serial.println(KeypadPlus4Led::LedRead(LED_GREEN));
break;
case BlueRead:
Serial.print("Blue Read "); Serial.println(KeypadPlus4Led::LedRead(LED_BLUE));
break;
case OrangeRead:
Serial.print("Orange Read "); Serial.println(KeypadPlus4Led::LedRead(LED_ORANGE));
break;
}
}