Help with a

what I have here is a code that converts 4-digit binary values and converts it to BCD, showing it on an LCD. In addition, using the red key prints 0 on the LCD IFTM, prints 1 decimal. What I want to do is for the code to convert a binary number to hexadecimal, but I can't seem to know where to start.

#include<LiquidCrystal.h>

LiquidCrystal lcd1(8,9,10,11,12,13);
int a, b, c, d, m,s1,s2,s3,s4,resultado;


void setup()
{
  lcd1.begin(16,2);// 16 x 2 16 caracteres em 2 linhas
  Serial.begin(9600); //PRINT Serial
     
  pinMode(5,INPUT); // first digit - A  (1-CH)
  pinMode(4,INPUT); //second digit - B   (2-CH)
  pinMode(3,INPUT); //third digit - C   (3-CH)
  pinMode(2,INPUT); //forth digit - D (4-CH)
  pinMode(A0,INPUT); // key m - 0 IFTM and 1 DECIMAL (DEC)Red

}

void loop()
{
  a = digitalRead(5);
  b = digitalRead(4);
  c = digitalRead(3);
  d = digitalRead(2);
  m = digitalRead(A0);
  
  if(a == 1){
    s1 = 8;
  }else{
    s1 = 0;
  }
  if(b == 1){
    s2 = 4;
  }else{
    s2 =0;
  }  
  if(c == 1){
    s3 = 2;
  }else{
    s3 =0;
  }
  if (d ==1){
    s4 = 1;
  }else{
    s4 = 0;
  }
  resultado = s1 + s2 + s3 + s4;
  Serial.print(m);
  Serial.print(' ');
  Serial.println(result);
  
  if(m == 0){
    lcd1.setCursor(0,0);
    lcd1.print("IFTM");// Prints IFTM
  }else if (m == 1){
    lcd1.setCursor(0,0);
    lcd1.print("DEC ");//Prints DEC
  }
  
  if(resultado < 10){
    lcd1.setCursor(0,1);
    lcd1.print("0");
    lcd1.setCursor(1,1);
    lcd1.print(result);
  }else{
    lcd1.setCursor(0,1);
    lcd1.print(result);
    }
    
    lcd1.setCursor(7,0);
    lcd1.print("a");
    lcd1.setCursor(7,1);
    lcd1.print(a);
  
    lcd1.setCursor(8,0);
    lcd1.print("b");
    lcd1.setCursor(8,1);
    lcd1.print(b);
  
    lcd1.setCursor(9,0);
    lcd1.print("c");
    lcd1.setCursor(9,1);
    lcd1.print(c);
  
    lcd1.setCursor(10,0);
    lcd1.print("d");
    lcd1.setCursor(10,1);
    lcd1.print(d);
  
  	delay(800);
  
  }

Topic moved

Why did you originally post this in the Remote Learning section of the forum ?

What I want to do is for the code to convert a binary number to hexadecimal

There's no difference, there is nothing to convert. Hexadecimal is just a convenient way to represent binary.

For hexadecimal:

  if(resultado < 0x10){
    lcd1.setCursor(0,1);
    lcd1.print("0");
    lcd1.setCursor(1,1);
    lcd1.print(result, HEX);
  }else{
    lcd1.setCursor(0,1);
    lcd1.print(result, HEX);
    }

Simplified:

    lcd1.setCursor(0,1);
  if(resultado < 0x10)
    lcd1.print("0");
  lcd1.print(result, HEX);

further simplified and indented :slight_smile:

lcd1.setCursor(0,1);
if (resultado < 0x10) lcd1.write('0');
lcd1.print(result, HEX);

The OP asks about converting binary to BCD. Then asks about converting binary to HEX. What is really wanted?

Paul

Paul_KD7HB:
The OP asks about converting binary to BCD. Then asks about converting binary to HEX. What is really wanted?

hum, that's not how I read it. He has code to display BCD and wants to modify it to display in HEX... at least that's how I understood the request

Paul_KD7HB:
The OP asks about converting binary to BCD. Then asks about converting binary to HEX. What is really wanted?

Paul

I think we all understood it differently, hence the different answers.

Indeed - very probably