4x4 membrane switch set up

조회 수: 5 (최근 30일)
Paul
Paul 2025년 2월 20일
답변: Nithin 2025년 2월 26일
I'm trying to wire a 4x4 membrane switch to be able to input a string of numbers. The problem is A.) don't have a lot of experience in matlab, and B.) all of the code libraries and stuff I've found for the switch is all based in the arduino software. Does anyone have any idea on how I can transfer it over or write my own?
here is a example of the arduino library:
#include Keypad.h
const byte ROWS = 4; // number of rows
const byte COLS = 3; // number of columns
char keys[ROWS][COLS] = {
{1,2,3},
{4,5,6},
{7,8,9},
{‘#’,0,*}
};
byte rowPins[ROWS] = {8, 7, 6, 5}; // row pinouts of the keypad R1 = D8, R2 = D7, R3 = D6, R4 = D5
byte colPins[COLS] = {4, 3, 2}; // column pinouts of the keypad C1 = D4, C2 = D3, C3 = D2
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup()
{
Serial.begin(9600);
}
void loop()
{
char key = keypad.getKey();
if (key ~= NO_KEY)
Serial.println(key);
}

답변 (1개)

Nithin
Nithin 2025년 2월 26일
Hi @Paul,
I am assuming that you are trying to interface a 4x4 membrane switch with an Arduino using MATLAB instead of Arduino Software.
Follow the below steps to write a script in MATLAB to capture the input from the membrane switch:
  • Use the "MATLAB Support Package for Arduino Hardware" to read keypad inputs directly in MATLAB. Install it via the Add-Ons section in MATLAB, then connect your 4x4 membrane switch to the Arduino based on your pin setup.
  • Define pin configurations in your MATLAB script and connect to the Arduino using the "arduino" function.
  • Initialize the digital pins by using the "configurePin" function and iterating through both the row and column pins. Then, set up an infinite loop to continuously monitor key presses.
  • In this loop, for each row pin, set it to "LOW" using the "writeDigitalPin" function and check each column pin for a "LOW" signal using the "readDigitalPin" function, indicating a key press. If a key is detected, display the corresponding key value and include a brief pause to handle debouncing. After checking each column, reset the row pin back to "HIGH" before moving to the next row.
To know more about the utilised functions, refer to the following documentations:
I hope this resolves your issue.

카테고리

Help CenterFile Exchange에서 MATLAB Support Package for Arduino Hardware에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by