# troubles programming a deco BCD-Hex

hello, i’m trying to take a binary number, and display its value in hexadecimal sistem in a 7 seg display taking from a array with byte that contains the infotmation to turn on the led of the 7 seg.
the problem is, it just show all 7 leds on, and is not show any change when i move the four switches in the input pins. can somebody help me plaese
thank you.

here is the code:

``````byte disp;
byte interruptor;
const byte deco7seg[16]={B0111111,B0000110,B1011011,B1001111,B1100110,B1101101,B1111101,B0000111,B1111111,B1100111,B1110111,B1111100,B0111001,B1011110,B1111001,B1110001}; /*//////////////////////////////////////*/
/*   0         1        2       3        4        5        6        7        8       9        a        b         c         d        e       f*/
void setup() {
int i;
for(i=9;1<13;i++) pinMode(i,INPUT_PULLUP);
for(i=2;i<9;i++) pinMode(i,OUTPUT);
}

void loop(void) {
interruptor; lectura();
disp = deco7seg[interruptor];
escritura (disp);
delay(500);

}

void escritura (byte x){
int i;
for(i=2;i<9;i++)
{digitalWrite(i,x&HIGH);
x>>1;
}
}

byte lectura (void)
{int i;
byte x=0,y;
for(i=12;i>8;i--)
{y=0;

x=(x>>1)||y;

}
return x;
}
``````
``````x=(x>>1)||y;
``````

I think you’ve got this backwards. The 1 will go in the right-most bit, so you’d want to shift x to the left.

``````interruptor; lectura();
``````

This code makes no sense. Did you mean to set the value of interruptor to the return value from that function?

``````interruptor = lectura();
``````
``````void escritura (byte x){
int i;
for(i=2;i<9;i++)
{digitalWrite(i,x&HIGH);
x>>1;
}
}
``````

This is weird too. You shouldn’t rely on HIGH always being 1. It will be, but you shouldn’t count on it.

``````void escritura (byte x){
int i;
for(i=2;i<9;i++)
{digitalWrite(i, x & 1);
x>>1;
}
}
``````

for(i=9;1<13;i++)

OOPS

kvnabril:
hello, i'm trying to take a binary number, and display its value in hexadecimal sistem in a 7 seg display taking from a array with byte that contains the infotmation to turn on the led of the 7 seg.
the problem is, it just show all 7 leds on, and is not show any change when i move the four switches in the input pins. can somebody help me plaese
thank you.

``````// convert decimal to BCD
uint8_t dec2bcd (uint8_t dec)
{
return ((dec / 10 * 16) + (dec % 10));
}

// convert BCD to decimal
uint8_t bcd2dec (uint8_t bcd)
{
return ((bcd / 16 * 10) + (bcd % 16));
}
``````

Great functions for decimal to BCD and back. But the OP wants hex. Should be easy enough to modify, but I've had too much scotch to start doing math.

Delta_G:
You shouldn't rely on HIGH always being 1. It will be, but you shouldn't count on it.

I often use 0/1 instead of LOW/HIGH, in 'digitalWrite()' statements, for easier typing. They're defined in "Arduino.h" as:-

``````#define HIGH 0x1
#define LOW  0x0
``````

I don't think I'll get into trouble by continuing to do it. (Fingers crossed.)

Something like :

``````uint8_t lowHex(uint8_t num) {return num & 15;}
uint8_t highHex(uint8_t num) {return num >> 4;}
``````

By the way, quicker bcd convert :

``````uint8_t dec2bcd (uint8_t dec) {return dec + 6 * (dec / 10);}
uint8_t bcd2dec (uint8_t bcd) {return bcd - 6 * (bcd >> 4);}
``````