Problem with or in if loop |solved|

I want to create a loop that will not work if one of the two specific buttons are pressed.
So I tried the if loop with one condition and it worked.

#include <Keypad.h>

const byte ROWS = 4; 
const byte COLS = 4; 
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
byte rowPins[ROWS] = {5, 4, 3, 2}; 
byte colPins[COLS] = {9, 8, 7, 6}; 
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
int x=1;
char key;

void setup() 


void loop(){
  char key = keypad.waitForKey();
  if (key!='3')

After this I tried two condition as listed on the OR page.

if (key!='3'||key!='5')

And it did not worked. So I tried this.

if (key!='3'|key!='5')

I tried also.

if (key!='3' or key!='5')

But none of them work, so for now Im stuck with 2 if loops. can I do the two parameters in one loop?

Try && instead.

Try writing the statement out in words:
if key is not equal to 3 or key is not equal to 5

If key is 3 then it is not equal to 5, likewise if key is 5 it is not equal to 3.

You need and not or.

if key is not equal to 3 and not equal to 5.

As WildBill has written AND is &&.

Okay thanks it solved the problem. Do you know why the option of or do not work?

Hi, I’m testing Wokwi, and made a simulation for your sketch:
Click the green start button to run. Click the stop button to change the sketch.

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