Arduino char-variable problem

Hey there,

i have a problem with my Arduino program. It doesn't work with more than one digit in the char variable.
Is it possible to make it work in this configuration?
Thanks for the help.
Ps.: It's an project for school where we have to build an piano with 31 buttons.

#include <Keypad.h>
//Hier wird die größe des Keypads definiert
const byte COLS = 5; //5 Spalten
const byte ROWS = 8; //8 Zeilen
//Die Ziffern/Zeichen:
const 
char hexaKeys[ROWS][COLS]={
{'Cis3',  'C3',    '-',   'Cis1',   'E2'},
{'D3',    'H2',    '-',   'D1',     'Dis2'},
{'Dis3',  'B2',    '-',   'Dis1',   'D2'},
{'E3',    'A2',    '-',   'E1',     'Cis2'},
{'F3',    'Gis2',  '-',   'F1',     'C2'},
{'-',      'G2',   '-',   'Fis1',   'H1'},
{'-',      'Fis2', 'H0',  'G1',     'B1'},
{'-',      'F2',   'C1',  'Gis1',   'A1'}
};
byte colPins[COLS] = { 8, 7, 6 }; //Definition der Pins für die 3 Spalten
byte rowPins[ROWS] = { 5, 4, 3, 2 };//Definition der Pins für die 4 Zeilen
char Taste; //pressedKey entspricht in Zukunft den gedrückten Tasten
Keypad Tastenfeld = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); //Das Keypad kann absofort mit myKeypad angesprochen werden
void setup() {
Serial.begin(9600);
}

void loop() {
Taste = Tastenfeld.getKey(); //pressedKey entspricht der gedrückten Taste
if (Taste) { //Wenn eine Taste gedrückt wurde
Serial.print("Die Taste ");
Serial.print(Taste);
Serial.print("wurde gedrueckt");
Serial.println(); //Teile uns am Serial Monitor die gedrückte Taste mit
}
}

It doesn't work with more than one digit in the char variable.

How many feet can you get in one shoe? Of course you can only put one char in a char variable. That's no different from being able to store only one int in an int variable.

If you want to store more than one of something, you need an array.

What are you going to do with the value in the Taste variable apart from print it ?

Looking closer I see that you have 8 rows and 5 columns in the hexaKeys array but only 3 pins in the colPins array and 4 pins in the rowPins array

Is that deliberate ?

Yeah i forgot to change the colPins and the rowPins. But i already changed that. Also i made an array and now i can compile the program. But now the serial monitor shows strange things, you can see that on the screenshot. And at the moment i only print "Taste".

#include <Keypad.h>
//Hier wird die größe des Keypads definiert
const byte COLS = 5; //5 Spalten
const byte ROWS = 8; //8 Zeilen
//Die Ziffern/Zeichen:
const 
char *hexaKeys[]={
"Cis3",  "C3",    "-",   "Cis1",   "E2",
"D3",    "H2",    "-",   "D1",     "Dis2",
"Dis3",  "B2",    "-",   "Dis1",   "D2",
"E3",    "A2",    "-",   "E1",     "Cis2",
"F3",    "Gis2",  "-",   "F1",     "C2",
"-",      "G2",   "-",   "Fis1",   "H1",
"-",      "Fis2", "H0",  "G1",     "B1",
"-",      "F2",   "C1",  "Gis1",   "A1"
};
byte colPins[COLS] = { 12, 11, 10, 9, 8 }; //Definition der Pins für die 3 Spalten
byte rowPins[ROWS] = { 7, 6, 5, 4, 3, 2, 1, 0 };//Definition der Pins für die 4 Zeilen
char Taste; //pressedKey entspricht in Zukunft den gedrückten Tasten
Keypad Tastenfeld = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); //Das Keypad kann absofort mit myKeypad angesprochen werden
void setup() {
Serial.begin(9600);
}

void loop() {
Taste = Tastenfeld.getKey(); //pressedKey entspricht der gedrückten Taste
if (Taste) { //Wenn eine Taste gedrückt wurde
Serial.print("Die Taste ");
Serial.print(Taste);
Serial.print(" wurde gedrueckt");
Serial.println(); //Teile uns am Serial Monitor die gedrückte Taste mit
}
}

