0

I have a keypad like this:

here is another, closer look at the bottom part.

Basically, I want the LED to power the amount that I pressed.

this is my code:

int LED = 1;
int inputs[4] {
  6,
  4,
  2,
  0
};
int outputs[4] {
  13,
  12,
  10,
  8
};
void outputAllBut(short int);
int numberInputCheck();
int inputCheck();

void setup() {
  for (int i = 0; i < 4; i++) {
    pinMode(outputs[i], OUTPUT);
    pinMode(inputs[i], INPUT_PULLUP);
  }
  pinMode(LED, OUTPUT);
}

void loop() {
  while (true) {
    int x = numberInputCheck();

    if (x == 500) {
      break;
    }

    delay(500);

    for (int i = 0; i < x; i++) {
      digitalWrite(LED, HIGH);
      delay(500);
      digitalWrite(LED, LOW);
      delay(300);
    }
  }
}

int numberInputCheck() {
  while (true) {
    for (int i = 0; i < 4; i++) {
      outputAllBut(i);
      for (int m = 0; m < 4; m++) {
        if (digitalRead(inputs[m]) == LOW) {
          switch (i) {
          case 0:
            if (m == 3) {
              return 500;
            } else {
              return m + 7;
            }
          case 1:
            if (m == 3) {
              return 500;
            } else {
              return m + 4;
            }
          case 2:
            if (m == 3) {
              return 500;
            } else {
              return m + 1;
            }
          case 3:
            return 500;
          }
        }
      }
    }
  }
}

void outputAllBut(short int b) {
  for (int i = 0; i < 4; i++) {
    if (i != b) {
      digitalWrite(outputs[i], HIGH);
    }
  }
}

int inputCheck() {
  for (int i = 0; i < 4; i++) {
    if (digitalRead(inputs[i]) == LOW) {
      return i;
    }
  }
  return 5;
}

for reference, the numberInputCheck function is the one that might go wrong here, I want it to wait for an input and return the number that I pressed. i is the rows and m is the columns. My problem is that whenever I press the keypad at one of the numbers, it will give me a random amount of times that the LED turns on, there doesn't seem to be any pattern.

m+1, m+4 & m+7 is because m is my columns so if I press 0 and the row is 0 then I add 1, same goes with everything else. Can anyone help me?

1

Your outputAllBut function is wrong. It turns all but the specified output high, but it doesn't turn the specified output low. As a result, after calling it multiple times, you end up with all of the outputs high forever (since you don't have code anywhere else that would turn them off), so it's just a question of timing which one it considered "on" any time you press a button.

Side note: toggling the outputs between low and high to do the matrix read is dangerous, as pressing multiple buttons at a time can cause a short circuit that way. You should instead always keep all of them low, and control which one is active by setting it to output mode and the rest to input mode.

  • your answer helped the situation but I'm still getting mixed signals. my button 2 just pressed only once for some reason – ArchyInUse Oct 20 '18 at 17:18

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.