Multiple functions for buttons

Hello

Im trying to make a little gadget here.

it will use atleast 3 buttons.

1 for selecting a program (program 1-6)
2 and 3 will do some keystrokes

how can i make the 2 and 3 button to do different keystrokes depending on wich program i selected with button 1?

as i understand i need to use switch-case.
i got it to work to change program.
but when i try to add a button action in a case i get stuck.

i will later on use a small oled-screen. but for now i just trying to get it to work with the serial monitor.

this is how far got now
its a mix from 2 other examples.

Can someone please help me why i get stuck in “action1()”
I can not change program. just print Executing #1 with my button.

/*
 * This code create a MENU structure on a 16x2 LCD screen.
 * Six (6) different screens are implemented, and you can travel
 * thru them by pressing a button on Arduino PIN 4. Each press
 * of the button advances one (1) screen.
 *
 * Made by Clovis Fritzen in 05/06/2017
 * http://www.FritzenMaker.com
 * http://www.FritzenLab.com.br
*/
//Based on YWROBOT's LiquidCrystal_I2C library, Library version:1.1
//Also based on the Debounce.ino sketch that comes with Arduino IDE
 
#include <Wire.h>

 
int WhichScreen =1;   // This variable stores the current Screen number
boolean hasChanged = true;
const int buttonPin = 7;    // the number of the pushbutton pin
int selectButton = 8;
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers
 
void setup()
{
  
  pinMode(buttonPin, INPUT);
  pinMode(selectButton, INPUT);
  Serial.begin(9600);
   
}
void loop()
{
 
  if (hasChanged == true) {
   
  switch(WhichScreen) {
    case 1:
    {
      firstScreen();
    }
      break;
   
    case 2:
      {
        secondScreen();
      }
      break;
   
    case 3:
      {
        thirdScreen();
      }
      break;
   
    case 4:
      {
        fourthScreen();
      }
      break;
   
    case 5:
      {
        fifthScreen();
      }
      break;
   
    case 6:
      {
        sixthScreen();
      }
      break;
    case 0:
      {
       
      }
      break;
    }
}
 
    //-------------------------------
    // BEGIN of the switch debouncing code
    int reading = digitalRead(buttonPin);
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }
 
if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:
 
    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;
 
      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
        hasChanged = true;
        WhichScreen++;
       
       
      }
    } else {
      hasChanged = false;
    }
  }

// execute button start
  if (!digitalRead(selectButton)){
    executeAction();
    //updateMenu();
    delay(100);
    while (!digitalRead(selectButton));
  }

  // execute button end
  lastButtonState = reading;
  // END of the switch Debouncing code
  // --------------------------------------
  if (WhichScreen > 6){
    WhichScreen = 1;
  }
}
 
void firstScreen()
  {

   Serial.println("Hello world 1");

  }
void secondScreen()
  {

    Serial.println("Hello world 2");

  }
void thirdScreen()
  {

    Serial.println("Hello world 3");

  }
void fourthScreen()
  {

    Serial.println("Hello world 4");
  }
void fifthScreen()
  {

    Serial.println("Hello world 5");
  }
void sixthScreen()
  {

    Serial.println("Hello world 6");
  }


// the executebutton

void executeAction() {
  switch (WhichScreen) {
    case 1:
      action1();
      break;
    case 2:
      action2();
      break;
    case 3:
      action3();
      break;
    case 4:
      action4();
      break;
  }
}

void action1() {
  
  Serial.println(">Executing #1");
  delay(100);
}
void action2() {
  
  Serial.println(">Executing #2");
  delay(100);
}
void action3() {
  
  Serial.println(">Executing #3");
  delay(100);
}
void action4() {
  
  Serial.println(">Executing #4");
  delay(100);
}

Welcome to the forums. +1 Karma for using code tags on your first post!

Your logic is a bit off. If a button press causes a screen to change you set hasChanged = 1 but you only ever set if back to 0 inside the same if() statement. You need to always set it back to zero after you take the action.

Also, you do not need curly braces inside a case statement. They don’t do anything and are not typical.

