2 different Functions with one Keypad

Hello,
I am facing the following problem. I have a keypad and I want to press key 1 e.g. have a certain other function. When this function has been carried out, the basic function of key 1 should be reset as key switch command 1. How can this be implemented?
Hier der Sketch:

#include <Keypad.h>       
#include <Keyboard.h>     

const byte ROWS = 4; 
const byte COLS = 8; 

char hexaKeys[ROWS][COLS] = {
  {'E','0','C','A','B','D','F','G'},
  {'9','8','7','J','K','L','M','N'},
  {'6','5','4','X','X','X','H','I'},
  {'3','2','1','X','X','X','O','P'}
  
};

byte rowPins[ROWS] = {22, 23, 24, 25};
byte colPins[COLS] = {26, 27, 28, 29, 30, 31, 32, 33};

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

char ctrlKey = KEY_LEFT_CTRL;
char leftshift = KEY_LEFT_SHIFT;

void setup() {
  Serial.begin(9500);
}
  
void loop() {
  
  char customKey = customKeypad.getKey();
  
  if (customKey){
    Serial.println(customKey);
  }
}

MfG
Simon_2002

Keep track of what pressing key 1 should do by using the value of a variable. That way key 1 can always return the same value but depending on the value of the variable different actions can be taken

The program could have a "state variable" that determines which action should be taken when a certain key is pressed.

Change that variable at any time to determine the outcome of the next key press, for example, a shift key could change a state variable.

Hello,

Thanks for the answers, I understand that I can do this with the help of variables. Unfortunately, my problem is, I don't quite know how to incorporate that into my sketch.

As an example again:

So I have a keyboard (keypad): 1,2,3,4,5,6,7,8,9,0, enter and delete
and the key 1 is the keyboard command is: leftshift 1, standard.

But now I would like to do it in such a way that button 1 initially has the keyboard command: leftshift g and when it is then pressed, the keyboard command: leftshift 1 functions again as the actual main function.
If I then e.g. enter 1234 as a Pin and I should press Enter to get everything back from
go ahead.

MfG
Simon

Please provide more information.

What do "leftshift" and "standard" mean, and how do you imagine they are implemented?

How many and which keys does your keypad have? Do all of them work? Post a photo.

Hallo,

As you can see in the picture, there are 2 functions for each of the 10 buttons and I would like to make them possible to work.

I did not understand your description what should happen.
what does "leftshift" mean? Is this an extra-key?

For example the top-left-key named "Linie / Kurs 1 "

So what is your idea to select if pressing this key labeled "**Linie / Kurs 1 ** "
shall initiate function "Linie / Kurs" or shall initiate function enter the digit "1" ?

by the way
using serial.begin(9500) is a non-standard baudrate.
Standard is 9600 but I would use 115200 because it is much faster

Your code defines a keypad 4 rows 8 colums
the picture does not show all keys that are physically on the keypad.
Do you want to use just the keys 1 to 9 "löschen" 0 "Eingabe" ?

best regards Stefan

It would make sense to define the green up and down arrow buttons to set/reset a state variable to use the "upper" and "lower" functions of the other keys.

if (key_pressed == UP_ARROW) state = USE_UPPER;
if (key_pressed == DOWN_ARROW) state = USE_LOWER;
...
if (key_pressed == '2' ) {
   if (state == USE_UPPER)  Route();
   else ...

   }

Well, the first thing that should work is all of these functions that are above the numbers (Linie/Kurs, for example)

if then e.g. Linie/Kurs was pressed, it should work as a number again.

Bsp.

We have given you some hints. Try putting them into code before you post again.

Do either of the blue buttons have any purpose in your project?

doing a first key-press activating the function "Linie / Kurs" and then the next presses of any key shall mean " a single digit" can be coded if you have always the pattern

"Linie / Kurs" then a single digit example 1
"Linie / Kurs" then a single digit example 2
"Linie / Kurs" then a single digit example 3
...
"Linie / Kurs" then a single digit example 9
"Linie / Kurs" then a single digit example 0

as soon as you have a pattern
"Linie / Kurs" then a single digit example 1
"Linie / Kurs" then two or more digits example "23"
"Linie / Kurs" then two or more digits example "789"

it has to be coded different from the first case.

The user has to press an additional key to to give your code the information "entering of digits finished"
That's the reason why have a enter-key on almost every keyboard.

As you can see from this analysis you have to provide more information about the functionality you want to achieve.
Any experienced or professional code-developper starts with an analysis of the functionality. This description is done in normal words. This is something you can do and you should do. Nobody here likes to write down code-examples that are just eventually useful.

So the next step is to describe the functionality in normal words.
best regards Stefan

