I am trying to get a 3 digit number into the first element of an array by getting input from the keypad. The code I have gets the three inputs from the keypad but adds them together instead of adding just the digit .
For example I want the user to enter a three digit number into the keypad for example 135. The way my code is now the first digit is printed correctly ie 1. When the 3 digit is pressed it adds the pressed 3 to the already present 1 to show 4. Then when the 5 is pressed it adds 5 to the existing 4 and shows 9. I need a way to concatenate the input into the array. I've searched the web and forum but haven't found a way. Help please. The code in question is under header "PAGE LEFT TURN". Thanks for any help in pointing me in the right direction.
BTW using Arduino UNO, LCD, and 4 x 4 keypad.
`#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
enum pageType { Main_Menu,
Menu_2,
Fwd,
Rev,
LTurn,
RTurn,
Pause,
Beep,
Run,
};
enum pageType currPage = Main_Menu;
byte data_count = 0;
const byte ROWS = 4;
const byte COLS = 4;
char Forward;
char Reverse;
char LeftTurn;
char RightTurn;
char PauseTime;
char BeepTime;
int v =0;
int null = "";
int x = 0;
int t = 0;
int d = 0;
int Time;
int revAmount;
int fwdAmount;
int leftAmount;
int leftRun[10];
int rightAmount;
int rightRun[10];
int runOrder[10];
char runDir[10];
int runDur[10];
int count =0;
int motor1pin1 = 10; // pin assignment for motors
int motor1pin2 = 13;
int motor2pin1 = 11;
int motor2pin2 = 12;
char hexaKeys[ROWS][COLS] = { // array for keypad
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
byte rowPins[ROWS] = { 9, 8, 7, 6 }; // pin assignments for keypad
byte colPins[COLS] = { 5, 4, 3, 2 };
Keypad customKeypad =
Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
LiquidCrystal_I2C lcd =
LiquidCrystal_I2C(0x27, 20, 4); // Change to (0x27,20,4) for 20x4 LCD.
// ==============================================================================
// || SETUP LOOP ||
// ==============================================================================
void setup() {
// put your setup code here, to run once:
pinMode(motor1pin1, OUTPUT);
pinMode(motor1pin2, OUTPUT);
pinMode(motor2pin1, OUTPUT);
pinMode(motor2pin2, OUTPUT);
// Initiate the LCD:
lcd.init();
lcd.backlight();
lcd.clear();
}
// ==============================================================================
// || MAIN LOOP ||
// ==============================================================================
void loop() {
// put your main code here, to run repeatedly:
switch (currPage) { // start menu system
case Main_Menu:
page_Main_Menu();
break;
}
}
// ==============================================================================
// || PAGE MAIN MENU called from void loop ||
// ==============================================================================
void page_Main_Menu() { // print menu
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" MAIN MENU");
lcd.setCursor(0, 1);
lcd.print("1. FWD 2. REV");
lcd.setCursor(0, 2);
lcd.print("3. LTURN 4. RTURN");
lcd.setCursor(0, 3);
lcd.print("5. NEXT #. RUN");
char customKey = customKeypad.waitForKey();
if (customKey) { // get input from menu
switch (customKey) {
case '1':
page_Fwd();
break;
case '2':
page_Rev();
break;
case '3':
page_LTurn();
break;
case '4':
page_RTurn();
break;
case '5':
page_Menu_2();
break;
case '#':
page_Run();
break;
default:
lcd.clear();
lcd.setCursor(8, 0);
lcd.print("Error");
delay(2000);
currPage = Main_Menu;
break;
}
}
}
// ==============================================================================
// || PAGE MENU 2 called from Main Menu ||
// ==============================================================================
void page_Menu_2() { // print menu
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" MENU 2");
lcd.setCursor(0, 1);
lcd.print("6. BEEP 7. PAUSE");
lcd.setCursor(0, 2);
lcd.print("8. 9. ");
lcd.setCursor(0, 3);
lcd.print("B. BACK #. RUN");
char customKey = customKeypad.waitForKey();
if (customKey) { // get input from menu
switch (customKey) {
case '6':
page_Beep();
break;
case '7':
page_Pause();
break;
case 'B':
page_Main_Menu();
break;
case '#':
page_Run();
break;
default:
lcd.clear();
lcd.setCursor(8, 0);
lcd.print("Error");
delay(2000);
currPage = Menu_2;
break;
}
}
}
// ==============================================================================
// || PAGE FORWARD called from main menu ||
// ==============================================================================
void page_Fwd() { // print menu
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("Distance in Feet");
lcd.setCursor(6, 1);
lcd.print(" 1 to 9");
lcd.setCursor(9, 2);
lcd.blink();
char Forward = customKeypad.waitForKey();
if (Forward >= '0' && Forward <= '9') { // weed out unavailable input
runDur[x] = Forward - '0';
runDir[x] = 'F';
lcd.noBlink();
runOrder[x] = runDir[x];
x++;
}
else {
lcd.setCursor(9, 2);
lcd.print("Error");
delay(2000);
lcd.clear();
page_Fwd();
}
}
// ==============================================================================
// || PAGE REVERSE ||
// ==============================================================================
void page_Rev() { // print menu
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("Distance in Feet");
lcd.setCursor(6, 1);
lcd.print(" 1 to 9");
lcd.setCursor(9, 2);
lcd.blink();
char Reverse = customKeypad.waitForKey();
if (Reverse >= '0' && Reverse <= '9') { // weed out unavailable input
runDur[x] = Reverse - '0';
runDir[x] = 'B';
lcd.noBlink();
runOrder[x] = runDir[x];
x++;
} else {
lcd.setCursor(9, 2);
lcd.print("Error");
delay(2000);
currPage = Rev;
}
}
// ==============================================================================
// || PAGE LEFT TURN ||
// ==============================================================================
void page_LTurn() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Direction in Degrees");
lcd.setCursor(6, 1);
lcd.print(" 1 to 359");
lcd.setCursor(9, 2);
lcd.blink();
do {
char key = customKeypad.waitForKey();
if (key >= '0' && key <= '9') // weed out unavailable input
LeftTurn = key -'0';
runDur[x] += LeftTurn ;
lcd.print (runDur[x]);
count++;
currPage = LTurn;
}
while (count >= 0 && count <=2);
delay(1000);
runDir[x] = 'L';
lcd.noBlink();
runOrder[x] = runDir[x];
lcd.clear();
lcd.setCursor(0, 0);
lcd.print (runDur[x]);
lcd.setCursor(0, 1);
lcd.print (runDir[x]);
lcd.setCursor (0, 2);
lcd.print (runOrder[x]);
delay(2000);
x++;
cleanUp();
currPage = Main_Menu;
}
// ==============================================================================
// || PAGE RIGHT TURN ||
// ==============================================================================
void page_RTurn() {
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("Direction in Degrees");
lcd.setCursor(6, 1);
lcd.print(" 1 to 359");
lcd.setCursor(9, 2);
lcd.blink();
char LeftTurn = customKeypad.waitForKey();
if (RightTurn >= '0' && RightTurn <= '9') { // weed out unavailable input
runDur[x] = RightTurn - '0';
runDir[x] = 'R';
lcd.noBlink();
runOrder[x] = runDir[x];
x++;
} else {
lcd.setCursor(9, 2);
lcd.print("Error");
delay(2000);
currPage = RTurn;
}
}
// ==============================================================================
// || PAGE PAUSE ||
// ==============================================================================
void page_Pause() {}
// ==============================================================================
// || PAGE BEEP ||
// ==============================================================================
void page_Beep() {}
// ==============================================================================
// || PAGE RUN called from menus ||
// ==============================================================================
void page_Run() {
lcd.clear();
lcd.setCursor(4, 0);
lcd.print("RUN PROGRAM");
delay(2000);
while (runOrder[d] != null){
if (runDir[d] == 70) { //ascii for F
runDur[d] = (runDur[d] * 2000);
forwardRun();
d = (d + 1);
} else if (runDir[d] == 66) { //ascii for B
runDur[d] = (runDur[d] * 2000);
reverseRun();
d = (d + 1);
} else if (runDir[d] == 76) { //ascii for L
runDur[d] = (runDur[d] * 2000);
LTurnRun();
d = (d + 1);
} else if (runDir[d] == 82) { //ascii for R
runDur[d] = (runDur[d] * 2000);
RTurnRun();
d = (d + 1);
} else {
lcd.clear();
lcd.setCursor(8,1);
lcd.print("DONE");
delay(2000);
break;
}
}
cleanUp();
currPage = Main_Menu;
}
// ==============================================================================
// || CLEAN UP ||
// ==============================================================================
void cleanUp(){
lcd.clear();
lcd.setCursor(6,0);
lcd.print("CLEAN UP");
delay(2000);
lcd.clear();
lcd.setCursor(6,0);
lcd.print (runDir[2]);
lcd.setCursor(6,1);
lcd.print (runDur[2]);
lcd.setCursor(6,2);
lcd.print (runOrder[2]);
lcd.setCursor(6,3);
lcd.print (x);
lcd.print(d);
delay(2000);
memset(runDir, '\0', sizeof(runDir)); // clear arrays
memset(runDur, '\0', sizeof(runDur));
memset(runOrder, '\0', sizeof(runOrder));
count = 0;
lcd.clear();
lcd.blink();
lcd.setCursor(6,0);
lcd.print (runDir[2]);
lcd.setCursor(6,1);
lcd.print (runDur[2]);
lcd.setCursor(6,2);
lcd.print (runOrder[2]);
lcd.setCursor(6,3);
lcd.print (x);
lcd.print(d);
delay(2000);
lcd.noBlink();
}
// ==============================================================================
// || PAGE forward run called from Run ||
// ==============================================================================
void forwardRun() {
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("FORWARD ");
digitalWrite(motor1pin1, LOW); // turn motor on
digitalWrite(motor1pin2, HIGH);
digitalWrite(motor2pin1, HIGH);
digitalWrite(motor2pin2, LOW);
Time = runDur[t];
delay(Time); // TIME MOTOR RUNS
digitalWrite(motor1pin1, LOW); // turn motor off
digitalWrite(motor1pin2, LOW);
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, LOW);
delay(250);
t = t + 1;
}
// ==============================================================================
// || PAGE reverse run called from Run ||
// ==============================================================================
void reverseRun() {
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("REVERSE ");
digitalWrite(motor1pin1, HIGH); // turn motor on
digitalWrite(motor1pin2, LOW);
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, HIGH);
Time = runDur[t];
delay(Time); // TIME MOTOR RUNS
digitalWrite(motor1pin1, LOW); // turn motor off
digitalWrite(motor1pin2, LOW);
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, LOW);
delay(250);
t = t + 1;
}
// ==============================================================================
// || PAGE LTurn run called from Run ||
// ==============================================================================
void LTurnRun() {
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("LEFT TURN ");
digitalWrite(motor1pin1, LOW); // turn motor on
digitalWrite(motor1pin2, HIGH);
digitalWrite(motor2pin1, HIGH);
digitalWrite(motor2pin2, LOW);
Time = runDur[0];
delay(Time); // TIME MOTOR RUNS
digitalWrite(motor1pin1, LOW); // turn motor off
digitalWrite(motor1pin2, LOW);
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, LOW);
delay(250);
}
// ==============================================================================
// || PAGE RTurn run called from Run ||
// ==============================================================================
void RTurnRun() {
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("RIGHT TURN ");
digitalWrite(motor1pin1, HIGH); // turn motor on
digitalWrite(motor1pin2, LOW);
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, HIGH);
Time = runDur[0];
delay(Time); // TIME MOTOR RUNS
digitalWrite(motor1pin1, LOW); // turn motor off
digitalWrite(motor1pin2, LOW);
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, LOW);
delay(250);
}`