You also need to check if your screen number rolls over before you do any action, not afterwards.

/*
   This code create a MENU structure on a 16x2 LCD screen.
   Six (6) different screens are implemented, and you can travel
   thru them by pressing a button on Arduino PIN 4. Each press
   of the button advances one (1) screen.

   Made by Clovis Fritzen in 05/06/2017
   http://www.FritzenMaker.com
   http://www.FritzenLab.com.br
*/
//Based on YWROBOT's LiquidCrystal_I2C library, Library version:1.1
//Also based on the Debounce.ino sketch that comes with Arduino IDE

#include <Wire.h>


int WhichScreen = 1;  // This variable stores the current Screen number
boolean hasChanged = true;
const int buttonPin = 7;    // the number of the pushbutton pin
int selectButton = 8;
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup()
{

  pinMode(buttonPin, INPUT);
  pinMode(selectButton, INPUT);
  Serial.begin(9600);

}
void loop()
{

  if (hasChanged == true) {
    hasChanged = false;

    switch (WhichScreen) {
      case 1:
        firstScreen();
        break;

      case 2:
        secondScreen();
        break;

      case 3:
        thirdScreen();
        break;

      case 4:
        fourthScreen();
        break;

      case 5:
        fifthScreen();
        break;

      case 6:
        sixthScreen();
        break;

      case 0:
        break;
    }
  }

  //-------------------------------
  // BEGIN of the switch debouncing code
  int reading = digitalRead(buttonPin);
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
        hasChanged = true;
        WhichScreen++;
        if (WhichScreen > 6) {
          WhichScreen = 1;
        }
      }
    }
  }

  // execute button start
  if (!digitalRead(selectButton)) {
    executeAction();
    //updateMenu();
    delay(100);
    while (!digitalRead(selectButton));
  }

  // execute button end
  lastButtonState = reading;
  // END of the switch Debouncing code
  // --------------------------------------
}

void firstScreen()
{

  Serial.println("Hello world 1");

}
void secondScreen()
{

  Serial.println("Hello world 2");

}
void thirdScreen()
{

  Serial.println("Hello world 3");

}
void fourthScreen()
{

  Serial.println("Hello world 4");
}
void fifthScreen()
{

  Serial.println("Hello world 5");
}
void sixthScreen()
{

  Serial.println("Hello world 6");
}


// the executebutton

void executeAction() {
  switch (WhichScreen) {
    case 1:
      action1();
      break;
    case 2:
      action2();
      break;
    case 3:
      action3();
      break;
    case 4:
      action4();
      break;
  }
}

void action1() {

  Serial.println(">Executing #1");
  delay(100);
}
void action2() {

  Serial.println(">Executing #2");
  delay(100);
}
void action3() {

  Serial.println(">Executing #3");
  delay(100);
}
void action4() {

  Serial.println(">Executing #4");
  delay(100);
}

blh64:
Welcome to the forums. +1 Karma for using code tags on your first post!

Your logic is a bit off. If a button press causes a screen to change you set hasChanged = 1 but you only ever set if back to 0 inside the same if() statement. You need to always set it back to zero after you take the action.


Is this why you moved “hasChanged = false;” to just before the Switch - case?

You also need to check if your screen number rolls over before you do any action, not afterwards.

And this is why you moved

