break and goto not working

I am trying to get the used to type in a number and when they press a key other then an integer, the program will assign that number to the age. However, for some reason the break command doesn’t seem to be working. I also tried to make it incremental and that didn’t work either.

#include <Keypad.h>

const byte ROWS = 4;
const byte COLS = 4;

char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

void setup() {
  Serial.begin(9600);
  Serial.print("Age: ");
  int i = 1;
  char age = '0';
  while (i = 1) {
    char customKey = customKeypad.getKey();

    //    if (customKey != 'A' && customKey != 'B' && customKey != 'C' && customKey != 'D' && customKey != '*' && customKey != '#' ) {
    //      Serial.print(customKey);
    //    }
    if (customKey == 'A' || customKey == 'B' || customKey == 'C' || customKey == 'D' || customKey == '*' || customKey == '#')
    {
      age = customKey;
      break;
    }
  }
  Serial.println(age);

}

I’m quite sure that the break works; but you assign customKey to age when customKey is a non-digit.

You might want to try this

    if (customKey == 'A' || customKey == 'B' || customKey == 'C' || customKey == 'D' || customKey == '*' || customKey == '#')
    {
      break;
    }
    else
    {
      age = customKey;
    }

It should at least allow you to get the numbers 0…9 for the age. If you need more than that, you’re code needs a bit more :wink:

And this is a bug

while (i = 1)

The difference between = and == :wink:

// Edit
And a comment:
don’t use goto; with properly written code, there is no need.

@masher

I have tested your program without any change except adding few lines to blink L. Your program assigns correct value A/B/C/D/*/# to the variable age as you have wanted in your original post – “when they press a key other then an integer, the program will assign that number to the age.”. The break statement also works well.

Copy of your Codes:

#include <Keypad.h>

const byte ROWS = 4;
const byte COLS = 4;

char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

void setup() 
{
  Serial.begin(9600);
  Serial.print("Age: ");
  pinMode(13, OUTPUT);
  int i = 1;
  char age = '0';
  while (i == 1) {   //change + to == in the light of Post#1
    char customKey = customKeypad.getKey();

    //    if (customKey != 'A' && customKey != 'B' && customKey != 'C' && customKey != 'D' && customKey != '*' && customKey != '#' ) {
    //      Serial.print(customKey);
    //    }
    if (customKey == 'A' || customKey == 'B' || customKey == 'C' || customKey == 'D' || customKey == '*' || customKey == '#')
    {
      age = customKey;
      break;
    }
  }
  Serial.println(age);

}

void loop()
{
  digitalWrite(13, !digitalRead(13));
  delay(1000);
}

@sterretje
I have tried/tested your codes; unfortunately, it does not show any result (except the message Age:) on the Serial Monitor. When the key A/B/C/D/*/# is pressed down, the break statement takes place.

#include <Keypad.h>

const byte ROWS = 4;
const byte COLS = 4;

char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

void setup()
{
  Serial.begin(9600);
  Serial.print("Age: ");
  pinMode(13, OUTPUT);
  int i = 1;
  char age = '0';
  while (i == 1) 
  {  //change = to == in the light of Post#1
    char customKey = customKeypad.getKey();
    if (customKey == 'A' || customKey == 'B' || customKey == 'C' || customKey == 'D' || customKey == '*' || customKey == '#')
    {
      break;
    }
    else
    {
      age = customKey;
    }

    /*
        //    if (customKey != 'A' && customKey != 'B' && customKey != 'C' && customKey != 'D' && customKey != '*' && customKey != '#' ) {
        //      Serial.print(customKey);
        //    }
        if (customKey == 'A' || customKey == 'B' || customKey == 'C' || customKey == 'D' || customKey == '*' || customKey == '#')
        {
          age = customKey;
          break;
        }
      }
    */
  }
    Serial.println(age);

  }

  void loop()
  {
    digitalWrite(13, !digitalRead(13));
    delay(1000);
  }

I overlooked the NO_KEY

    if (customKey == 'A' || customKey == 'B' || customKey == 'C' || customKey == 'D' || customKey == '*' || customKey == '#')
    {
      break;
    }
    else
    {
      if (customKey != NO_KEY)
      {
        age = customKey;
      }
    }

That should do.