Alright, so I have a new issue. And this one I REALLY cant figure out! I got the basics done, and I got some code written. But it gives me a weird output. Let me explain...
Alright so I got one of those membrane-matrix 4x4 keypads you can hook up to your Arduino or Rpi. I am using it with my pi.
The goal of this script is simple and that is that I just want to have it say which key I have pressed. Because once I have a nice, basic template I can proceed to make better and/or more complicated stuffs.
Anyways, I hook the keypad up to my GPIO pins, and I run this code:
Now from what I can see, this code would theoretically work perfectly. Cept' when I excecute the script, I am greeted with a continuous message of, "you pressed: 1" even when I am NOT pressing "1".
Yeah that's it! If I were to press anything else, it would still say "you pressed 1". At first I thought the problem was the actual keypad, so what I did was disconnect the row1 and column1... Still got the "you pressed 1". Next I tried to run the script without the fucking keypad attached at all! And guess what, I still get "you pressed 1"...
SO. I have come to conclusion of thought that it is not the keypad that is broken, it is my code. If it would be too much trouble, could someone possibly tell me what I am doing wrong or if this shit is even possible? lol. Thanks in advance B)
Alright so I got one of those membrane-matrix 4x4 keypads you can hook up to your Arduino or Rpi. I am using it with my pi.
The goal of this script is simple and that is that I just want to have it say which key I have pressed. Because once I have a nice, basic template I can proceed to make better and/or more complicated stuffs.
Anyways, I hook the keypad up to my GPIO pins, and I run this code:
Code:
#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] = {26, 27, 28, 29};
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("you pressed: %c\n", x);
else
printf("you have nothing pressed shit head\n");
delay(250);
}
return 0;
}
Now from what I can see, this code would theoretically work perfectly. Cept' when I excecute the script, I am greeted with a continuous message of, "you pressed: 1" even when I am NOT pressing "1".
Yeah that's it! If I were to press anything else, it would still say "you pressed 1". At first I thought the problem was the actual keypad, so what I did was disconnect the row1 and column1... Still got the "you pressed 1". Next I tried to run the script without the fucking keypad attached at all! And guess what, I still get "you pressed 1"...
SO. I have come to conclusion of thought that it is not the keypad that is broken, it is my code. If it would be too much trouble, could someone possibly tell me what I am doing wrong or if this shit is even possible? lol. Thanks in advance B)