Variable won't update?!

Hey there. I'm trying to make a keyboard using a 16 key keypad. (yes that sounds stupid i know) and the varible for keeping track of my F1 - F4 keys wont update.

#include <Key.h>
#include <Keypad.h>

//keypad
const byte ROWS = 4;
const byte COLS = 4;

byte rowPins[ROWS] = {5, 4, 3, 2};
byte colPins[COLS] = {6, 7, 8, 9};
int currentKey = 1;

char commands[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'F1', 'F2', 'F3', 'F4'}
};

Keypad MyPad_command = Keypad(makeKeymap(commands), rowPins, colPins, ROWS, COLS);


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  if (currentKey == 1 ) {
      char result = MyPad_command.getKey();
    if (result == "F2") {
      currentKey = 2;
    } else if (result == "F3") {
      currentKey = 3; 
    } else if (result == "F4") {
      currentKey = 4;
    } else {
      if (result) {
        Serial.print(result);
        Serial.print(" current Key: ");
        Serial.println(currentKey);
      }

    }

  }

}

if F2 - F4 are pushed, the code should stop registering key presses, but that doesnt happen...

This clears 'result' each cycle.

You are comparing a character to a string literal.

Print the value of result so you can see what to compare it to properly.

Also these are not characters:

'F1', 'F2', 'F3', 'F4'}

a7

You can't compare a char to a string.

If result were a string. You cannot use == to compare strings. Use the strcmp() function.

Your code should have shown some compiler warnings to tell you of some mistakes. Turn on compiler warnings in the IDE in the File menu, Preferences. Warnings will allow the code to complile, but the code may not work right. You can't fix the warnings unless you know about them.

As mentioned, these are not characters. It might make sense to instead use something like '*', "#' or '.' in that row. The character '0' (zero) comes in amazingly handy, as well.

2 Likes

turns out changing "" to '' and F1 - F4 to #, $, %, and ^ worked.

It would be courteous to post the final working code for those that may find this post in the future.

full working code:


//librarys
#include <Key.h>
#include <Keypad.h>

//keypad
const byte ROWS = 4;
const byte COLS = 4;

byte rowPins[ROWS] = {5, 4, 3, 2};
byte colPins[COLS] = {6, 7, 8, 9};
int currentKey = 1;

char commands[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'#', '$', '%', '^'}
};

char letter1[ROWS][COLS] = {
  {'q', 'w', 'e', 'r'},
  {'a', 's', 'd', 'f'},
  {'z', 'x', 'c', 'v'},
  {'#', '$', '%', '^'}
};

char letter2[ROWS][COLS] = {
  {'t', 'y', 'u', 'i'},
  {'g', 'h', 'j', 'k'},
  {'b', 'n', 'm', '0'},
  {'#', '$', '%', '^'}
};

char letter3[ROWS][COLS] = {
  {'o', 'p', '1', '2'},
  {'l', '3', '4', '5'},
  {'6', '7', '8', '9'},
  {'#', '$', '%', '^'}
};

Keypad MyPad_command = Keypad(makeKeymap(commands), rowPins, colPins, ROWS, COLS);
Keypad MyPad_letter1 = Keypad(makeKeymap(letter1), rowPins, colPins, ROWS, COLS);
Keypad MyPad_letter2 = Keypad(makeKeymap(letter2), rowPins, colPins, ROWS, COLS);
Keypad MyPad_letter3 = Keypad(makeKeymap(letter3), rowPins, colPins, ROWS, COLS);


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  if (currentKey == 1 ) {
    char result = MyPad_command.getKey();
    if (result == '$') {
      currentKey = 2;
    } else if (result == '%') {
      currentKey = 3; 
    } else if (result == '^') {
      currentKey = 4;
    } else {
      if (result) {
        Serial.println(result);
      }

    }

  } else if (currentKey == 2 ) {
    char result = MyPad_letter1.getKey();
    if (result == '#') {
      currentKey = 1;
    } else if (result == '%') {
      currentKey = 3; 
    } else if (result == '^') {
      currentKey = 4;
    } else {
      if (result) {
        Serial.println(result);
      }

    }
    
  } else if (currentKey == 3 ) {
    char result = MyPad_letter2.getKey();
    if (result == '#') {
      currentKey = 1;
    } else if (result == '$') {
      currentKey = 2;
    } else if (result == '^') {
      currentKey = 4;
    } else {
      if (result) {
        Serial.println(result);
      }

    }
    
  } else if (currentKey == 4 ) {
    char result = MyPad_letter3.getKey();
    if (result == '#') {
      currentKey = 1;
    } else if (result == '$') {
      currentKey = 2;
    } else if (result == '%') {
      currentKey = 3;
    } else {
      if (result) {
        Serial.println(result);
      }

    }

  }
}

1 Like

I take it you have no use for the digit '0'.

yes, because i used it elsewhere

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.