Keypad Tastenfeld = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

You have the Tastenfeld instance using pins { 12, 11, 10, 9, 8 } and { 7, 6, 5, 4, 3, 2, 1, 0 }.

Serial.begin(9600);

You can NOT use Serial, because you are using pins 0 and 1 for the keypad.

at the moment i only print "Taste".

I can see that, but what do you want to do with it in the future ? What I was thinking of was to use 'A' to 'Z' and '[' to '_' in the keypad and using the ASCII value returned as the index to an array of the strings required, if indeed they are required.

Incidentally, you say that the keyboard should have 31 buttons but you have 40 definitions in the keypad. I assume that the entries for '-' are dummy ones, or are they ?

I want to connect a speaker to the arduino and play a sound when i press a key (for example C2).
I don't understand the second part with the ASCII value.

char *hexaKeys[]={
"Cis3",  "C3",    "-",   "Cis1",   "E2",
"D3",    "H2",    "-",   "D1",     "Dis2",
"Dis3",  "B2",    "-",   "Dis1",   "D2",
"E3",    "A2",    "-",   "E1",     "Cis2",
"F3",    "Gis2",  "-",   "F1",     "C2",
"-",      "G2",   "-",   "Fis1",   "H1",
"-",      "Fis2", "H0",  "G1",     "B1",
"-",      "F2",   "C1",  "Gis1",   "A1"
};

Do you mean i have to change that part of the program?

I don't understand the second part with the ASCII value.

A simple example.

Suppose that you had just 5 keys which returned 'A', 'B', 'C', 'D' and 'E' respectively. Then, when the user presses key 'A' you will get an ASCII value of 65. Subtract 65 from the value of any key returned and you will get a value between 0 and 5. This value can be used as the index to an array of strings to convert the number into a string that is unique to that key

Ah ok now i get it.
Also i changed my program so i get the right names from the serial monitor. I know it's not the best way to make it but it works ;D

#include <Keypad.h>
//Hier wird die größe des Keypads definiert
const byte COLS = 5; //5 Spalten
const byte ROWS = 8; //8 Zeilen
//Die Ziffern/Zeichen:
char hexaKeys[ROWS][COLS]={
{'Q',  'P',    '-',   '2',   'H'},
{'R',  'O',    '-',   '3',   'G'},
{'S',  'N',    '-',   '4',   'F'},
{'T',  'M',    '-',   '5',   'E'},
{'U',  'L',    '-',   '6',   'D'},
{'-',  'K',    '-',   '7',   'C'},
{'-',  'J',    '0',   '8',   'B'},
{'-',  'I',    '1',   '9',   'A'}
};
byte colPins[COLS] = { 13, 12, 11, 10, 9 }; //Definition der Pins für die 3 Spalten
byte rowPins[ROWS] = { 8, 7, 6, 5, 4, 3, 2, A0 };//Definition der Pins für die 4 Zeilen
char Taste; //pressedKey entspricht in Zukunft den gedrückten Tasten
int Note;
Keypad Tastenfeld = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); //Das Keypad kann absofort mit myKeypad angesprochen werden
void setup() {
Serial.begin(9600);
}