if (WhichScreen > 6) {
          WhichScreen = 1;

I see you have helped me editing the code.
But there is still something wrong.
i can not use my “buttonPin = 7”.

Like im stuck in firstScreen().

can still use my “selectButton = 8” to Execute 1. But only that

I notice you have the button pins configured as INPUT rather than INPUT_PULLUP.

Are your buttons wired with pullup or pulldown resistors? If so which?

How are your buttons wired up? Your code wants them to be HIGH when they are pressed. To accomplish this, you need to have a pulldown reisistor installed. Do you?

A less component way of doing it is to wire one side of a button to ground and the other to a pin. You then declare the pin as INPUT_PULLUP which allows it to be HIGH when not pressed and LOW when pressed.

This code is based on the StateChangeDetection example (File->Examples->02.Digital->StateChangeDetection) and also assumes you are using INPUT_PULLUP.

/*
   This code create a MENU structure on a 16x2 LCD screen.
   Six (6) different screens are implemented, and you can travel
   thru them by pressing a button on Arduino PIN 4. Each press
   of the button advances one (1) screen.

   Made by Clovis Fritzen in 05/06/2017
   http://www.FritzenMaker.com
   http://www.FritzenLab.com.br
*/
//Based on YWROBOT's LiquidCrystal_I2C library, Library version:1.1
//Also based on the Debounce.ino sketch that comes with Arduino IDE

#include <Wire.h>


int WhichScreen = 1;  // This variable stores the current Screen number
boolean hasChanged = true;
const int choiceButtonPin = 7;    // the number of the pushbutton pin
const int selectButtonPin = 8;
int lastChoiceState;
int lastSelectState;

const unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup()
{

  pinMode(choiceButtonPin, INPUT_PULLUP);
  pinMode(selectButtonPin, INPUT_PULLUP);
  Serial.begin(9600);

  lastChoiceState = digitalRead( choiceButtonPin );
  lastSelectState = digitalRead( selectButtonPin );
}
void loop()
{
  // check Choice switch
  int choiceState = digitalRead(choiceButtonPin);
  if (choiceState != lastChoiceState) {
    // only signal state change when pressed (LOW)
    if (choiceState == LOW) {
      hasChanged = true;
      WhichScreen++;
      if (WhichScreen > 6) {
        WhichScreen = 1;
      }
    }
    delay(debounceDelay);
  }
  lastChoiceState = choiceState;

  if (hasChanged == true) {
    hasChanged = false;

    switch (WhichScreen) {
      case 1:
        firstScreen();
        break;

      case 2:
        secondScreen();
        break;

      case 3:
        thirdScreen();
        break;

      case 4:
        fourthScreen();
        break;

      case 5:
        fifthScreen();
        break;

      case 6:
        sixthScreen();
        break;

      case 0:
        break;
    }
  }

  // check select switch
  int selectState = digitalRead(selectButtonPin);
  if (selectState != lastSelectState) {
    // only signal state change when pressed (LOW)
    if (selectState == LOW) {
      hasChanged = true;
    }
    delay(debounceDelay);
  }
  lastSelectState = selectState;

  // execute button start
  if (hasChanged == true) {
    hasChanged = false;
    executeAction();
    delay(100);
  }
}

void firstScreen()
{
  Serial.println("Hello world 1");
}

void secondScreen()
{
  Serial.println("Hello world 2");
}

void thirdScreen()
{
  Serial.println("Hello world 3");
}

void fourthScreen()
{
  Serial.println("Hello world 4");
}

void fifthScreen()
{
  Serial.println("Hello world 5");
}

void sixthScreen()
{
  Serial.println("Hello world 6");
}


// the executebutton

void executeAction() {
  switch (WhichScreen) {
    case 1:
      action1();
      break;
    case 2:
      action2();
      break;
    case 3:
      action3();
      break;
    case 4:
      action4();
      break;
  }
}

void action1() {

  Serial.println(">Executing #1");
  delay(100);
}
void action2() {

  Serial.println(">Executing #2");
  delay(100);
}
void action3() {

  Serial.println(">Executing #3");
  delay(100);
}
void action4() {

  Serial.println(">Executing #4");
  delay(100);
}

blh64:
How are your buttons wired up? Your code wants them to be HIGH when they are pressed. To accomplish this, you need to have a pulldown reisistor installed. Do you?

A less component way of doing it is to wire one side of a button to ground and the other to a pin. You then declare the pin as INPUT_PULLUP which allows it to be HIGH when not pressed and LOW when pressed.

This code is based on the StateChangeDetection example (File->Examples->02.Digital->StateChangeDetection) and also assumes you are using INPUT_PULLUP.

/*

This code create a MENU structure on a 16x2 LCD screen.
Six (6) different screens are implemented, and you can travel
thru them by pressing a button on Arduino PIN 4. Each press
of the button advances one (1) screen.

Made by Clovis Fritzen in 05/06/2017
http://www.FritzenMaker.com
http://www.FritzenLab.com.br
*/
//Based on YWROBOT’s LiquidCrystal_I2C library, Library version:1.1
//Also based on the Debounce.ino sketch that comes with Arduino IDE

#include <Wire.h>

int WhichScreen = 1; // This variable stores the current Screen number
boolean hasChanged = true;
const int choiceButtonPin = 7; // the number of the pushbutton pin
const int selectButtonPin = 8;
int lastChoiceState;
int lastSelectState;

const unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers

void setup()
{

pinMode(choiceButtonPin, INPUT_PULLUP);
pinMode(selectButtonPin, INPUT_PULLUP);
Serial.begin(9600);

lastChoiceState = digitalRead( choiceButtonPin );
lastSelectState = digitalRead( selectButtonPin );
}
void loop()
{
// check Choice switch
int choiceState = digitalRead(choiceButtonPin);
if (choiceState != lastChoiceState) {
// only signal state change when pressed (LOW)
if (choiceState == LOW) {
hasChanged = true;
WhichScreen++;
if (WhichScreen > 6) {
WhichScreen = 1;
}
}
delay(debounceDelay);
}
lastChoiceState = choiceState;

if (hasChanged == true) {
hasChanged = false;

switch (WhichScreen) {
  case 1:
    firstScreen();
    break;

  case 2:
    secondScreen();
    break;

  case 3:
    thirdScreen();
    break;

  case 4:
    fourthScreen();
    break;

  case 5:
    fifthScreen();
    break;

  case 6:
    sixthScreen();
    break;

  case 0:
    break;
}

}

// check select switch
int selectState = digitalRead(selectButtonPin);
if (selectState != lastSelectState) {
// only signal state change when pressed (LOW)
if (selectState == LOW) {
hasChanged = true;
}
delay(debounceDelay);
}
lastSelectState = selectState;

// execute button start
if (hasChanged == true) {
hasChanged = false;
executeAction();
delay(100);
}
}

void firstScreen()
{
Serial.println(“Hello world 1”);
}

void secondScreen()
{
Serial.println(“Hello world 2”);
}

void thirdScreen()
{
Serial.println(“Hello world 3”);
}

void fourthScreen()
{
Serial.println(“Hello world 4”);
}

void fifthScreen()
{
Serial.println(“Hello world 5”);
}

void sixthScreen()
{
Serial.println(“Hello world 6”);
}

// the executebutton

void executeAction() {
switch (WhichScreen) {
case 1:
action1();
break;
case 2:
action2();
break;
case 3:
action3();
break;
case 4:
action4();
break;
}
}

void action1() {

Serial.println(">Executing #1");
delay(100);
}
void action2() {

Serial.println(">Executing #2");
delay(100);
}
void action3() {

Serial.println(">Executing #3");
delay(100);
}
void action4() {

Serial.println(">Executing #4");
delay(100);
}

ToddL1962:
I notice you have the button pins configured as INPUT rather than INPUT_PULLUP.

Are your buttons wired with pullup or pulldown resistors? If so which?

Wow. Thank you.
Both of you were spot on.

This is how i wired:

And i see this is wired as pull up.

blh64 Your code worked right away

ill read a bit on if i should use pull up or pull down.

Thank you yet again.
now i can continue with this project after your help.
this was a great forum : )

