1

i am having a very hard time finding a purely C program for the Raspberry Pi and the 4x4 membrane matrix keypad. i can find python, c#, stamp, c++ and a couple java. however, C is completely missing from my 1/2 day's worth of google'ing.

any help would be greatly appreciated.

thanks.

2
  • Seems you have to migrate a running program to C by yourself? Main programming language on raspberry pi is python.
    – Ingo
    Commented Mar 23, 2018 at 11:45
  • 1
    i've wanted to learn C for a very long time. it'll be good practice and i'll try that. i did check out Python. i know it's a very capable language. but, i'd much rather "code to the metal". also, for me personally choosing Python over C is like choosing Visual Basic over C# for asp.net. it looks like a dirty tinker toy, relatively speaking. i'm just surprised because i have been able to find everything i've needed. it's just this one keypad. once i get my program working i'll answer my own question below. Commented Mar 23, 2018 at 12:10

2 Answers 2

2
#include <wiringPi.h>
#include <stdio.h>

#define ROWS 4
#define COLS 4

char pressedKey = '\0';
int rowPins[ROWS] = {1, 4, 5, 6};
int colPins[COLS] = {12, 3, 2, 0};

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

void init_keypad()
{
   for (int c = 0; c < COLS; c++)
   {
      pinMode(colPins[c], OUTPUT);   
      digitalWrite(colPins[c], HIGH);
   }

   for (int r = 0; r < ROWS; r++)
   {
      pinMode(rowPins[0], INPUT);   
      pullUpDnControl(rowPins[r], PUD_UP);
   }
}

int findLowRow()
{
   for (int r = 0; r < ROWS; r++)
   {
      if (digitalRead(rowPins[r]) == LOW)
         return r;
   }

   return -1;
}

char get_key()
{
   int rowIndex;

   for (int c = 0; c < COLS; c++)
   {
      digitalWrite(colPins[c], LOW);

      rowIndex = findLowRow();
      if (rowIndex > -1)
      {
         if (!pressedKey)
            pressedKey = keys[rowIndex][c];
         return pressedKey;
      }

      digitalWrite(colPins[c], HIGH);
   }

   pressedKey = '\0';

   return pressedKey;
}

int main(void) 
{
   wiringPiSetup();

   init_keypad();

   while(1)
   {
      char x = get_key();

      if (x)
         printf("pressed: %c\n", x);
      else
         printf("no key pressed\n");

      delay(250);
   }

   return 0;
}
1
  • This doesn't seem to work.. it just prints pressed: 1 all the time.
    – Joshua G
    Commented Jan 22, 2021 at 12:50
1

Please note it should be

pullUpDnControl(rowPins[r], PUD_UP);

in the place of

digitalWrite(rowPins[r], PUD_UP);

Sorry for submitting as Answer as I'm currently LOW on reputation for adding Comments.

1
  • It is indeed an answer. But you should refer to the answer from Mike Kangas and explain what's wrong with digitalWrite and better with pullUpControl.
    – Ingo
    Commented Feb 25, 2019 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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