void loop() {
Taste = Tastenfeld.getKey(); //pressedKey entspricht der gedrückten Taste
if (Taste) { //Wenn eine Taste gedrückt wurde
  Serial.print("bereit");
  if (Taste == '0'){
      Serial.print("Die Taste ");
      Serial.print("H0");
      Serial.print(" wurde gedrueckt");
      Serial.println(); //Teile uns am Serial Monitor die gedrückte Taste mit
      }
   if (Taste == '1') {
      Serial.print("Die Taste ");
      Serial.print("C1");
      Serial.print(" wurde gedrueckt");
      Serial.println(); //Teile uns am Serial Monitor die gedrückte Taste mit
      }
   if (Taste == '2') {
      Serial.print("Die Taste ");
      Serial.print("Cis1");
      Serial.print(" wurde gedrueckt");
      Serial.println(); //Teile uns am Serial Monitor die gedrückte Taste mit
      }
   if (Taste == '3') {
      Serial.print("Die Taste ");
      Serial.print("D1");
      Serial.print(" wurde gedrueckt");
      Serial.println(); //Teile uns am Serial Monitor die gedrückte Taste mit
      }
   if (Taste == '4') {
      Serial.print("Die Taste ");
      Serial.print("Dis1");
      Serial.print(" wurde gedrueckt");
      Serial.println(); //Teile uns am Serial Monitor die gedrückte Taste mit
      }
   if (Taste == '5') {
      Serial.print("Die Taste ");
      Serial.print("E1");
      Serial.print(" wurde gedrueckt");
      Serial.println(); //Teile uns am Serial Monitor die gedrückte Taste mit
      }
   if (Taste == '6') {
      Serial.print("Die Taste ");
      Serial.print("F1");
      Serial.print(" wurde gedrueckt");
      Serial.println(); //Teile uns am Serial Monitor die gedrückte Taste mit
      }
   if (Taste == '7') {
      Serial.print("Die Taste ");
      Serial.print("Fis1");
      Serial.print(" wurde gedrueckt");
      Serial.println(); //Teile uns am Serial Monitor die gedrückte Taste mit
      }
   if (Taste == '8') {
      Serial.print("Die Taste ");
      Serial.print("G1");
      Serial.print(" wurde gedrueckt");
      Serial.println(); //Teile uns am Serial Monitor die gedrückte Taste mit
      }
   if (Taste == '9') {
      Serial.print("Die Taste ");
      Serial.print("Gis1");
      Serial.print(" wurde gedrueckt");
      Serial.println(); //Teile uns am Serial Monitor die gedrückte Taste mit
      }
   if (Taste == 'A') {
      Serial.print("Die Taste ");
      Serial.print("A1");
      Serial.print(" wurde gedrueckt");
      Serial.println(); //Teile uns am Serial Monitor die gedrückte Taste mit
      }
   if (Taste == 'B') {
      Serial.print("Die Taste ");
      Serial.print("B1");
      Serial.print(" wurde gedrueckt");
      Serial.println(); //Teile uns am Serial Monitor die gedrückte Taste mit
      }
   if (Taste == 'C') {
      Serial.print("Die Taste ");
      Serial.print("H1");
      Serial.print(" wurde gedrueckt");
      Serial.println(); //Teile uns am Serial Monitor die gedrückte Taste mit
      }
   if (Taste == 'D') {
      Serial.print("Die Taste ");
      Serial.print("C2");
      Serial.print(" wurde gedrueckt");
      Serial.println(); //Teile uns am Serial Monitor die gedrückte Taste mit
      }
   if (Taste == 'E') {
      Serial.print("Die Taste ");
      Serial.print("Cis2");
      Serial.print(" wurde gedrueckt");
      Serial.println(); //Teile uns am Serial Monitor die gedrückte Taste mit
      }
   if (Taste == 'F') {
      Serial.print("Die Taste ");
      Serial.print("D2");
      Serial.print(" wurde gedrueckt");
      Serial.println(); //Teile uns am Serial Monitor die gedrückte Taste mit
      }
   if (Taste == 'G') {
      Serial.print("Die Taste ");
      Serial.print("Dis2");
      Serial.print(" wurde gedrueckt");
      Serial.println(); //Teile uns am Serial Monitor die gedrückte Taste mit
      }
   if (Taste == 'H') {
      Serial.print("Die Taste ");
      Serial.print("E1");
      Serial.print(" wurde gedrueckt");
      Serial.println(); //Teile uns am Serial Monitor die gedrückte Taste mit
      }
   if (Taste == 'I') {
      Serial.print("Die Taste ");
      Serial.print("F1");
      Serial.print(" wurde gedrueckt");
      Serial.println(); //Teile uns am Serial Monitor die gedrückte Taste mit
      }
   if (Taste == 'J') {
      Serial.print("Die Taste ");
      Serial.print("Fis2");
      Serial.print(" wurde gedrueckt");
      Serial.println(); //Teile uns am Serial Monitor die gedrückte Taste mit
      }
   if (Taste == 'K') {
      Serial.print("Die Taste ");
      Serial.print("G2");
      Serial.print(" wurde gedrueckt");
      Serial.println(); //Teile uns am Serial Monitor die gedrückte Taste mit
      }
   if (Taste == 'L') {
      Serial.print("Die Taste ");
      Serial.print("Gis2");
      Serial.print(" wurde gedrueckt");
      Serial.println(); //Teile uns am Serial Monitor die gedrückte Taste mit
      }
   if (Taste == 'M') {
      Serial.print("Die Taste ");
      Serial.print("A2");
      Serial.print(" wurde gedrueckt");
      Serial.println(); //Teile uns am Serial Monitor die gedrückte Taste mit
      }
   if (Taste == 'N') {
      Serial.print("Die Taste ");
      Serial.print("B2");
      Serial.print(" wurde gedrueckt");
      Serial.println(); //Teile uns am Serial Monitor die gedrückte Taste mit
      }
   if (Taste == 'O') {
      Serial.print("Die Taste ");
      Serial.print("H2");
      Serial.print(" wurde gedrueckt");
      Serial.println(); //Teile uns am Serial Monitor die gedrückte Taste mit
      }
   if (Taste == 'P') {
      Serial.print("Die Taste ");
      Serial.print("C3");
      Serial.print(" wurde gedrueckt");
      Serial.println(); //Teile uns am Serial Monitor die gedrückte Taste mit
      }
   if (Taste == 'Q') {
      Serial.print("Die Taste ");
      Serial.print("Cis3");
      Serial.print(" wurde gedrueckt");
      Serial.println(); //Teile uns am Serial Monitor die gedrückte Taste mit
      }
   if (Taste == 'R') {
      Serial.print("Die Taste ");
      Serial.print("D3");
      Serial.print(" wurde gedrueckt");
      Serial.println(); //Teile uns am Serial Monitor die gedrückte Taste mit
      }
   if (Taste == 'S') {
      Serial.print("Die Taste ");
      Serial.print("Dis3");
      Serial.print(" wurde gedrueckt");
      Serial.println(); //Teile uns am Serial Monitor die gedrückte Taste mit
      }
   if (Taste == 'T') {
      Serial.print("Die Taste ");
      Serial.print("E3");
      Serial.print(" wurde gedrueckt");
      Serial.println(); //Teile uns am Serial Monitor die gedrückte Taste mit
      }
   if (Taste == 'U') {
      Serial.print("Die Taste ");
      Serial.print("F3");
      Serial.print(" wurde gedrueckt");
      Serial.println(); //Teile uns am Serial Monitor die gedrückte Taste mit
      }
   
}
}

