0

My problem is nothing is display to my LCD when * is pressed. There are no errors in my code, but when I run it and press the * to enter set mode and update the time and temp, nothing is happening.

For my project, when the * is pressed, the code is supposed to enter set mode. And in set mode, you are supposed to enter the hour, minute, seconds, am/pm, and temperature from sensor. After that, everything is supposed to update and show everything at the end. That isn't what is going on when I run the code. I have a nucleo board and am sure I have everything correctly wired. Just need help with my code that is provided.

#include <iostream>
#include <string>
#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 = temperature * 10 + (key - '0');
            lcd.printf("%.2f", temperature);
        } else if (key == '#') {
            // Temperature set
            break;
        }
    }
}

2
  • Did you try to press the 'A' key to see if it functioned like the '*' key? Ie, can you verify pressing all keys is printing the key name to 'stdout', via std::cout << sequence;? It is difficult for anyone to know if you have the underlying hardware mapped correctly. For instance, an issue could be you need to drive column and read row, instead of the way you have done. Commented Apr 14, 2024 at 16:17
  • Additionally, it is common to add 'debounce' code. However, this is most likely not your issue, just something else that the code seems to lack. Commented Apr 14, 2024 at 16:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.