#include <Keypad.h>
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
bool numbers[10][8] {
{1, 1, 1, 1, 1, 1, 0, 0}, // 0
{0, 1, 1, 0, 0, 0, 0, 0}, // 1
{1, 1, 0, 1, 1, 0, 1, 0}, // 2
{1, 1, 1, 1, 0, 0, 1, 0}, // 3
{0, 1, 1, 0, 0, 1, 1, 0}, // 4
{1, 0, 1, 1, 0, 1, 1, 0}, // 5
{1, 0, 1, 1, 1, 1, 1, 0}, // 6
{1, 1, 1, 0, 0, 0, 0, 0}, // 7
{1, 1, 1, 1, 1, 1, 1, 0}, // 8
{1, 1, 1, 1, 0, 1, 1, 0}, // 9
};
bool ports[4][4] {
{1,0,0,0},
{0,1,0,0},
{0,0,1,0},
{0,0,0,1},
};
// add input pullup here
void setPinModes() { // sets up input/output
int i;
for(i = 0; i <= 8; i++) {
pinMode(i, OUTPUT);
}
for(i = 14; i <= 17; i++) {
pinMode(i, OUTPUT);
}
}
// this will test if all the segments are connected
// correctly
void ligthAll() {
int i;
for(i = 14; i <= 17; i++) {
digitalWrite(i, HIGH);
}
for(i = 0; i <= 8; i++) {
digitalWrite(i, LOW);
}
}
// takes a number, and which ports should display it
// for ports see const above
void lightNumber(int number, int port) {
bool *numberArr = numbers[number];
bool *portArr = ports[port];
int i;
for(i = 14; i <= 17; i++) {
if (portArr[i - 14]) {
digitalWrite(i, HIGH); // choose
}
else {
digitalWrite(i, LOW);
}
}
for(i = 0; i <= 8; i++) {
if (numberArr[i]) {
digitalWrite(i, LOW); // turn on the light
}
else {
digitalWrite(i, HIGH); // turn it off
}
}
}
uint8_t colPins[COLS] = { 5, 4, 3, 2 }; // Pins connected to C1, C2, C3, C4
uint8_t rowPins[ROWS] = { 9, 8, 7, 6 }; // Pins connected to R1, R2, R3, R4
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void testLights() {
int i;
for(int i = 0; i < 4; i++) {
lightNumber(i, i);
}
}
void setup() {
setPinModes();
}
void loop() {
//lightNumber(2, 0);
testLights();
delay(1500);
}