Can i use the switch case to I set a value?

like this

	void keypadEvent(KeypadEvent eKey) {
 
		switch (eKey) {
			case '*': 
			val = HIGH; 
				break;

I use this code but it does not work

 	#include <Keypad.h>
	
char keys[rows][cols] = { 	//@SIM char[][] keys = new char[][]{  
	{'1','2','3'},    		//@SIM new char[]{'1','2','3'} , 
	{'4','5','6'},   		//@SIM new char[]{'4','5','6'} ,
	{'7','8','9'}, 			//@SIM new char[]{'7','8','9'} ,
	{'#','0','*'} 			//@SIM new char[]{'#','0','*'} ,
	};						//@SIM };
  
	// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these pins, 
	// eg. ROW0 = Arduino pin2.
	byte rowPins[] = { 0, 1, 2, 3 };

	// Connect keypad COL0, COL1 and COL2 to these pins, 
	// eg. COL0 = Arduino pin6.
	byte colPins[] = { 6, 5, 4 };

	//create a new Keypad
 	Keypad keypad = Keypad(rowPins, colPins,  sizeOf(rowPins) , sizeOf(colPins) );

	#define ledPin 13 
int val;	 

	void setup() 
	{
		digitalWrite(ledPin, HIGH);   // sets the LED on
	 	Serial.begin(9600);
	  	keypad.addEventListener(keypadEvent); //add an event listener
	}

	void loop() 
	{
		keypad.getKey();

	}
 
	//take care of some special events
	void keypadEvent(KeypadEvent eKey) {
 
		switch (eKey) {
			case '*': 
			val = HIGH; 
				break;
			case '#': 
				digitalWrite(ledPin, LOW); 
				break;
			case KEY_RELEASED:
				Serial.println("Key Released"); 
				break;
			default:
				if (eKey != NO_KEY){
					Serial.println(eKey);
				}
			if (val == HIGH){
				digitalWrite(ledPin, HIGH);
			}
		} 
 
	}

What does "it does not work" mean?

not working

Ok, let's try the inverse.
What would you define as "working"?

Your 'if (val == HIGH)' is inside the default clause of the switch statement. Does anything get Serial.printed?

if (val == HIGH){
digitalWrite(ledPin, HIGH);

this is i want to do ledPin = high

Your 'if (val == HIGH)' is inside the default clause of the switch statement.

No, it isn't. The indenting of OPs code is atrocious, making it very difficult to follow what is going on.

OP: The Tools + Auto format tool would be a good thing for you to learn how to use. Consistent placement of { relative to functions would be a good habit to develop, too. I prefer putting all { on separate lines. That makes lining up the { and } possible, and clearly brackets a block of code/function.

There's no reason to have a Serial.begin() statement with no Serial.print() or Serial.println() statements. So, since you already have a Serial.begin() statement, you need to add some Serial.print() statements to help you determine what is happening.

The getKey() function returns the value of the key being pressed. Why are you throwing that value away in loop()? Why are you even calling getKey() in loop()?

I'd say the "if" is inside the default clause.
Whether or not it is meant to be, only the OP can answer.

I'd say the "if" is inside the default clause.

You're right. I was missing the { on the if(eKey) statement, because of the poor formatting.

				if (eKey != NO_KEY){
					Serial.println(eKey);
				}

Didn't think I was going entirely mad :slight_smile:

I tried to use this code and it is ok

 	#include <Keypad.h>
	
char keys[rows][cols] = { 	//@SIM char[][] keys = new char[][]{  
	{'1','2','3'},    		//@SIM new char[]{'1','2','3'} , 
	{'4','5','6'},   		//@SIM new char[]{'4','5','6'} ,
	{'7','8','9'}, 			//@SIM new char[]{'7','8','9'} ,
	{'#','0','*'} 			//@SIM new char[]{'#','0','*'} ,
	};						//@SIM };
  
	// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these pins, 
	// eg. ROW0 = Arduino pin2.
	byte rowPins[] = { 0, 1, 2, 3 };

	// Connect keypad COL0, COL1 and COL2 to these pins, 
	// eg. COL0 = Arduino pin6.
	byte colPins[] = { 6, 5, 4 };

	//create a new Keypad
 	Keypad keypad = Keypad(rowPins, colPins,  sizeOf(rowPins) , sizeOf(colPins) );

	#define ledPin 13 	 
int val ;


	void setup() 
	{
		digitalWrite(ledPin, HIGH);   // sets the LED on
	 	Serial.begin(9600);
	  	keypad.addEventListener(keypadEvent); //add an event listener
	}

	void loop() 
	{
		keypad.getKey();

	}
 
	//take care of some special events
	void keypadEvent(KeypadEvent eKey) {
 
		switch (eKey) {
			case '*': 
			val = HIGH;
				break;
			case '#': 
		val = LOW; 
				break;
			case KEY_RELEASED:
				Serial.println("Key Released"); 
				break;
			default:

				}
	if (val == HIGH){
		digitalWrite(ledPin, HIGH); 
	}
	if (val == LOW){
		digitalWrite(ledPin, LOW); 
	}
	}

But this is an example
I have this code. I'm going step by step is working when i press ''A'' and after that an other button for example the ''A" and "1"

#include <Wire.h>
#include <PCFCrystal.h>
#include <i2ckeypad.h>
#include <Password.h>

Password password = Password( "1234" );

byte buffer = 0;

PCFCrystal lcd(B00100000, B00010000, B00000001, B00000010, B00000100, B00001000, 0x39, &buffer);
const int buttonPin = 2;
int buttonState = 0; 

#define ROWS 4
#define COLS 4


#define PCF8574_ADDR 0x38


i2ckeypad kpd = i2ckeypad(PCF8574_ADDR, ROWS, COLS);
int val;
#define ledPin 13 

void setup() {
  Serial.begin(9600);
  Wire.begin();
  lcd.begin(16, 2);
  lcd.setCursor(0,0);

  pinMode(buttonPin, INPUT);
  kpd.init();


  Serial.print("Testing keypad/PCF8574 I2C port expander arduino lib\n\n");
}

void loop() {
  buttonState = digitalRead(buttonPin);





  char key = kpd.get_key();

  if(key) {
    switch (key)
    {
    case 'A':
      val=HIGH;
      break;
    case 'B':
      lcd.setCursor(0,0);
      lcd.print("ON OUT                         ");
      break;

      Serial.println(key);
    case '#': 
      guessPassword(); 
      break;
    case '*': 
      password.reset(); 
      break;
    default: 

      password.append(key);

      if (val == HIGH){
        lcd.setCursor(0,0);
        lcd.print("ok                    ");
      }

    }

  }



}



void guessPassword(){
  if (password.evaluate()){
    val=LOW;
    lcd.setCursor(0,0);
    lcd.print("correct password                     ");

  }
  else{
    lcd.setCursor(0,0);
    lcd.print("wrong  password                     ");
  }
}

Before posting code, could you please run it through the IDE's auto format tool, please?
Your indentation hurts my eyes.

OK SORRY

if (val == HIGH){
digitalWrite(ledPin, HIGH);

this is i want to do ledPin = high

...

void setup() 
{
  digitalWrite(ledPin, HIGH);   // sets the LED on
  Serial.begin(9600);
  keypad.addEventListener(keypadEvent); //add an event listener
}

You never set the pin mode of the ledPin to output. That isn't going to help.

It is ok Reply #10