Problem with arduino 7 segments decoder

HI, I am using a 7 segments display plus an arduino, I wanna make a decoder without the IC chip (7447, etc), only using the arduino, a dipswitch and the 7 segments. I have 4 inputs (binary code) and 7 outputs. The problme is that I have "finished" my code but it does not work. I have 2 days reseraching in google and triying to solve why the display doesn´t change when I move the dipswith inputs. Somebody know why this happen?

Here is my code:

byte pinesI[4] = {13,12,11,10};
int pinesO[8] = {22,23,24,25,26,27,28,29};
byte digitoleido;

void setup(){

for (int cont1=0 ; cont1 < 8 ; cont1++){
pinMode(pinesO[cont1],OUTPUT);
pinMode(pinesI[cont1],INPUT);
}

}
void display (int a, int b, int c, int d, int e, int f, int g, int dot)// Función del display
{
digitalWrite (22,a);
digitalWrite (23,b);
digitalWrite (24,c);
digitalWrite (25,d);
digitalWrite (26,e);
digitalWrite (27,f);
digitalWrite (28,g);
digitalWrite (29,dot);
}

void loop(){
pinesI[0] = digitalRead(13);
pinesI[1] = digitalRead(12);
pinesI[2] = digitalRead(11);
pinesI[3] = digitalRead(10);
digitoleido = pinesI[3] | pinesI[2] <<1 | pinesI[1]<< 1| pinesI[0] << 1 ;
while (digitoleido != 0 ){
switch( digitoleido) {

case 0:
display(0,0,0,0,0,0,1,1 );
break;
case 2:
display(1,0,0,1,1,1,1,1 );
break;
case 3:
display(0,0,1,0,0,1,0,1 );
break;
case 4:
display(0,0,0,0,1,1,0,1 );
break;
case 5:
display(1,0,0,1,1,0,0,1);
break;
case 0101:
display(0,1,0,0,1,0,0,1 );
break;
case 0110:
display(0,1,0,0,0,0,0,1 );
break;
case 0111:
display(0,0,0,1,1,1,1,1 );
break;
case 1001:
display( 0,0,0,0,0,0,0,1 );
break;
case 1010:
display(0,0,0,1,1,0,0,1 );
break;
case 1011:
display(1,0,0,1,1,1,1,1 );
break;
case 1100:
display(1,0,0,1,1,1,1,1 );
break;
case 1101:
display(1,0,0,1,1,1,1,1 );
break;

}
}
}

Yep, remove the while :wink:

triying to solve why the display doesn´t change when I move the dipswith inputs.

Probably because your switches are not wired correctly. You do have external pull-up or pull-down resistors, right? How ARE they wired?

I have already remove the while, but now the problem is the same: the digits in binary that I put in the dipswitch does not match with the 7 segments...

I have connected one side of the the dipswitch with vcc and the other side th inputs form the arduino and the 330 ohms resistors to GND

I probably think that the error is in digitoleido = pinesI[3] | pinesI[2] <<1 | pinesI[1]<< 1| pinesI[0] << 1 ;

digitoleido is a byte variable, and I am only using 4 bits, i think that the bits manipulation with << is where I am bad. But I reaaly dont know why

byte for bitshift is just fine. But you only shift them 1 place...

And first you fill pinesI with the pinnumers and later you use it to store the read value..

And the cases 0101 and further are not okay.

Also, you make a nice variable for the display but then you hard code the pins in the display function....

Try:

byte pinesI[4] = {13,12,11,10};
int pinesO[8] = {22,23,24,25,26,27,28,29};
byte digitoleido;
const byte DigitMap[] = {0x11111100, 0x01100000, 0x11011010, 0x11110010, 0x01100110, 0x1011011, 0x01111110, 11100000, 0x11111110, 0x11110110};


void setup(){
 
  for (byte i = 0 ; i < 8 ; i++){
    pinMode(pinesO[i],OUTPUT);
    if(i < 4){
      pinMode(pinesI[i],INPUT);
    }
  }
  
 
 
}
void display (byte num)// Función del display
{
  byte shift = DigitMap[num];
  
  for(byte i = 0; i < 8; i++){
    digitalWrite(pinesO[7 - i], shift & 0x01);
    shift >>= 1;
  }
}
 
       

void loop(){
  digitoleido = digitalRead(pinesI[3]) | digitalRead(pinesI[2]) <<1 | digitalRead(pinesI[1])<< 2 | digitalRead(pinesI[0]) << 3 ;
  display(digitoleido);
}

Thanks Septillion!, but the problem is still the same but woth other combinationss of iputs in the dipswitch, the digits that I iput are diferents that the seven segments show. For example, Right now i have 0100 in the dip, an the seven segments shows the number 4. All the inputs and ouputs from the arduino are connected.

You have made quite a lot of changes. Please post the new code.

Please read the two posts at the top of this Forum by Nick Gammon on guidelines for posting here, especially the use of code tags which make the code look like thiswhen posting source code files. Also, before posting the code, use Ctrl-T in the IDE to reformat the code in a standard format, which makes it easier for us to read.

Taking all the above suggestions and you end up with something along the lines of -

// <https://forum.arduino.cc/index.php?topic=348807.0>

#define ELEMENT_COUNT(ARRAY) (sizeof(ARRAY) / sizeof(ARRAY[0]))

