Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

privided is my code, what do i change my code in order to make the led 1 turn on when code 2 7 5 3

privided is my code, what do i change my code in order to make the led1 turn on when code 2753# is entered and led1 off when * is entered. here is my code:
#include
#include
#include "mbed.h"
#define NUM_ROWS 4
#define NUM_COLS 4
#define ROW_ACTIVE_TIME 5ms
// Define the key map
char key_map[NUM_ROWS][NUM_COLS]={
{'1','2','3','A'},//1st row
{'4','5','6','B'},//2nd row
{'7','8','9','C'},//3rd row
{'*','0','#','D'}//4th row
};
// Define the LED
DigitalOut LED1(LED1);
DigitalOut ROW1(PA_6);
DigitalOut ROW2(PA_7);
DigitalOut ROW3(PB_6);
DigitalOut ROW4(PC_7);
DigitalIn COL1(PA_9, PullUp);
DigitalIn COL2(PA_8, PullUp);
DigitalIn COL3(PB_10, PullUp);
DigitalIn COL4(PB_4, PullUp);
// Function to scan the columns
int col_scan(){
if (!COL1)
return 0;
else if (!COL2)
return 1;
else if (!COL3)
return 2;
else if (!COL4)
return 3;
else
return -1;
}
// Function to scan the keypad
char keypad_scan(){
static char sequence[6]={0}; // Initialize sequence with zeros
static int seq_index =0;
for (int i =0; i <4; i++){
// Select the current row
switch (i){
case 0:
ROW1=0; ROW2=1; ROW3=1; ROW4=1;
break;
case 1:
ROW1=1; ROW2=0; ROW3=1; ROW4=1;
break;
case 2:
ROW1=1; ROW2=1; ROW3=0; ROW4=1;
break;
case 3:
ROW1=1; ROW2=1; ROW3=1; ROW4=0;
break;
}
// Poll for key press
int col = col_scan();
if (col !=-1){
// Key pressed
char key = key_map[i][col];
if (seq_index <5){// Keep track of the sequence until it reaches 5 characters
sequence[seq_index++]= key;
}
if (seq_index ==5){// Check if the sequence matches "2753#"
if (strcmp(sequence,"2753#")==0){
LED1=1; // Turn on LED1
}
}
if (key =='*'){// Check if "*" is pressed
LED1=0; // Turn off LED1
}
return key;
}
// Delay to stabilize
ThisThread::sleep_for(5ms);
}
// No key pressed
return '\0';
}
int main(){
while (1){
char key = keypad_scan();
if (key !='\0'){
printf("Key pressed: %c\r
", key);
// Do something with the key
}
ThisThread::sleep_for(100ms); // Adjust the delay as needed
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Nested Relations And Complex Objects In Databases Lncs 361

Authors: Serge Abiteboul ,Patrick C. Fischer ,Hans-Jorg Schek

1st Edition

3540511717, 978-3540511717

More Books

Students also viewed these Databases questions