Hello! I am creating a scoreboard that I want to be able to control from an IR remote as well as a keypad on the side of it. I currently have a code that works perfectly for the keypad, however, I am unsure how to incorporate the keypad to call the same functions that the keypad buttons do. Here;s the working code:
#include "IRremote.h"
#include "LedControl.h" // Library used for communication with 7 segments
LedControl lc = LedControl(11, 12, 10, 1); // (DIN, CLK, LOAD, number of Max7219 chips)
#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
const int RECV_PIN = A0;
IRrecv irrecv(RECV_PIN);
decode_results results;
int scoreA; // Team A
int scoreB; // Team B
int scoreAdd = 0; // Hold the number that will be added to the team and the number that will be subtracted from current score if over 21
int value = 0; // the number accumulator
int key; // the key press
int isnum;
bool twentyOne;
bool bestOf;
bool powerOn = true;
int aOne;
int aTen;
int bOne;
int bTen;
const int dOff = 0xFF;
int aWin = false;
int bWin = false;
void(* resetFunc) (void) = 0;
void progRefresh() {
scoreA = 0;
scoreB = 0;
scoreAdd = 0;
aOne = 0;
aTen = 0;
bOne = 0;
bTen = 0;
lc.shutdown(0, false);
lc.setIntensity (0, 15);
lc.setDigit(0, 0, 0, aWin);
lc.setDigit(0, 1, 0, false);
lc.setDigit(0, 2, 0, bWin);
lc.setDigit(0, 3, 0, false);
}
void setup() {
Serial.begin(9600);
scoreA = 0;
scoreB = 0;
scoreAdd = 0;
powerOn = true;
aOne = 0;
aTen = 0;
bOne = 0;
bTen = 0;
bestOf = true;
twentyOne = true;
lc.shutdown(0, false); // Wake up MAX7219
lc.setIntensity (0, 15);
// Put zeros on both displays at startup
lc.setDigit(0, 0, 0, aWin); // (Max7219 chip #, Digit, value, DP on or off)
lc.setDigit(0, 1, 0, false);
lc.setDigit(0, 2, 0, bWin);
lc.setDigit(0, 3, 0, false);
irrecv.enableIRIn(); // Start the receiver
}
void loop() {
char key = keypad.getKey();
if (twentyOne == true) {
lc.setDigit(0, 5, 8, true);
}
if (twentyOne == false) {
lc.setDigit(0, 5, 4, false);
}
if (bestOf == true) {
lc.setDigit(0, 7, 8, true);
}
if (bestOf == false) {
lc.setDigit(0, 7, 4, false);
}
do
{
key = keypad.getKey(); // input the key
isnum = (key >= '0' && key <= '9'); // is it a digit?
if (isnum)
{
scoreAdd = scoreAdd * 10 + key - '0'; // accumulate the input number
}
}
while (isnum || !key); // until not a digit or while no key pressed
if (key != NO_KEY) {
if (key == 'A') {
scoreA += scoreAdd;
// Serial.println(results.value);
if (twentyOne == true) {
if (scoreA > 21) {
scoreAdd -= (21 - scoreA);
scoreA -= scoreAdd;
}
}
aOne = scoreA % 10;
aTen = scoreA / 10;
lc.setDigit(0, 0, aOne, aWin);
lc.setDigit(0, 1, aTen, false);
}
if (key == 'B') {
scoreB += scoreAdd;
if (twentyOne == true) {
if (scoreB > 21) {
scoreAdd -= (21 - scoreB);
scoreB -= scoreAdd;
}
}
bOne = scoreB % 10;
bTen = scoreB / 10;
lc.setDigit(0, 2, bOne, bWin);
lc.setDigit(0, 3, bTen, false);
}
if (key == '*') {
progRefresh();
bestOf = true;
twentyOne = true;
aWin = false;
bWin = false;
powerOn = !powerOn;
}
if (key == 'C') {
bestOf = !bestOf;
}
if (key == 'D') {
twentyOne = !twentyOne;
}
if (key == '#') {
progRefresh();
}
if (powerOn == true) {
lc.shutdown(0, false);
}
if (powerOn == false) {
lc.shutdown(0, true);
}
}
scoreAdd = 0;
if (scoreA == 21) {
if (bestOf == true) {
if (aWin == true) {
delay(3000);
resetFunc();
}
if (aWin == false) {
aWin = true;
delay(2000);
progRefresh();
}
}
if (bestOf == false) {
delay(3000);
progRefresh();
}
}
if (scoreB == 21) {
if (bestOf == true) {
if (bWin == true) {
delay(3000);
resetFunc();
}
if (bWin == false) {
delay(2000);
bWin = true;
progRefresh();
}
}
if (bestOf == false) {
delay(3000);
progRefresh();
}
}
}
I want the IR codes to perform the same functions as the keys that are pressed. What would be the best way to do this?
A) if (key == 'A' || results.value == 0xFF10EF);
-> adding the ir code to call each individual function?
-->tried but never successful
B) Redoing every function but only calling through IR codes
-> One section for keypad, one for IR remote but they do the same thing
C) Defining a code to equal a key
-> If the code 0xFF10EF is received, it would be the same as if key A was pressed?
--> Not sure how to go about this.
Thanks for any help!! Really appreciate it!!
Please let me know if you have any questions or if anything is unclear.