1

I've been recently working with an Arduino - trying to develop a program. The program should find which pins a keypad button is connected to in order for it to be easier to use libraries such as "keypad". I don't need to use a multimeter.

I'm currently working with a Megarduino and an LCD keypad shield.

This is the code that I have for scanning the keypad pins connected to the Arduino but I can't make it work.

There is previously a pin object array that has 9 pins with the following attributes: arduinoPin, scannedPin, basePin

for (int kpdBasePin=2; kpdBasePin<NUMPINS; kpdBasePin++)
{
    scanPins(kpdBasePin);    // scan all pins less than kpdBasePin.
}

void scanPins(int baseKpdPin)
{
    // Set base_pin output to LOW to begin scan process.
    int base = kpdPin[baseKpdPin].arduinoPin;
    pinMode(base, OUTPUT);
    digitalWrite(base, LOW);

    // Scan all pins up to, but not including, the base_pin. A LOW indicates a
    // key is being pressed.
    for (int scannedKpdPin = 1; scannedKpdPin < baseKpdPin; scannedKpdPin++)
    { 
        // Created a local variable for readability.
        int arduinoPin = kpdPin[scannedKpdPin].arduinoPin;
        int keyState = !digitalRead(arduinoPin);

        if (keyState)
        {
            storePins(scannedKpdPin, base);    //method to store both pins
        }
    }

    // End pin scanning process.
    digitalWrite(base, HIGH);
    pinMode(base, INPUT_PULLUP);
}

Is there something wrong with this code?

Giulio Vian
  • 8,248
  • 2
  • 33
  • 41
  • Sorry but I could not understand what you are truying to achieve and why it is not working as you expected... Can you explain it better, please? – frarugi87 Jul 16 '15 at 09:22
  • Sorry, I'm new to arduino and to stackoverflow... what I'm trying to do is to let anybody connect a membrane (keypad) to the arduino, run the program, and have an lcd showing which pair of pins is connected to a button that the person is pressing in the membrane. scanPins objective is to set an arduino pin to low and then check the pins before to see if a connection is being made, then store the information. Did I explain myself better? – carlos lopez Jul 16 '15 at 15:05
  • Ok, now it's much more easy to understand what you want... You still haven't explained why it's failing... In my opinion this should do what you wanted.. – frarugi87 Jul 16 '15 at 15:19
  • Im not sure... as I upload the program to the arduino, I don't even get to press a button and the if(keystate) condition is fulfilled somehow... – carlos lopez Jul 16 '15 at 15:30
  • Did you initialize all the pins to inputs WITH pullup? And what are the pins he thinks are pressed? – frarugi87 Jul 16 '15 at 15:31
  • mostly every pair of pins, it fulfills the condition practically everytime, I haven't checked exactly the pairs that it prints. about initializing the pins... I just declare the pin array previously like this: `kpdPin[1].arduinoPin = 32; kpdPin[2].arduinoPin = 34; kpdPin[3].arduinoPin = 36; kpdPin[4].arduinoPin = 38;` and the first time I use pinMode is the one shown in scanPins() – carlos lopez Jul 16 '15 at 15:43
  • try to put `pinMode(kpdPin[#].arduinoPin, INPUT_PULLUP);` in the setup function; of course `#` is every number you have – frarugi87 Jul 16 '15 at 15:45
  • Thank you, I've changed it and I'll try it as soon as possible. Unfortunately, I need to first fix a problem with my arduino, for some hours I've been receiving problems while uploading a sketch. I'll try it when I fix it. – carlos lopez Jul 16 '15 at 16:05

0 Answers0