I know it's not the best way to make it but it works

Ouch ! That code is painful.

At the very least set a variable to the decoded string, remove the printing from inside the if clauses and do it at the end of the if (Taste) block

Go on. Try the "lookup by using the ASCII code" method. You know you want to !

The 'char' in your table of keys can be any value from 1 to 255 (0 means 'NoKey' so you can use that for unused portions of your matrix.

const char hexaKeys[ROWS][COLS]={
{1, 2, 3, 4, 5}, 
{6, 7, 8, 9, 10},  
{11,  12, 13, 14, 15}, 
{16, 17, 18, 19, 20},
{21, 22, 23, 24, 25}, 
{26, 27, 28, 29, 30}, 
{31, 32, 33, 34, 35}, 
{36, 37, 38, 39, 40}};

char *NameLookup[40] = { "Cis3",  "C3",    "-",   "Cis1",   "E2",
"D3",    "H2",    "-",   "D1",     "Dis2",
"Dis3",  "B2",    "-",   "Dis1",   "D2",
"E3",    "A2",    "-",   "E1",     "Cis2",
"F3",    "Gis2",  "-",   "F1",     "C2",
"-",      "G2",   "-",   "Fis1",   "H1",
"-",      "Fis2", "H0",  "G1",     "B1",
"-",      "F2",   "C1",  "Gis1",   "A1"};

char *name;
if (key != NoKey)
{
    name = NameLookup[key-1];  // Keys start a 1 but arrays start at 0
}