Issue printing a byte

I want to get two independent digits from the user and then merge them into a single byte. Like first digit is “1” ( tens place), and next is “5” (units place). I want to store it as “15” and then read it and display as “15”. The code snippet is as below :

      byte numval, tYear, uYear, year ;
      numval = 1;
      tYear = numval<<4;
      uYear = 5; 
      year = tYear + uYear ;
      EEPROM.write(4,year);
      tempYear = bcdToDec(EEPROM.read(4)) ; 
      lcd.print (tempYear);

What is wrong with above approach ??

I think you are mixing up binary and decimal maths.

You need

(numVal * 10) + uYear;

Or, if the "numbers" are characters in a char array such as numAsChars char[] = "15"; you can convert that to an int with the atoi() function.

...R

Yes, this does a multiply by 16:
tYear = numval<<4;

You forgot to show the declaration of tempYear. I suspect it is a byte which .print() will show as a character. To show it as a number, declare it (or cast it) int.

It appears that the data are inputted as a string, using two bytes, and then he wants to convert that to a numeric value 15. He then wants to store it as a single byte. If that's the case, then this might work:

void setup()
{
  byte year;
  char buff[3];
  int charsRead;
  Serial.begin(9600);

  Serial.println("Enter the year:");
  while (true) {
    if (Serial.available() > 0) {
      charsRead = Serial.readBytesUntil('\n', buff, sizeof(buff) - 1);
      buff[charsRead] = '\0';
      break;
    }
  }
  year = (byte) atoi(buff);  
  Serial.println( (int) year);
}

void loop()
{
}

If he wants to write it to EEPROM, it would be easy to do.

Solution still eludes .... The new code based on suggestions : So now I what am I missing ??

... byte tYear, uYear; int tempYear; 

 if (location == thou && numval != 'L') {
         tYear = numval *10;
         Serial.println(tYear);   // Prints 10 when user enters 1. OK
      }
      
      if (location == hund && numval != 'L') {
         uYear = numval;         // Prints 5 when user enters 5 . OK 
         Serial.println( uYear);
      }

     
      if (UserKey == 'E') {
        tempYear = tYear+uYear;
         Serial.println(tempYear);   //  Prints 79. Where did it get 79 ? 
         EEPROM.write ( 4, tempYear); // Saves BCD 55. How 55 ? 
        }

Your code fragment is too small. If you want a good answer you should post a small but COMPLETE sketch that shows the problem.

Sorry Johnwasser ! I thought it will be wasting others time posting all of the code - it is already some 350 lines...but I do get your point about being too brief.

Anyway I have got it working now and the code is as below :

.. byte tYear, uYear, tempYear.

if (location == thou && numval != 'L') {
         tYear =  numval * 10;
         Serial.println(tYear);   
      }
      
      if (location == hund && numval != 'L' && numval != 'E') {  // Setting the trap to avoid printing 'E' solved the issue.
         uYear = numval;         
         Serial.println( uYear);
      }
       
      if (UserKey == 'E') {
        tempYear = tYear+uYear;
         Serial.println(tempYear);   
         EEPROM.write(4,decToBcd(tempYear));
      }

Mogaraghu:
I want to get two independent digits from the user and then merge them into a single byte. Like first digit is “1” ( tens place), and next is “5” (units place). I want to store it as “15” and then read it and display as “15”. The code snippet is as below :

      byte numval, tYear, uYear, year ;

numval = 1;
      tYear = numval<<4;
      uYear = 5;
      year = tYear + uYear ;
      EEPROM.write(4,year);
      tempYear = bcdToDec(EEPROM.read(4)) ;
      lcd.print (tempYear);




What is wrong with above approach ??

First - are you getting these bytes from the user as ASCII? ASCII encodes characters as numbers, as described in any ASCII table. (eg: http://www.asciitable.com/ ). So the value of the ASCII digit ‘0’ is the number 0x30 (hecadecimal), 48 (Decimal), or XLVIII (roman numnerals). THe reason I mention roman numerals is to try to help you understand that a number is not the same thing as how it is written down.

So if the user input comes to you as ASCII (which seems to be what is hapenning), you need to subtract 0x30, 48, or better yet ‘0’ from the value to get it as a numeric quantity from 0-9.

Apart from that, yes this code would appear to store it as a BCD encoded value in year. And if the lcd.print function undertands that a byte is a numeric value that it needs to expand, then the print functrion should work.

What I would tend to do, rather than giving your entire program to this board, it to make up a snippet that compiles and runs on its own. Just write a short sketch whose setup() includes the code above and whose loop() does nothing.

Mogaraghu: Solution still eludes .... The new code based on suggestions : So now I what am I missing ??

Try the maths without any of the IFs - then add the IFs when it works without them.

When debugging simplify simplify simpl....

And post a complete program.

...R

Thanks to all of you who responded.

And pointed the right things without getting annoyed. For sure will take the points and try to be more specific next time onwards...

The support eco system for the Arduino is really robust ..I like it.