Page 1 of 1

Modify this arduino c code with char function key to boolean key: This is the code for char function: const int numRows=

Posted: Fri Apr 29, 2022 7:05 am
by answerhappygod
Modify this arduino c code with char function key to
boolean key:
This is the code for char function:
const int numRows=4;
const int numCols=3;
const char keymap[numRows][numCols]= {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
const int rowPins[numRows]={0,1,2,3};
const int colPins[numCols]={6,5,4};
void setup() {
Serial.begin(9600);
for (int row=0; row<numRows; row++)
{
pinMode(rowPins[row],OUTPUT);
digitalWrite(rowPins[row],HIGH);
}
for(int column=0; column<numCols; column++)
{
pinMode(colPins[column],INPUT_PULLUP);
digitalWrite(colPins[column],HIGH);
}
}
void loop() {
char key= getKey();
if(key !=0){
Serial.print("Got key ");
Serial.println(key);
}
}
char getKey()
{
char key=0;
for(int row=0; row<numRows; row++)
{
digitalWrite(rowPins[row],LOW);
for(int column=0; column<numCols;column++)
{
if(digitalRead(colPins[column])==LOW)
{
delay(1000);
while(digitalRead(colPins[column])==
LOW);
key= keymap[row][column];
}
}
digitalWrite(rowPins[row],HIGH);
}
return key;
}
The boolean code for the get function should look
something similar to this:
Modify This Arduino C Code With Char Function Key To Boolean Key This Is The Code For Char Function Const Int Numrows 1
Modify This Arduino C Code With Char Function Key To Boolean Key This Is The Code For Char Function Const Int Numrows 1 (5.42 KiB) Viewed 25 times
Modify This Arduino C Code With Char Function Key To Boolean Key This Is The Code For Char Function Const Int Numrows 2
Modify This Arduino C Code With Char Function Key To Boolean Key This Is The Code For Char Function Const Int Numrows 2 (13.09 KiB) Viewed 25 times
Please make sure to write the full c code for the get
boolean function.
Thank you.
void loop() { bool key_pressed=false; key_pressed = get_key(); if (key_pressed==true) { Serial.print (keys[row][col]); } }
bool get_key (void) { uint8_t x=0; for (row=0; row < no_rows ; row++) { // set rowPins[row] to 0, all other output pins to 1 for (col=0; col<no_cols; col++) { //read colPins [col] into x, if x is 0 wait for x to be high (pin released!) //retuen true (return true will exit the functon!) } } return false; //no key is pressed }