0

I am trying to run a c program which will wait for me to hit a key on a 4x4 keypad and then display the number I have hit. The program I have bit runs but ends just as soon as I start it and I can't find my mistake.

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

 #define COL1     11
 #define COL2     10
 #define COL3     6
 #define COL4     5
 #define ROW1     4
 #define ROW2     3
 #define ROW3     2
 #define ROW4     1

 #define DET      0

 int colnum;
 int rownum;

 int keyinterrupt (void)
 {
      int col[] = {COL1, COL2, COL3, COL4};
      int row[] = {ROW1, ROW2, ROW3, ROW4};
      int reset;
      for(colnum = 0; colnum++; colnum < 4)
      {
           for(reset = 0; reset++; reset < 4)
           {
                digitalWrite(col[reset], 0);
           }

           digitalWrite(col[colnum], 1);

           for(rownum = 0; rownum++; rownum < 4)
           {
                if(digitalRead(row[rownum]))
                {return 0;}
           }
      }
 }

 int main (void)
 {
      pinMode(COL1, OUTPUT);
      pinMode(COL2, OUTPUT);
      pinMode(COL3, OUTPUT);
      pinMode(COL4, OUTPUT);
      pinMode(ROW1, INPUT);
      pinMode(ROW2, INPUT);
      pinMode(ROW3, INPUT);
      pinMode(ROW4, INPUT);

      if(wiringPiISR (DET, INT_EDGE_RISING, &keyinterrupt) < 0)
      {
           printf("Unable to setup ISR");
           return 1;
      }

      while(1)
      {
           while(!DET)
           {
                delay(1);
           }

           printf("The key pressed was at Col:%i Row:%i", colnum, rownum);
      }
      return 0;
 }
  • Add "\n" at the and of your "Unable to setup ISR" string to printf() and it be more clear on why your program ends its execution. Also note that your while(!DET) loop is an infinite loop since DET is defined to be 0. – Krzysztof Adamski May 6 '13 at 19:40
2

Your program is possibly segfaulting because have swapped the loop and post-loop statements while the function does not return a meaningful value. Consider:

int keyinterrupt (void)
 {
      int col[] = {COL1, COL2, COL3, COL4};
      int row[] = {ROW1, ROW2, ROW3, ROW4};
      int reset;
      for(colnum = 0; colnum++; colnum < 4)
      {
           // ...
      }
 }

colnum++ returns 0, so the contents of the loop is never executed. I guess you meant:

for (colnum = 0; colnum < 4; colnum++)

This function does not return any value. If you enabled compiler warnings (cc -Wall -Wextra), it would have warned you.

  • Thanks, I did notice this mistake. All my for loops have the conditions switched on them. However, the program still didn't perform the task. – Marmstrong May 7 '13 at 10:47
  • @Marmstrong What "does not not work"? Any error messages? Is the program segfaulting? – Lekensteyn May 7 '13 at 12:49
  • Sorry for the ambiguous reply. The program compiles with no errors but when it is run it just ends. It should wait for a keystroke and then display it, then wait for another keystroke. – Marmstrong May 7 '13 at 15:08
  • No output at all? Try adding puts("...."); on lines for debugging, substituting .... for your debug message (e.g. puts("BEFORE digitalWrite"); or `printf("BEFORE digitalWrite: %i:%i\n", colnum, reset) – Lekensteyn May 7 '13 at 15:58

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.