I'm not exactly sure how that is wired since you can not tell if the left two pins on a button are tied together or the top two pins tied together. I'm assuming it is the left two pins tied together and the right two pins tied together.

This configuration would not be a pull-up but a pull-down resistor. The resistor is there to pull the pin to a ground level when the button is not pushed. Once the button is pushed, power is connected to the pin to make it go high.

Thank you.
Must have gotten that wrong somehow :S

Now im stuck at another thing.

If i press my "sign-button" (Or remote button 2) 3 times the cumputer freezes.
I got it to work somehow when i changes something in the part for the IR.
Line 128 - 163. But dont remember what it was. So i think something there isnt ok.

If i use only the part of the IR-code without any other code, i can press the remote without its freezing.

Maby something is wrong when i added the "sign-button"?

Heres my code so far:

 /*
* IR-pin = 4
 * 
 * yellow button = 10 - select
 * green button = 16 - sign
 * white button = 5
 * red button = 15 - choise
*/

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <IRremote.h>
#include <Keyboard.h>

const int RECV_PIN = 4;
IRrecv irrecv(RECV_PIN);
decode_results results;
unsigned long key_value = 0;

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

int WhichScreen = 1;  // This variable stores the current Screen number
boolean hasChanged = true;
const int choiceButtonPin = 15;    // the number of the pushbutton pin
const int selectButtonPin = 10;
const int signButtonPin = 16;
int lastChoiceState;
int lastSelectState;
int lastSignState;
int LED = 5;


const unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup()
{
  irrecv.enableIRIn();
  irrecv.blink13(true);
  pinMode(LED, OUTPUT); //set the LED pin as OUTPUT

  pinMode(choiceButtonPin, INPUT_PULLUP);
  pinMode(selectButtonPin, INPUT_PULLUP);
  pinMode(signButtonPin, INPUT_PULLUP);
  
  Serial.begin(9600);

  lastChoiceState = digitalRead( choiceButtonPin );
  lastSelectState = digitalRead( selectButtonPin );
  lastSignState = digitalRead( signButtonPin );

  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }
  delay(2000);
  display.clearDisplay();

  display.setTextSize(3);
  display.setTextColor(WHITE);
  display.setCursor(0, 10);
  // Display static text
  display.println("test");
  display.display();
  delay(2000);


}
  //start blinkfunktion
void Blink() {

     digitalWrite(LED, HIGH);
  delay(150);
     digitalWrite(LED, LOW);
     
  }


  //slut blinkfunktion
  
void loop()
{
  if (irrecv.decode(&results)){
 
        if (results.value == 0XFFFFFFFF)
          results.value = key_value;

        switch(results.value){
      
          case 0x8BA4F801:
            Serial.println("Press F9");
            Keyboard.write(0xCA);  //F9
            Blink();
            delay(200);
            break ;
          case 0x1AC86FA2:
            Serial.println("Sign-sequence");
            Keyboard.write(0xC5);  // tryck F4
            delay(200);
            Keyboard.press(0x80); // Hold ctrl
            Keyboard.write('a'); // press a
            delay(100);
            Keyboard.releaseAll(); // release buttons
            Keyboard.press(0xD4); //delete
            Keyboard.print("CESP1010");
            delay(200); 
            Keyboard.write(0xB0); //enter
            Blink();
            delay(1000);
            break ;
          case 0x1EC81DBF:
            Serial.println("3");
            Blink();
            delay(200);
            break ;      
        }
        key_value = results.value;
        irrecv.resume(); 
  }
  // check Choice switch
  int choiceState = digitalRead(choiceButtonPin);
  if (choiceState != lastChoiceState) {
    // only signal state change when pressed (LOW)
    if (choiceState == LOW) {
      hasChanged = true;
      WhichScreen++;
      if (WhichScreen > 6) {
        WhichScreen = 1;
      }
    }
    delay(debounceDelay);
  }
  lastChoiceState = choiceState;

  if (hasChanged == true) {
    hasChanged = false;

    switch (WhichScreen) {
      case 1:
        firstScreen();
        break;

      case 2:
        secondScreen();
        break;

      case 3:
        thirdScreen();
        break;

      case 4:
        fourthScreen();
        break;

      case 5:
        fifthScreen();
        break;

      case 6:
        sixthScreen();
        break;

      case 0:
        break;
    }
  }

  // check select switch
  int selectState = digitalRead(selectButtonPin);
  if (selectState != lastSelectState) {
    // only signal state change when pressed (LOW)
    if (selectState == LOW) {
      hasChanged = true;
    }
    delay(debounceDelay);
  }
  lastSelectState = selectState;

  // execute button start
  if (hasChanged == true) {
    hasChanged = false;
    executeAction();
    delay(100);
  }

  //sign switch start

  // check select switch
  int signState = digitalRead(signButtonPin);
  if (signState != lastSignState) {
    // only signal state change when pressed (LOW)
    if (signState == LOW) {
      hasChanged = true;
    }
    delay(debounceDelay);
  }
  lastSignState = signState;

  // execute button start
  if (hasChanged == true) {
    hasChanged = false;
    signAction();
    delay(100);
  }

  //sign switch slut
}

