0

I am trying to get the value of a key pressed and set it to return a value to use in a loop. In the code below the Print(t) does not show anything

#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char hexaKeys[ROWS][COLS] = {
    {'1', '2', '3', 'A'},
    {'4', '5', '6', 'B'},
    {'7', '8', '9', 'C'},
    {'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; 
byte colPins[ROWS] = {5, 4, 3, 2};    
Keypad customKeypad = Keypad(makeKeymap(hexaKeys),rowPins,colPins,ROWS,COLS;                                  

void setup() {
    Serial.begin(9600);
}

void loop()
{
    int t; 
    switch (customKeypad.getKey())
    {
    case 'A':
        t = 8;
        break;
    case '2':
        t = 9;
        break;
    }
    return t;
    Serial.print(t);
}
  • Please show your wiring – chrisl Nov 15 '19 at 20:39
4

You misunderstand the use of return. The loop function is declared void. That means it doesn't return anything. If you simply remove that return statement completely then your code will do what you want.

The return stops the loop. It throws away the value since the loop is defined as returning void, and code execution never reaches the print statement. Writing anything in a function after a return statement is useless.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.