Here is the code which I did. I want a function that can return to the beginning of the void loop().
Now If I press the '#', the array had been set to empty which is what I expected. But it did not save the value to the array after I press the key again. As I know, the loop() will execute the function in order. So, I do not know why it is not let me put value into the array again after I empty it.
#include<Keypad.h>
const byte ROWS = 4;
const byte COLS = 4;
unsigned int time;
// Array to represent keys on keypad
char Keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
// Connections to Arduino
byte rowPins[ROWS] = {6, 7, 8, 9};
byte colPins[COLS] = {10, 11, 12, 13};
// Create keypad object
Keypad customKeypad = Keypad(makeKeymap(Keys), rowPins, colPins, ROWS, COLS);
int theArray[4] = { 0 };
byte arrayIndex = 0;
int guess = 0;
void setup()
{
Serial.begin(9600);
// Declare pins as Outputs
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
}
void loop() {
char customKey = customKeypad.getKey();
if (customKey)
{
Serial.println(customKey); // print the key i pressed
}
if (customKey == '1' || customKey == '2'|| customKey == '3'|| customKey == '4'|| customKey == '5'|| customKey == '6'|| customKey == '7'|| customKey == '8'|| customKey == '9'|| customKey == '0')
{
if (arrayIndex < 5) // If the array length smaller than 5, save the pressed value in array
{
theArray[arrayIndex] = customKey - '0';
arrayIndex++;
}
}
if (customKey == '*') // after the array is reach enough length, press '*' to print the array out
{
int guess = (1000 * theArray[0]) + (100 * theArray[1] + (10 * theArray[2]) + theArray[3]);
Serial.println(guess);
}
if (customKey == '#') // press '#' to set the array empty
{
memset(theArray, 0, sizeof(theArray));
arrayIndex == 0;
return 0;
}
// here I want a function that can return to the beginning of the void loop()
// The problem is after I press the '#', the array had been set to empty "0"
// But It did not save the value to the array after I press the kay again
}