Identifying rogue inputs

Good Evening,

I am a Mature Foundation year student and struggling a little bit.

Very simple sketch, but i want to differentiate when a number is input rather than a letter.

I actually want the Serial window to tell me that "whatever" is not a letter, and tell me to input a letter

I suppose i am looking for the opposite of the JS command (isNaN)

Would appreciate any pointers

Here is my code

/*Scott Lambert
 Forum code*/

 char char_input(0);

void setup()  {         //as always

      Serial.begin(9600);    // setting port transfer speed
        Serial.println( "  Please input a letter  ");  // serial monitor to display text encased in quotes
}

void loop() {            // as always

  charFunction();
  
}

void charFunction()  {
  
        if(Serial.available() == true)    //checks if there has been an input
{
          {
        char_input =  Serial.read();      
  
  
          }  
        
                  Serial.print("your input was    ");
                  delay (1000);
                  Serial.print(char_input);
                  
           }}

Thanks

Scott

check out what the JS command was based on: ctype.h: int isalpha(int c) int isdigit(int c) int isxdigit(int c) ...

Since you are working with one character at a time it will be sufficient to determine if that character is a leter or not.

    if (!isalpha(char_input))
       Serial.print("not a letter.");
    else
        Serial.print(char_input);

There are also library functions for classifying characters: isnum(), isspace(), isalpha(). Just include ctype.h

http://www.nongnu.org/avr-libc/user-manual/group__ctype.html

You are receiving single characters, which aren't actually characters, but their ASCII values. You can check the value to be in a range to be specified by you, and build a filter with that. I think that might help you do this.

I guess the above answers using ctype.h will be easier.

Take a look at the values Ascii gives to each char on the keyboard. Look at the way 0…9, a…z and A…Z are arranged,

0 has the value x then 1 is x+1 2 is x+2 etc,
A has a value y then B y+1 C= y+2 and so on
a has the value z then b has the value z+1 …

So

ch=Serial.read();
if (ch>='0' && ch<='9') // its a number

You can do the same with the letter, with simple calculation you can conver from upper to lower case.

But best of all you can go from the char for a number to its value eg ch-‘0’ gets you the number you input.

Mark

Serial.available() actually returns an int, and while what you are doing will probably work, it's only by accident that it works.