Is it possible to include this code twice with 2 variables in the sketch?

char hexaKeys[ROWS][COLS] = {
  {'E','0','C','A','B','D','F','G'},
  {'9','8','7','J','K','L','M','N'},
  {'6','5','4','X','X','X','H','I'},
  {'3','2','1','X','X','X','O','P'}
 
};

Is it possible to include this code twice with 2 variables in the sketch?

Yes, of course you can declare a second array with a different name and values but what would you then do with it ?

We are talking about details. Please give an overview over your whole project.
in mimimum 70% of all cases knowing the whole thing offers completely different and much better working solutions.
This is like

Newbee: "I want to do better cutting please help me sharpening. "
Expert: Sure I can help you. With what are you cutting?
Newbee: With a scissor.
Expert: OK take this sharpening tool
Newbee: Yea works great Next question How can I make it cut faster I need to finish faster.
expert: Motorised scissors.
newbee Yea works great though still not fast enough.

expert: Ok can you give an overview about what you are cutting.
newbee: the green of a football-arena.
expert: Oha! take a big mowing tractor with a seven boom spindel-mower and GPS-steering

In the beginning the newbee alsways just told details.
The expert was assuming the newbee knows that his basic approach is well suited.
which turns out to be very bad suited
that's the reason why it is always a good idea to give an overview and to explain what shall happen in the end.

Still you are free to walk down a lot of bad-working-approaches collecting experience
best regards Stefan

Ok then I'll do that now !!
The thing is, I'm German and now not the superman in English texts and since my project is quite extensive,
I'll write the text in German now, I'm sorry.

DE:
Also, erstmal Grundlegende Infos zum Projekt.
Ich habe eine Fahrerkabiene in meinem Zimmer aus einem Citaro Facelift Bus (Bild 1/2), diesen habe ich so umgebaut, dass er zusammen mit dem Computerspiel
OMSI 2 Der Omnibussimulator funktioniert. Dies ist auch schon teils abgeschlossen.

Nun zu meinem eigentlichen neben Projekt, zu dem auch meine Frage galt.

Ich habe ein IBIS MAS 2 aus einem alten Berliner BVG Bus, welches ich mit meinem Arduino Due verbunden habe.
Ein IBIS MAS 2 (siehe 3. Bild im Anhang) ist ein Steuergerät, welches unteranderen für die Schilderung von älteren Bussen
(also das Zeil was vorne am Bus drann steht) verantwortlich ist. Dieses habe ich erworben und baue es nun so um, dass es zusammen mit dem Computerspiel
OMSI 2 funktioniert. Das setze ich demnach dann mit einem Arduino Due um
und einem Plugin für das Spiel, welches die Grundlegenden Infos: z.B. Halltestellen oder Uhrzeiten, etc via eine Serielle Schnittstelle dem Arduino mitteilt.
Dieses Plugin liegt mir schon vor, ebenso dass wissen, wie man dies umsetzt, also wie man die Infos vom Plugin auf das Display überträgt.
Mein Problem ist nun folgendes. In echt funktioniert dieses IBIS Mas 2 so, dass bei dem Bereich mit dem Zahlen: 1,2,3,4,5,6,7,8,9,0 (Siehe Anhang 4) ZWEI Funktionen hinterlegt sind.
Anfangs, wenn man z.B. das IBIS MAS 2 startet, gibt es auf dem Nummernfeld NUR die Funktionen, welche ÜBER! den Zahlen stehen. z.B. "Linie/Kurs", "Route", etc.
Wenn man dann auf die "1" in diesem Fall mit der Funktion "Linie/Kurs" drückt, wirt z.B. der Tastendruck "o" ausgegeben, welcher dann IM Spiel ebenfals den Trigger "o": Linie/Kurs betätigt.
Wenn dies Passiert erscheint auf dem Display: Linie/Kurs:_____. Dort kann man dann 5 Zahlen eingeben, welche dann für die Linie zuständig sind, die dann vorne am Bus angezeigt wird.
Das ist ja noch einfach, dass Problem jetzt ist jetzt darf die Taste "1" NICHT mehr den Tastenbefehl "o" ausführen, sondern z.B. "k" für die ZAHL: 1, damit dann der Trigger IM Spiel, für die Taste 1 (das gilt dann auch für alle anderen Zahlentaster am IBIS) ausgeführt wird.
(siehe auch Schema im Anhang 5) Und genau das ist mein Problem. Mir ist nicht ganz klar wie ich das umsetzen kann.
Wer es immer noch nicht ganz verstanden hat, hier gibts ein Video wie es funktionieren soll: OmsiDeluxe - OMSI - Funktionstest IBIS (Arbeitsstand) - YouTube oder OMSI 2: IBIS 2 Mod Vorstellung (Variante VAG Nürnberg) - YouTube
Man kann jetzt auch auf die Idee kommen Ihn (beim Video) anzuschreiben, jedoch gibt er kein Support mehr, deshalb wende ich mich an euch. Auch die Idee, die anderen Tasten, wie die Pfeiltasten zu benutzen ist nicht möglich, da ich diese noch für andere Funktionen brauch!

