setting four digital pins to a bcd code...or not?

I am trying to set four digital pins to be able to run a seven segment via a 74ls47.
I have looked through many examples of how to produce BCD and can’t see how to send the signal to pins.
I have produced the following code that should send the BCD to pins 4,5,6 and7 but an LED on pin 4 stays on (pin 4 stays high) whatever number I put in the monitor?
Can someone see my error please?

int b;
int pins[4] = {4,5,6,7}; // A,B,C,D inputs
byte BCD[16][4] ={{0,0,0,0},
{1,0,0,0},
{0,1,0,0},
{1,1,0,0},
{0,0,1,0},
{1,0,1,0},
{0,1,1,0},
{1,1,1,0},
{0,0,0,1},
{1,0,0,1},
{0,1,0,1},
{1,1,0,1},
{0,0,1,1},
{1,0,1,1},
{0,1,1,1},
{1,1,1,1}}; //BCD code
{
  Serial.begin(9600);
  for(int a = 0; a < 4; a++)
  {
  pinMode(pins[a], OUTPUT); //set outputs
  }
}
void loop() 
{
  if (Serial.available()) 
  {
    int number = Serial.read();
    for( int b = 0; b < 4; b++)
    {
    digitalWrite(pins[b], BCD[number][b]);
    }
  }
}

I have a feeling it’s in the way I am reading the number from the monitor but I can’t see why?

thanks

What data type does Serial.read() return?

What is the value of number if you display it on the serial monitor?

Why not just do

for( int b = 0; b < 4; b++)
 { digitalWrite(pins[b], bitRead(number, b)); }

In the grand scheme of things, it is a bit slower but will save (valuable) memory (RAM).

What do you have connected to the Serial pins? If a terminal emulator, do you need to press Return or Enter, which would always give you the same final character?

What Arduino are you using? Serial may be the wrong thing to use for some Arduinos.

If you get a number beyond the end of the array, you may output something random.

My reference for Serial.read() includes code to show the received character. Perhaps you should do something similar.

Perhaps you should do something to verify that the pins are wired correctly and work.

Hi adwsystems,
I am using BCD(number) as opposed to your bitread(number) because I want it to pick up the numbers in the array BCD and send them to the pins 4,5,6 and 7.
If I put

Serial.print(pins[b], BCD[number][b]);

in there I get 49 if I put a 1 in, 50 if I put a 2 in followed by 13 which I assume is the carriage return. So I assume Serial.read returns the ascii code of the character.

vaj4088,
I have nothing connected to the pins yet apart from an LED (and resistor) connected to pin 4 of my UNO I am just trying to get it to give me the output I am looking for. As my last answer to adwsystems hints I look as if the Serial.read is returning the ascii code and not the number, how can I alter this?

I am not sure what I intend using to address it eventually it is just a little project with a seven segment I removed from an old box I was given.
I will look at ways of feeding the numbers 0 - 9 in some other way.

The documentation for Serial.read() says that it returns an int that contains -1 if there is no character available, or returns the ASCII character. This is all you will need from Serial.read().

A trick that may work in your case is

byte number = Serial.read() - '0' :

Of course, this only works correctly if Serial.read() returns a character from 0 through 9 so you better check that number is legitimate.

By the way, the numbers that you are seeing are the ASCII encoding for those characters. '0' is represented by 48, '1' is represented by 49, etc.

matelot:
Hi adwsystems,
I am using BCD(number) as opposed to your bitread(number) because I want it to pick up the numbers in the array BCD and send them to the pins 4,5,6 and 7.

The method I propose does the same same thing except I don't have to have an array. Trace through it.

When working with values 0-9 and 4 bits, BCD and binary are the same thing.

Decimal 3
BCD: 0011
Binary: 0011

When you get to a two digit decimal numbers and represent each digit in an 8 bit byte then there is a difference.

Decimal: 34
BCD: 0011 0100
Binary: 0010 0010

After three decades of knowing about this chip, you now make me wonder why they call it BCD. With only 4 bits in, the BCD doesn't really matter.

Try subtracting 0x30 from your received data before using it.

adwsystems:
When working with values 0-9 and 4 bits, BCD and binary are the same thing.

Key point. When using 7-segment displays, BCD only enters the equation for multi-digit displays. Another method of attack is to convert the value (>9) to a char array (itoa) and treat each entry as a single digit, masking the 4 lsb, effectively producing a BCD array.