0
#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'E'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
int count = 0; char num[4];
char key;
byte rowPins[ROWS] = {A0, A1, A2, A3}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6, 5, 4, 3}; //connect to the column pinouts of the keypad

char adhaar[4][4] = {
  {'1', '0', '8', '3'},
  {'9', '1', '6', '0'},
  {'5', '7', '6', '1'},
  {'6', '7', '9', '3'}
};

char adhaar0[4] = {'1', '0', '8', '3'};
char adhaar1[4] = {'9', '1', '6', '0'};
char adhaar2[4] = {'5', '7', '6', '1'};
char adhaar3[4] = {'6', '7', '9', '3'};
boolean check, control, adhar, tag_status;
int i = 0, j, p, q;

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
  Serial.begin(9600);
  Serial.println(" enter your adhaar ");
}

void loop() {
  getkey();
  checkkey();
}

void getkey() {
  while (i < 4) {
    char key = keypad.getKey();
    if (key) {
      num[i] = key;
      Serial.print(num[i]);
      i++;
    }
  }
}


void checkkey() {
  if (i == 4) {
    control = true;
    for (p = 0; p < 4; p++) {
      for (q = 0; q < 4; q++) {
        if (adhaar[p][q] == num[q]) {
          check = true;
          Serial.println(num[q]);
        } else {
          check = false;
          Serial.println(" not eligile");
          break;
        }
      }
      if (check == true) {
        adhar = true;
        Serial.println(" cast your vote");
      }
    }
    i = 0;
  }
}

My problem is once the adhaar matches with the values initialised in the array I have, it's going to "cast your vote" as expected, but after that it's again executing "not eligible" 3 times.

  • 2
    What was the output? What did you expect it to be? Please edit your question. – Craig May 3 '18 at 15:05
  • My problem is once the adhaar matches with the values initialised in the array i have taken, it's going to "cast your vote" as expected, but after that its again executing "not eligible" 3 times – Renuka A Rajur May 3 '18 at 15:09
  • please someone help me to solve this problem...thank you – Renuka A Rajur May 3 '18 at 15:18
  • 2
    Do not answer in a comment, please edit your question! – Edgar Bonet May 3 '18 at 15:19
  • 1
    Also please just post the output, rather then trying to explain it – Chad G May 3 '18 at 15:22
3

First, a general remark: most of your variables are never used. You should get rid of them, as they only serve to make your code more obscure.

Then, the reason for the observed behavior is that you are comparing the entered code with 4 predefined codes. Whatever the user enters, there will be at least 3 non-matches, hence 3 times the program prints “not eligile”.

To get the behavior you expect, I would get rid all all those boolean variables that make the logic hard to follow, and instead break down the problem into smaller chunks, each implemented in its own small function. This makes it easier to write code that is easy to understand.

First, how do you tell whether two 4-character codes are the same? Like this:

// Do codes a and b match?
bool match(char a[4], char b[4]) {
    for (int i = 0; i < 4; i++) {
        if (a[i] != b[i])
            return false;
    }
    return true;
}

Then, given a code entered by the user, tell whether it matches adhaar[0] or adhaar[1] or... or none of them:

// Return the adhaar index matching the given key,
// or -1 if there is no match.
int matching_idx(char key[4]) {
    for (int i = 0; i < 4; i++) {
        if (match(key, adhaar[i]))
            return i;
    }
    return -1;  // no match
}

Now your checkkey() function can be rewritten as follows:

void checkkey() {
    if (i != 4) return;  // should never happen
    if (matching_idx(num) == -1)
        Serial.println(" not eligile");
    else
        Serial.println(" cast your vote");
    i = 0;
}
| improve this answer | |
  • when I try this, its a infinite loop.....and its not going to next loop – Renuka A Rajur May 4 '18 at 3:58
  • @RenukaARajur: It goes into an infinite loop because I forgot to clear the global i variable in checkkey(). Fixed in the edit. Note that this way of using the global i is very poor design in the first place: this should instead be a counter local to getkey(), as in for (int i = 0; i < 4; i++) .... – Edgar Bonet May 4 '18 at 9:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.