if statement

I have a keypad connected to my Arduino via a Keypad Encoder IC. It works well, returning a unique character for each key press. The IC is serially connected to pin 3.

I can display the unique number, but what I can’t do is the following:

if the number >> 6, do something.

Should I be using (DEC) or (char) or (BYTE)?

I would appreciate your help.

#include <NewSoftSerial.h>

NewSoftSerial mySerial(3,1);

int val;

void setup(){
 
  Serial.begin(9600);
  
  mySerial.begin(9600);
  val == ((char)mySerial.read());
}
  
  void loop(){

    delay (200);
  
  if (mySerial.available()) {
      Serial.print((char)mySerial.read());
  }    
   
 [glow]  if ((val)>>(char),6) {
      Serial.print("YES");[/glow]
   }     
   delay (500);
  }

You're using a right-shift operator instead of "greater than". Not sure what that comma is doing either.

This:

 val == ((char)mySerial.read());

isn't doing anything except reading the read buffer and discarding the result.

In "loop" you're reading a character from the receive buffer and printing it, but not saving it.

Is this what you’re trying to do?

#include <NewSoftSerial.h>

NewSoftSerial mySerial(3,1);

void setup()
{
  Serial.begin(9600);
  mySerial.begin(9600);
}

void loop()
{
  char val;

  delay (200);

  if (mySerial.available()) 
  {
    val=mySerial.read();
    Serial.print(val);

    if(val>6) 
      Serial.print("YES");
  }

  delay (500);
}

Or maybe you want to do something if val is greater than the character 6. If so, change to if(val>‘6’).

Thanks Andy,

I think I understand a little better.

However, regardless of what character is returned, "YES" is printed. What I am trying to achieve is if a character 1 is returned do something - if 2 is returned do something else....and so on

Try printing the value that is sent to the Arduino. It may not match the number on the key. If it is being sent '1' when the 1 key is pressed, the ASCII value for 1 is 49, which IS greater than 6.

Thanks for the assistance. That’s what I like about arduino - help is never far away.

The following works.

#include <NewSoftSerial.h>

NewSoftSerial mySerial(3,1);

void setup()
{
  Serial.begin(9600);
  mySerial.begin(9600);
}

void loop()
{
  char val;

  delay (200);

  if (mySerial.available())
  {
    val=mySerial.read();
    Serial.print(val,DEC);

if (val == 52)
Serial.print("dong");
else
Serial.print ("ding");

     delay (500);
}
}
if (val == 52)

You code also use:

if (val == '4')

Assuming that 52 corresponds to the 4 key, this is much more readable.