Mit freudlichen Grüßen
Simon

Would you be more comfortable if this topic was moved to the German section of the forum ?

Joa wouldn't be a problem for me

Hi Simon,

wow very cool equipment! Bus driving is your real passion!

google-translate works very good in this cases.
as a demonstration for this i put your german text translated into english here into a codesection

EN:
So, first of all, basic information about the project.
I have a driver's cab in my room from a Citaro Facelift Bus (Fig. 1/2), which I converted so that it can be used with the computer game
OMSI 2 The bus simulator works. This has already been partially completed.

Now to my actual next project, to which my question also applied.

I have an IBIS MAS 2 from an old Berlin BVG bus, which I connected to my Arduino Due.
An IBIS MAS 2 (see 3rd picture in the appendix) is a control device which, among other things, is used to describe older buses
(So ​​the line that is on the front of the bus) is responsible. I bought this one and am now converting it to work with the computer game
OMSI 2 works. I then do that with an Arduino Due
and a plugin for the game, which provides the basic information: e.g. Hall test points or times, etc via a serial interface to the Arduino.
This plugin is already available to me, as well as knowing how to implement this, i.e. how to transfer the information from the plugin to the display.
My problem now is this. In real life, this IBIS Mas 2 works in such a way that TWO functions are stored in the area with the numbers: 1,2,3,4,5,6,7,8,9,0 (see Appendix 4).
Initially, if you e.g. the IBIS MAS 2 starts, there are ONLY the functions on the number field which ABOUT! the numbers. e.g. "Line / Course", "Route", etc.
If you then press the "1" in this case with the "Line / Course" function, e.g. the key press "o" is output, which then also activates the trigger "o": line / course in the game.
When this happens, the display shows: Line / Course: _____. There you can then enter 5 numbers, which are then responsible for the line, which is then displayed on the front of the bus.
That is still simple, the problem is now the key "1" must NO longer execute the key command "o", but e.g. "k" for the NUMBER: 1, so that the trigger IM game is executed for key 1 (this also applies to all other number buttons on the IBIS).
(See also scheme in Appendix 5) And that is exactly my problem. I am not entirely sure how to implement this.
If you still haven't fully understood it, here is a video of how it should work: https://www.youtube.com/watch?v=DS03lygkCvo or https://www.youtube.com/watch?v=wWrSBoyx7mQ
You can now get the idea to write to him (with the video), but he no longer gives any support, so I'll get in touch with you. The idea of ​​using the other keys, such as the arrow keys, is also not possible, as I need them for other functions!

With kind regards
Simon

You can do the same for translating back into german.

As I'm writing this post the thread ist still in the english-forum.
OK so now mor ethings are clear I still have some more questions:

You described press key "Linie/Kurs" then appears placeholders for 5 digits
does the original device need to have always typed in five digits?
example Linie "2" ist typed in as "00002" ? if this is true it is a fixed pattern and is no problem to code it
without additional keypresses. Because it IS a fixed pattern

If the IBIS MAS 2-device is in "idle" state the arduino awaits the first keypress after the first keypress is done and it is key "Linie Kurs 1" the Arduino-code switches the mode to input 5 digits.

This is really no problem to write code for this.

Did you already connect the keypad to the arduino and the basic function is available?

best regards Stefan

DE:
Joa also, für Linie: 2 wäre es dann 00200, für Linie: 20 -> 02000 und für Linie 200 -> 20000. Die 00000 sind für die Routen Eingabe also Route: 1 -> 01 und Route; 10 -> 10. Dies ist aber auch möglich beim drücken der Taste 2 (Route), da gibt es dann nur 2 Zahlenfelder: Route:__.

Jap die Tastatur ist angeschlossen und die grundfunktonen also das normale belegen einer Taste ist vollkommen funktionsfähig.

EN:
So yeah, for line: 2 it would be 00200, for line: 20 -> 02000 and for line 200 -> 20000. The 00000 are for the route input so route: 1 -> 01 and route; 10 -> 10. This is also possible by pressing button 2 (route), there are then only 2 number fields: Route: __.

Yes, the keyboard is connected and the basic functions, i.e. the normal assignment of a key, is fully functional.