const uint8_t   pinDIP_SWITCH_0001  = 10;
const uint8_t   pinDIP_SWITCH_0010  = 11;
const uint8_t   pinDIP_SWITCH_0100  = 12;
const uint8_t   pinDIP_SWITCH_1000  = 13;

const uint8_t   pinSEG_A            = 22;
const uint8_t   pinSEG_B            = 23;
const uint8_t   pinSEG_C            = 24;
const uint8_t   pinSEG_D            = 25;
const uint8_t   pinSEG_E            = 26;
const uint8_t   pinSEG_F            = 27;
const uint8_t   pinSEG_G            = 28;
const uint8_t   pinSEG_DP           = 29;

const int       SEG_OFF             = HIGH;
const int       SEG_ON              = LOW;

const uint8_t   pinDIP_SWITCHES[]   =
{
      pinDIP_SWITCH_0001, pinDIP_SWITCH_0010, pinDIP_SWITCH_0100, pinDIP_SWITCH_1000
};

const uint8_t   pinSEGMENT_LEDS[]   =
{
      pinSEG_A, pinSEG_B, pinSEG_C, pinSEG_D, pinSEG_E, pinSEG_F, pinSEG_G
    , pinSEG_DP
};

const uint8_t   a_segment_bits[]    =
{
    //           D
    //  ABCDEFGP
      0b11111100    // 0    ABCDEFgp
    , 0b01100000    // 1    aBCdefgp
    , 0b11011010    // 2    ABcDEfGp
    , 0b11110010    // 3    ABCDefGp
    , 0b01100110    // 4    aBCdeFGp
    , 0b10110110    // 5    AbCDeFGp
    , 0b10111110    // 6    AbCDEFGp
    , 0b11100000    // 7    ABCdefgp
    , 0b11111110    // 8    ABCDEFGp
    , 0b11100110    // 9    ABCdeFGp
    // HEX ALPHA
    , 0b01100000    // A    ABCdEFGp
    , 0b01100000    // B    avCDEFgp
    , 0b10011110    // C    AbcDEFgp
    , 0b01111010    // D    aBCDEfgp
    , 0b10011110    // E    AbcDEFGp
    , 0b10001110    // F    AbcdEFGp
};

void display(uint8_t segment_bits)
{
    // SET SEGMENTS IN ORDER - DP, G, F, E, D, C, B, A
    // LSB -> MSB OF segment_bits

    for ( size_t i = ELEMENT_COUNT(pinSEGMENT_LEDS); i--; segment_bits >>= 1)
    {
        const uint8_t   pin   = pinSEGMENT_LEDS[i];
        const int       state = ((segment_bits & 1) ? SEG_ON : SEG_OFF);
        digitalWrite(pin, state);
    }
}

void loop()
{ 
    // SET SELECTION FROM MSB -> LSB
    size_t selector = 0;
    
    for ( size_t i = ELEMENT_COUNT(pinDIP_SWITCHES); i--; )
    {
        selector |= ((digitalRead(pinDIP_SWITCHES[i]) ? 1 : 0) << i);
    }

    display(a_segment_bits[selector]);
}

void setup()
{
    // SET INPUT PINS
    for ( size_t i = ELEMENT_COUNT(pinDIP_SWITCHES); i--; )
    {
        pinMode(pinDIP_SWITCHES[i], INPUT);
    }

    // SET OUTPUT PINS
    for ( size_t i = ELEMENT_COUNT(pinSEGMENT_LEDS); i--; )
    {
        pinMode(pinSEGMENT_LEDS[i], OUTPUT);
    }
}

@JuanTono, I don't see a problem... 0100 is the binary representation of 4... So what did you expect it to display?

I'm obviously missing something. It is not obvious to me what the OP is trying to do and I cannot visualize a circuit diagram that makes sense with the code he has posted. It seems to me that at best he can only display one 7 segment digit at a time since I see no multiplexing which is normally needed with 7 segment displays. The OP needs to post his circuit diagram and post a better description of his project expectations.

stowite:
since I see no multiplexing which is normally needed with 7 segment displays.

Yes, if you want to drive more then 1 segment... But I don't think the OP wants to drive more then 1 segment so the code made kind of sens.

stowite:
The OP needs to post his circuit diagram and post a better description of his project expectations.

True, diagram is always better.

septillion:
Yes, if you want to drive more then 1 segment... But I don't think the OP wants to drive more then 1 segment so the code made kind of sens.

Not to me! If, as I understand it, he has 4 switches one for each common (anode or cathode) of each digit of the 4 digit 7 segment displays then the code makes no sense. Of course without a circuit diagram and a better understanding of the OP's expectations I am only guessing.

That's why I said, kind of :wink:

In my opinion he has 1 digit. All segements connected to the Arduino and just 4 dip switches connected to the Arduino as well to set a number. Exactly what a 7447 (to which he refers) can do.

digitoleido = pinesI[3] | pinesI[2] <<1 | pinesI[1]<< 1| pinesI[0] << 1 ;

Use this:

digitoleido = pinesI[3] | pinesI[2] <<1 | pinesI[1]<< 2| pinesI[0] << 3 ;

Or this (opposite bit order -- which way are your inputs arranged):

digitoleido = pinesI[3]<<3 | pinesI[2] <<2 | pinesI[1]<< 1| pinesI[0];

Or this (same as my first one, but with arithmetic instead of bit shifts):

digitoleido = pinesI[3] + (pinesI[2]*2) + (pinesI[1]*4) + (pinesI[0]*8);