Transfer array between different switch cases

hello,
I would like some help. trying to make a code that collect digits from keypad, put it in array, and then print it in the serial monitor after "#" is pressed.

but then, I saw that my array doesn't save itself between cases.
may someone help me please?

#include <Keypad.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
  
  //main:
byte i = 0;
bool codeflag;
  
  //keypad:

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
   
   //lcd:
LiquidCrystal_I2C lcd(0x27,20,4);  // set the LCD address to 0x27 for a 16 chars and 2 line display

void setup() {
   
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
  lcd.setCursor(3,0);
  lcd.print("power on");
  delay(700);
  lcd.clear();
}

void loop() {
  char array1[5];
  // put your main code here, to run repeatedly:
  
  char key = keypad.getKey();

  switch (key){
    case ('#'):
       lcd.clear();
    codeflag = false;
    for (int f=1; f<= i; f++){
      Serial.print(array1[f]);
    }
    i = 0;
    Serial.println();
    break;

    case ('*'):
      codeflag =true;
      break;

    default:
      if (key and codeflag== true){    //2 conditions: pressing key+ the first character is '*' (code flag is true).
        
        lcd.setCursor(i,0);
        array1[i] = key;
        lcd.print(array1[i]);
        Serial.print(array1[i]);
        i++;
        if (i>=3){
         i = 3;
          }
        }
     break;
      }
  }

Is that the array that is not saving its data between 'cases' or a loop of loop()?

the array usually doesnt contain more then 4 values, scince it has:

lcd.setCursor(i,0);
        array1[i] = key;
        lcd.print(array1[i]);
        Serial.print(array1[i]);
        i++;
        if (i>=3){
         i = 3;
          }
       }
        ```

Did you mean

the thing is that the array isn't static since its get different values every time:

default:
      if (key and codeflag== true){    //2 conditions: pressing key+ the first character is '*' (code flag is true).
        
        lcd.setCursor(i,0);
        array1[i] = key;
        lcd.print(array1[i]);
        Serial.print(array1[i]);
        i++;
        if (i>=3){
         i = 3;
          }
        }
     break;
      }```

Ok.
I tried.

Did you read about static variables on the internet?

Static variables are defined using the static keyword. These retain their value even when they are no longer in use. They are only initialized once and exist till the program is terminated. They are local to the function in which they are defined.

When the CPU gets to the last } in loop() all the non static variables are destroyed. Oh Just read about it on the internet...

You are confusing static with const

thank you!
learnt something new! :blush:

i am new in the tinkering world

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