Clear the array and re-store the new value in array

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
}

Your topic was MOVED to its current forum category which is more appropriate than the original as it has nothing to do with Installation and Troubleshooting of the IDE

Why not--?

arrayIndex = 0;
//return 0;
1 Like

since @GolamMostafa has found your error, instead of the above, consider (see ASCII)


   if ('0' <= customKey || customKey <= '9')

and instead of

consider doing following with each numeric key press

guess= 10*val + customkey-'0';

where guess is a global and is process/reset when '*' or '#' are pressed

For a full explanation see Keypad data entry. A beginners guide

Why is your array is of type int?

A keypress ( 0 to 9) in the Keypad returns ASCII code which being subtracted by '0' gives a byte value. For example: pressing Key-9 gives 0x09 (0x39 - '0' = 0x39 - 0x30 = 0x09).

I think you want: if ('0' <= customKey && customKey <= '9')

yes, thanks

Is not the following a Logical Style?
if(customKey >= '0' && customKey <= '9')

It is much more logical (and readable) in my opinion, but there are those here who prefer to put the constant value first in comparisons so that where = is used by mistake instead of == the compiler reports an error

1 Like

wouldn't the following (if possible) be easier to understand
'0' <= customKey <= '9'

Yes, if I can read like this:

char customeKey;
if('0' <= customKey <= '9')
{
     Serial.println(customeKey);
}

Not for me

if (customKey >= '0' and <= '9')

would be better (for me)

If we are going to invent new syntax then why not

if (customKey between '0 and '9')

because i thing 0 <= x <= 9 is more mathematical

I prefer my code to be readable, almost in English if possible. The compiler can then work whatever magic it wants without bothering me

so you're suggesting 0 <= x <= 9 is less readable?

doesn't it depend on what your coding? i spent a lot of time writing code on signal processors.

Yes

Read it to me in English

if x is greater that 0 and less than 9

That is not reading it, rather it is interpreting it.

That is not the same thing at all

does "between" imply Inclusiveness?