Hey guys, first of all, I have little experience with arduino coding. So excuse my code if its ugly. Altho i do have some coding experience, i'm struggling on how to approach my dilemna without frying anything.
So the logic of the end goal is this: Have 2 arduino pro micro soldered together. One of them (i'll call it the receiver) is plugged in pc via usb, and the other is plugged in another device through usb to send keyboad keys (i'll call it the transmitter).
so code wise, here is what i'm trying to do: send keyboard keys to pro micro receiver via the serial monitor, then with that keyboard key info, the receiver finds matching pins in the hexa matrix, then activate those pins to emulate a keypad button press. Then the transmitter reads the pins activated by the receiver (like if a keypad button was pressed) and sends a keyboard key.
This is how I plan the wiring:
PC -> pro micro receiver -> pro micro transmitter -> other device
identical pins on the 2 pro micros are soldered together. So pin 2 of pro micro receiver is linked to pin 2 of pro micro transmitter. The same is true for pins 2,3,4,5,6,7,8,9,10,14,15,16. So both pro micros are powered by the usb cable of their respective device.
So far my code works, i managed to find info by reading online and trial/error. However, when its time to activate pins, i'm scared to do trial and error because actual current is activated on pins and i don't want to fry anything.
so far with the code i managed to read serial input and find the physical pins via the matrix. I'm just not sure on how to do the code that activate the pins and emulates a button press. I think i'm supposed to use digital write?
i did read this: How to Set Up a Keypad on an Arduino - Circuit Basics but i'm not sure if i understand it right. So from what i understand, all columns pins are HIGH by default, and all the row pins are LOW by default. So if u understand correctly, once i have the column and row pins, i have to set that column pin to LOW (which was high) and that row pin to High(which was low)?
I'm only worried about the receiver code for now.
Here is the code, for the receiver so far. Lets assume I send "Y" to the receiver via serial monitor.
#include <Keypad.h>
const byte ROWS = 6; //EIGHT rows
const byte COLS = 6; //EIHT columns
char inChar;
char colChar;
char rowChar;
byte matchCol;
byte matchRow;
byte row;
byte col;
//define the symbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
// 2 3 4 5 6 7 pins
{ 'KEY_0', 'KEY_1', 'KEY_2', 'KEY_3','KEY_4', 'KEY_5'}, // 8
{ 'KEY_6', 'KEY_7', 'KEY_8', 'KEY_9','KEY_Q', 'KEY_W'}, // 9
{ 'KEY_E', 'KEY_R', 'KEY_T', 'KEY_Y','KEY_U', 'KEY_I'}, // 10
{ 'KEY_O', 'KEY_P', 'KEY_A', 'KEY_S','KEY_D', 'KEY_F'}, // 16
{ 'KEY_G', 'KEY_H', 'KEY_J', 'KEY_K','KEY_L', 'KEY_Z'}, // 14
{ 'KEY_X', 'KEY_C', 'KEY_V', 'KEY_B','KEY_N', 'KEY_M'} // 15
};
// index 0 1 2 3 4 5
byte rowPins[ROWS] = {8, 9, 10, 16, 14, 15}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {2, 3, 4, 5, 6, 7}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void setup(){
Serial.begin(9600);
}
void GetKeyPins(){
inChar = Serial.read(); // reads the key "Y"
for (row = 0; row < 6; row++)
{
for (col = 0; col < 6; col++)
{
if (hexaKeys[row][col] == inChar) // When it finds "Y" in the matrix it gives the array index
{
matchCol = col; //sets matchCol to 3 which is the column array index
matchRow = row; // sets matchRow to 2 which is the row array index
}
}
}
}
void SendPins(){
digitalWrite(colPins[matchCol], LOW ); //in our example, colPins[matchCol] would be colPins[3] which represents pin 5. So this sets pin 5 to LOW
digitalWrite(colPins[matchRow], HIGH );//in our example, rowPins[matchRow] would be rowPins[2] which represents pin 10. So this sets pin 10 to HIGH
delay(50);
digitalWrite(colPins[matchCol], HIGH ); //sets the pin back to what it was
digitalWrite(colPins[matchRow], LOW ); //sets the pin back to what it was
// this function should emulate pressing button at pin 5 and pin 10
}
void loop() {
if (Serial.available() > 0) {
GetKeyPins(); // gets the array index of column and row of the key i sent via serial monitor, "Y" in our example
delay(50);
SendPins(); // uses the arrays indexes found with GetKeyPins to emulate a button press
}
}
In the code, I commented what I think each function/line of code does. Feel free to correct me if i'm wrong.
So my question is: Is the code above correct? Do i have to declare pins with pinmode or does keypad.h sets columns pins to high and row pins low by default?
Thank you if you read all that, and if something isn't clear ask away. I'll do my best to clarify.