void firstScreen()
{
  Serial.println("Program 1");
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println("1/6");
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 20);
  display.println("Program 1");
  display.display();

}

void secondScreen()
{
  Serial.println("Program 2");
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println(" ");
  display.setCursor(0, 0);
  display.println("2/6");
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 20);
  display.println("Program 2");
  display.display();
}

void thirdScreen()
{
  Serial.println("Program 3");
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println(" ");
  display.setCursor(0, 0);
  display.println("3/6");
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 20);
  display.println("Program 3");
  display.display();
}

void fourthScreen()
{
  Serial.println("Program 4");
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println(" ");
  display.setCursor(0, 0);
  display.println("4/6");
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 20);
  display.println("Program 4");
  display.display();
}

void fifthScreen()
{
  Serial.println("Program 5");
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println(" ");
  display.setCursor(0, 0);
  display.println("5/6");
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 20);
  display.println("Program 5");
  display.display();
}

void sixthScreen()
{
  Serial.println("Program 6");
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println(" ");
  display.setCursor(0, 0);
  display.println("6/6");
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 20);
  display.println("Program 6");
  display.display();
}


// the executebutton

void executeAction() {
  switch (WhichScreen) {
    case 1:
      action1();
      break;
    case 2:
      action2();
      break;
    case 3:
      action3();
      break;
    case 4:
      action4();
      break;
    case 5:
      action1();
      break;
  }
}

void action1() {

  Serial.println(">Pressing F9");
  Keyboard.write(0xCA);  // tryck F9
  delay(100);
}
void action2() {

  Serial.println(">Executing #2");
  delay(100);
}
void action3() {

  Serial.println(">Executing #3");
  delay(100);
}
void action4() {

  Serial.println(">Executing #4");
  delay(100);
}


// the signbutton

void signAction() {
  switch (WhichScreen) {
    case 1:
      action01();
      break;
    case 2:
      action02();
      break;
    case 3:
      action03();
      break;
    case 4:
      action04();
      break;
    case 5:
      action01();
      break;
  }
}

void action01() {

  Serial.println(">Sign-sequence");
  Keyboard.write(0xC5);  // tryck F4
  delay(200);
  Keyboard.press(0x80); // Hold ctrl
  Keyboard.write('a'); // press a
  delay(100);
  Keyboard.releaseAll(); // release buttons
  Keyboard.press(0xD4); //delete
  Keyboard.print("CESP1010");
  delay(200); 
  Keyboard.write(0xB0); //enter
  delay(1000);
}
void action02() {

  Serial.println(">sign #2");
  delay(100);
}
void action03() {

  Serial.println(">sign #3");
  delay(100);
}
void action04() {

  Serial.println(">sign #4");
  delay(100);
}

I don't see anything in the code that would cause the system to freeze. What does the Serial Monitor output look like?

Hm..
Well the serial output usally prints the button text ("Pressing F9" or "Sign-sequence") and is doing the keyboard command.

But after plugging the pro micro out from the computer, sometimes nothing appears in the serial output, but it do the keyboardcommands.

But moast of the times it freezes on the fourth press on the "sign-button".

Maby this might be a hardwareproblem?
when i transfer the program to the pro micro i have to do it 2 times. because the first time it says it cant find anything at the comport. but i can still use the buttons and the oled works. after unplugging it and plugging it back in i can transfer the program.

is there any maximum recommended file size? this program uses more then 90% of the storage and 40% of memory