/*
This is mostly working now. Needs fine tuning such as only lcd.printing the most recent number entered instead of the previous number and the most recent
*/
#include <IRremote.h>
#include <LiquidCrystal.h>
bool programming_mode = false;
bool review_mode = false;
bool firing_confirmation = false;
bool firing_mode = false;
int group_num = 9;
int port_num = 8;
int delay_time = 1;
int shot_num = 1;
int key;
int temp_num = 2;
#define IR_RECEIVE_PIN 3 // Signal Pin of IR receiver
IRrecv receiver(IR_RECEIVE_PIN);
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
const int numOptions = 3;
String options[numOptions] = {"Group Number: ", "Port Number: ", "Delay(ms): "};
String answers[4][numOptions]; // Store 10 groups of answers
void setup() {
Serial.begin(115200); // Initialize serial communication for debugging
while (!Serial);
Serial.println();
lcd.begin(20, 4);
lcd.print(" Start up complete.");
IrReceiver.enableIRIn(); // Start the IR receiver
Serial.println("#### END OF Setup ####");
}
//this version doesn't seem to work.
void Programming_Mode(int key_number) {
Serial.println();
Serial.println("#### START OF Programming_Mode ####");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("!Program! Shot# ");
//Serial.println("********");
//Serial.println("!Program! Shot# ");
// Initialize an array to store the current values being entered
String currentValues[numOptions];
for (int i = 0; i < numOptions; ++i) {
currentValues[i] = "";
}
Serial.print("currentValues[numOptions] = ");
Serial.println(currentValues[numOptions]);
while (shot_num <= temp_num) {
lcd.print(shot_num);
Serial.print("shot_num = ");
Serial.println(shot_num);
for (int i = 0; i < numOptions; ++i) {
lcd.setCursor(0, i + 1);
lcd.print(options[i]);
Serial.print("options[i] = ");
Serial.println(options[i]);
bool moveNext = false;
Serial.print("moveNext = ");
Serial.println(moveNext);
while (!moveNext) {
if (IrReceiver.decode()) {
Serial.print("IrReceiver.decode.command = ");
Serial.println(IrReceiver.decodedIRData.command);
if (IrReceiver.decodedIRData.command == 144) {
moveNext = true;
}
else {
switch (IrReceiver.decodedIRData.command) {
case 226: //MENU
firing_confirmation = false;
firing_mode = false;
programming_mode = false;
key = 11;
Serial.print("key = ");
Serial.println(key);
break;
case 104: //0
firing_confirmation = false;
firing_mode = false;
key = 0;
Serial.print("key = ");
Serial.println(key);
break;
case 48: //1
firing_confirmation = false;
firing_mode = false;
key = 1;
Serial.print("key = ");
Serial.println(key);
break;
case 24: //2
firing_confirmation = false;
firing_mode = false;
key = 2;
Serial.print("key = ");
Serial.println(key);
break;
case 122: //3
firing_confirmation = false;
firing_mode = false;
key = 3;
Serial.print("key = ");
Serial.println(key);
break;
case 16: //4
firing_confirmation = false;
firing_mode = false;
key = 4;
Serial.print("key = ");
Serial.println(key);
break;
case 56: //5
firing_confirmation = false;
firing_mode = false;
key = 5;
Serial.print("key = ");
Serial.println(key);
break;
case 90: //6
firing_confirmation = false;
firing_mode = false;
key = 6;
Serial.print("key = ");
Serial.println(key);
break;
case 66: //7
firing_confirmation = false;
firing_mode = false;
key = 7;
Serial.print("key = ");
Serial.println(key);
break;
case 74: //8
firing_confirmation = false;
firing_mode = false;
key = 8;
Serial.print("key = ");
Serial.println(key);
break;
case 82: //9
firing_confirmation = false;
firing_mode = false;
key = 9;
Serial.print("key = ");
Serial.println(key);
break;
default:
continue; // Skip processing if command is not a numeric key
}
currentValues[i] += key; // Append key to current value for this option
Serial.print("key = ");
Serial.print(key);
//lcd.setCursor(options[i].length(), i + 1); // Move cursor to after option text
lcd.print(currentValues[i]); // Print current value for this option
Serial.print("currentValues[i] = ");
Serial.println(currentValues[i]);
}
IrReceiver.resume(); // Receive the next value
}
}
}
shot_num++;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("!Program! Shot# ");
Serial.print("!Program! Shot# ");
}
// Store current values into answers array
for (int i = 0; i < numOptions; ++i) {
Serial.print("i = ");
Serial.println(i);
answers[shot_num - 2][i] = currentValues[i]; // Store values in the correct shot_num index
Serial.print("currentValues[i] = ");
Serial.println(currentValues[i]);
Serial.print("answers[shot_num - 2][i] = ");
Serial.println(answers[shot_num - 2][i]);
}
// Print stored answers for verification
Serial.println();
Serial.println("Stored Answers:");
for (int fo = 0; fo < temp_num; ++fo) {
Serial.print("Shot# ");
Serial.println(fo + 1);
for (int i = 0; i < numOptions; ++i) {
Serial.print(options[i]);
Serial.print(": ");
Serial.println(answers[fo][i]);
}
Serial.println(); // Blank line between groups for clarity
}
Serial.println("#### END OF Programming_Mode ####");
}
//this version works, but shows multiple characters on the lcd instead of single characters.
/*
void Programming_Mode(int key_number){
Serial.println();
Serial.println("#### START OF Programming_Mode ####");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("!Program! Shot# ");
while (shot_num <= temp_num) {
lcd.print(shot_num);
//Serial.print("shot_num = ");
//Serial.println(shot_num);
for (int i = 0; i < numOptions; ++i) {
lcd.setCursor(0, i + 1);
lcd.print(options[i]);
bool moveNext = false;
while (!moveNext) {
if (IrReceiver.decode()) {
if (IrReceiver.decodedIRData.command == 144) {
moveNext = true;
}
else {
switch (IrReceiver.decodedIRData.command) {
case 226: //MENU
firing_confirmation = false;
firing_mode = false;
//programming_mode = !programming_mode;
//lcdPrint("MENU", 11);
//Programming_Mode(11);
break;
case 104: //0
firing_confirmation = false;
firing_mode = false;
//lcdPrint("0", 0);
key = (0);
break;
case 48: //1
firing_confirmation = false;
firing_mode = false;
//lcdPrint("1", 1);
key = (1);
break;
case 24: //2
firing_confirmation = false;
firing_mode = false;
//lcdPrint("2", 2);
key = (2);
break;
case 122: //3
firing_confirmation = false;
firing_mode = false;
//lcdPrint("3", 3);
key = (3);
break;
case 16: //4
firing_confirmation = false;
firing_mode = false;
//lcdPrint("4", 4);
key = (4);
break;
case 56: //5
firing_confirmation = false;
firing_mode = false;
//lcdPrint("5", 5);
key = (5);
break;
case 90: //6
firing_confirmation = false;
firing_mode = false;
//lcdPrint("6", 6);
key = (6);
break;
case 66: //7
firing_confirmation = false;
firing_mode = false;
//lcdPrint("7", 7);
key = (7);
break;
case 74: //8
firing_confirmation = false;
firing_mode = false;
//lcdPrint("8", 8);
key = (8);
break;
case 82: //9
firing_confirmation = false;
firing_mode = false;
//lcdPrint("9", 9);
key = 9;
break;
default: //UNKNOWN
firing_confirmation = false;
firing_mode = false;
}
currentValues[i] += String(key);
answers[shot_num - 1][i] += key; // Store answer in correct shot_num index as string
lcd.print(answers[shot_num - 1][i]);
}
IrReceiver.resume(); // Receive the next value
}
}
}
Serial.println();
shot_num++;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("!Program! Shot# ");
}
// Print stored answers for verification
Serial.println();
Serial.println("Stored Answers:");
for (int fo = 0; fo < temp_num; ++fo) {
Serial.print("Shot# ");
Serial.println(fo + 1);
for (int i = 0; i < numOptions; ++i) {
Serial.print(options[i]);
//Serial.print(": ");
Serial.println(answers[fo][i]);
}
Serial.println(); // Blank line between groups for clarity
}
Serial.println("#### END OF Programming_Mode ####");
}
*/
void loop() {
if (IrReceiver.decode()) {
int value = IrReceiver.decodedIRData.command;
translateIR();
// Handle specific IR commands if needed
IrReceiver.resume(); // Receive the next value
}
}
void translateIR() {
Serial.println();
Serial.println("#### START OF translateIR ####");
// Takes command based on IR code received
switch (IrReceiver.decodedIRData.command) {
case 226: //MENU
firing_confirmation = false;
firing_mode = false;
programming_mode = !programming_mode;
lcdPrint("MENU", 11);
key = 11;
Programming_Mode(11);
break;
case 34: //TEST
firing_confirmation = false;
firing_mode = false;
review_mode = !review_mode;
lcdPrint("TEST", 12);
key = 12;
break;
case 168: //PLAY
if (!firing_confirmation) {
firing_confirmation = !firing_confirmation;
firing_mode = false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("!!! FIRING MODE !!!");
lcd.setCursor(0, 1);
lcd.print("");
lcd.setCursor(0, 2);
lcd.print(" Press Play again ");
lcd.setCursor(0, 3);
lcd.print(" to fire program. ");
} else {
firing_mode = true;
programming_mode = false;
review_mode = false;
firing_confirmation = !firing_confirmation;
//Display firing program in realtime here
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("!!! Firing mode: !!!");
lcd.setCursor(0, 2);
lcd.print(" !!! RUNNING !!!");
}
lcdPrint("PLAY", 16);
key = 16;
break;
case 144: //NEXT
firing_confirmation = false;
firing_mode = false;
lcdPrint("NEXT", 17);
key = 17;
//Programming_Mode(17);
break;
/*
case 162: //POWER
firing_confirmation = false;
firing_mode = false;
lcdPrint("POWER", 10); //this passes two variables to the translateIR function, The first is "text" and the second is "number".
key = 10;
break;
case 2: //PLUS
firing_confirmation = false;
firing_mode = false;
lcdPrint("PLUS", 13);
key = 13;
break;
case 194: //BACK
firing_confirmation = false;
firing_mode = false;
lcdPrint("BACK", 14);
key = 14;
break;
case 224: //PREV.
firing_confirmation = false;
firing_mode = false;
lcdPrint("PREV.", 15);
key = 15;
break;
case 152: //MINUS
firing_confirmation = false;
firing_mode = false;
lcdPrint("MINUS", 18);
key = 18;
break;
case 176: //C
firing_confirmation = false;
firing_mode = false;
lcdPrint("C", 19);
key = 19;
break;
*/
case 104: //0
firing_confirmation = false;
firing_mode = false;
lcdPrint("0", 0);
key = 0;
//Programming_Mode(0);
break;
case 48: //1
firing_confirmation = false;
firing_mode = false;
lcdPrint("1", 1);
key = 1;
break;
case 24: //2
firing_confirmation = false;
firing_mode = false;
lcdPrint("2", 2);
key = 2;
break;
case 122: //3
firing_confirmation = false;
firing_mode = false;
lcdPrint("3", 3);
key = 3;
break;
case 16: //4
firing_confirmation = false;
firing_mode = false;
lcdPrint("4", 4);
key = 4;
break;
case 56: //5
firing_confirmation = false;
firing_mode = false;
lcdPrint("5", 5);
key = 5;
break;
case 90: //6
firing_confirmation = false;
firing_mode = false;
lcdPrint("6", 6);
key = 6;
break;
case 66: //7
firing_confirmation = false;
firing_mode = false;
lcdPrint("7", 7);
key = 7;
break;
case 74: //8
firing_confirmation = false;
firing_mode = false;
lcdPrint("8", 8);
key = 8;
break;
case 82: //9
firing_confirmation = false;
firing_mode = false;
lcdPrint("9", 9);
key = 9;
break;
default: //UNKNOWN
firing_confirmation = false;
firing_mode = false;
review_mode = false;
programming_mode = false;
lcdPrint("UNKNOWN", 99);
key = 99;
/*
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(">> Button Pressed <<");
lcd.setCursor(0, 2);
lcd.print("!! Unknown Button !!");
lcd.setCursor(0, 3);
lcd.print("IR code: ");
lcd.print(IrReceiver.decodedIRData.command);
*/
}
Serial.println("#### END OF translateIR ####");
Serial.println();
Serial.println();
}
void lcdPrint(char* text, int number) {
Serial.println();
Serial.println("#### START OF lcdPrint ####");
if ((firing_confirmation) || (firing_mode) || (programming_mode)) {
goto fire_mode;
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(">> Button Pressed <<");
lcd.setCursor(0, 1);
lcd.print("Button Text: ");
lcd.print(text); //this prints the first of the two variables passed from the translateIR function
lcd.setCursor(0, 2);
lcd.print("Key Number: ");
lcd.print(number); //this prints the second of the two variables passed from the translateIR function
lcd.setCursor(0, 3);
lcd.print("IR code: ");
lcd.print(IrReceiver.decodedIRData.command);
fire_mode:
Serial.println(">> Button Pressed <<");
Serial.print("Button Text: ");
Serial.println(text);
Serial.print("Key Number: ");
Serial.println(number);
Serial.print("IR code: ");
Serial.println(IrReceiver.decodedIRData.command);
Serial.println("***********************");
Serial.print("programming_mode = ");
Serial.println(programming_mode);
Serial.print("review_mode = ");
Serial.println(review_mode);
Serial.print("firing_confirmation = ");
Serial.println(firing_confirmation);
Serial.print("firing_mode = ");
Serial.println(firing_mode);
Serial.println("***********************");
Serial.println("#### END OF lcdPrint ####");
}