Question: Can you help me fix my code! When the * is pressed, the code is suppposed to go into set mode. And during set mode,

Can you help me fix my code! When the * is pressed, the code is suppposed to go into set mode. And during set mode, the hour, minute, second, am/pm, and temp are to be updated. I need the coding solutions to my code not explaining to me what I need to do. I'm using a nucleo board, 16x2 LCD, potentiometer, and LM35 temp sensor. My code is below!
#include
#include
#include "mbed.h"
#include "TextLCD.h"
// Define the keypad layout
char key_map[4][4]={
{'1','2','3','A'},//1st row
{'4','5','6','B'},//2nd row
{'7','8','9','C'},//3rd row
{'*','0','#','D'}//4th row
};
// Define the GPIO pins connected to the keypad
#define ROW1_PIN PA_6
#define ROW2_PIN PA_7
#define ROW3_PIN PB_6
#define ROW4_PIN PC_7
#define COL1_PIN PA_9
#define COL2_PIN PA_8
#define COL3_PIN PB_10
#define COL4_PIN PB_4
// Define the GPIO pins connected to the LCD
#define RS_PIN PB_5
#define E_PIN PB_4
#define D4_PIN PC_7
#define D5_PIN PB_6
#define D6_PIN PA_7
#define D7_PIN PA_6
// Function prototypes
void initialize_gpio();
void set_row(int row);
void reset_rows();
char keypad_scan();
int col_scan();
void update_time_lcd();
void set_mode();
void update_temperature_lcd();
// Global variables for GPIO pins
DigitalOut ROW1(ROW1_PIN);
DigitalOut ROW2(ROW2_PIN);
DigitalOut ROW3(ROW3_PIN);
DigitalOut ROW4(ROW4_PIN);
DigitalIn COL1(COL1_PIN, PullUp);
DigitalIn COL2(COL2_PIN, PullUp);
DigitalIn COL3(COL3_PIN, PullUp);
DigitalIn COL4(COL4_PIN, PullUp);
TextLCD lcd(RS_PIN, E_PIN, D4_PIN, D5_PIN, D6_PIN, D7_PIN);
int main(){
initialize_gpio();
std::string sequence;
while (true){
char key = keypad_scan();
sequence += key;
std::cout << sequence;
if (key =='*'){
set_mode(); // Enter Set Mode when * is pressed
}
}
return 0;
}
void initialize_gpio(){
ROW1= ROW2= ROW3= ROW4=1;
}
void set_row(int row){
switch (row){
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;
}
}
void reset_rows(){
ROW1= ROW2= ROW3= ROW4=1;
}
char keypad_scan(){
char key_pressed ='\0';
for (int row =0; row <4; ++row){
set_row(row);
int col = col_scan();
if (col !=-1){
key_pressed = key_map[row][col];
break;
}
}
reset_rows();
return key_pressed;
}
int col_scan(){
if (!COL1) return 0;
if (!COL2) return 1;
if (!COL3) return 2;
if (!COL4) return 3;
return -1;
}
void update_time_lcd(){
}
void set_mode(){
// Prompt user to set hour
lcd.cls();
lcd.printf("HOUR");
int hour =0;
while (true){
char key = keypad_scan();
if (key >='0' && key <='9'){
hour = hour *10+(key -'0');
lcd.printf("%d", hour);
} else if (key =='#'){
if (hour >=1 && hour <=12){
break;
} else {
lcd.cls();
lcd.printf("ERROR");
lcd.cls();
lcd.printf("HOUR");
hour =0;
}
}
}
// Prompt user to set minute
lcd.cls();
lcd.printf("MIN");
int minute =0;
while (true){
char key = keypad_scan();
if (key >='0' && key <='9'){
minute = minute *10+(key -'0');
lcd.printf("%d", minute);
} else if (key =='#'){
if (minute >=0 && minute <=59){
break;
} else {
lcd.cls();
lcd.printf("ERROR");
lcd.cls();
lcd.printf("MIN");
minute =0;
}
}
}
// Prompt user to set seconds
lcd.cls();
lcd.printf("SEC");
int second =0;
while (true){
char key = keypad_scan();
if (key >='0' && key <='9'){
second = second *10+(key -'0');
lcd.printf("%d", second);
} else if (key =='#'){
if (second >=0 && second <=59){
break;
} else {
lcd.cls();
lcd.printf("ERROR");
lcd.cls();
lcd.printf("SEC");
second =0;
}
}
}
// Prompt user to set temperature
lcd.cls();
lcd.printf("TEMP");
float temperature =0.0f;
while (true){
char key = keypad_scan();
if (key >='0' && key <='9'){
temperature = tempera

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!