Function call another function : what's wrong ?

hi,

I do not understand what's wrong !!

[u]this works :[/u]

void Bit(int i)
{
if ((i&1)==1)
  digitalWrite(2,LOW);
else
  digitalWrite(2,HIGH);
if ((i>>1)&1==1)
  digitalWrite(3,LOW);
else
  digitalWrite(3,HIGH);
if ((i>>2)&1==1)
  digitalWrite(4,LOW);
else
  digitalWrite(4,HIGH);
if ((i>>3)&1==1)
  digitalWrite(5,LOW);
else
  digitalWrite(5,HIGH);
if ((i>>4)&1==1)
  digitalWrite(6,LOW);
else
  digitalWrite(6,HIGH);
if ((i>>5)&1==1)
  digitalWrite(7,LOW);
else
  digitalWrite(7,HIGH);
if ((i>>6)&1==1)
  digitalWrite(8,LOW);
else
  digitalWrite(8,HIGH);
if ((i>>7)&1==1)
  digitalWrite(9,LOW);
else
  digitalWrite(9,HIGH);
}

void loop() {
   
Bit(B00000000); // aucune led allumée
delay(10);

Bit(B10000000);// debut Y
delay(1);
Bit(B01000000);
delay(1);
Bit(B00100000);
delay(1);
Bit(B00011111);
delay(1);
Bit(B00100000);
delay(1);
Bit(B01000000);
delay(1);
Bit(B10000000);
delay(1); // Fin Y
}

[u]but if I try this, this will not work :[/u]

void Bit(int i)
{
if ((i&1)==1)
  digitalWrite(2,LOW);
else
  digitalWrite(2,HIGH);
if ((i>>1)&1==1)
  digitalWrite(3,LOW);
else
  digitalWrite(3,HIGH);
if ((i>>2)&1==1)
  digitalWrite(4,LOW);
else
  digitalWrite(4,HIGH);
if ((i>>3)&1==1)
  digitalWrite(5,LOW);
else
  digitalWrite(5,HIGH);
if ((i>>4)&1==1)
  digitalWrite(6,LOW);
else
  digitalWrite(6,HIGH);
if ((i>>5)&1==1)
  digitalWrite(7,LOW);
else
  digitalWrite(7,HIGH);
if ((i>>6)&1==1)
  digitalWrite(8,LOW);
else
  digitalWrite(8,HIGH);
if ((i>>7)&1==1)
  digitalWrite(9,LOW);
else
  digitalWrite(9,HIGH);
}

void Lettre_Y()
{
Bit(B10000000);// debut Y
delay(1);
Bit(B01000000);
delay(1);
Bit(B00100000);
delay(1);
Bit(B00011111);
delay(1);
Bit(B00100000);
delay(1);
Bit(B01000000);
delay(1);
Bit(B10000000);
delay(1); // Fin Y
}

void loop() {
Bit(B00000000); // espace
delay(3);   
Lettre_Y;
delay(500);
}

I don't understand why the 2nd version is not working, can someone help me ? thanks in advance.

Flop

Put code inside [code] [/code] to make it forum readable.
The edit window # button above the smileys makes [code] [/code] with 1 click.

void loop() {
Bit(B00000000); // espace
delay(3);   
Lettre_Y; // <<===== this looks like an undeclared variable, try Lettre_Y() instead
delay(500);
}

many thanks, you saved me a lot of time, I don't think I could have found this error !

this looks like an undeclared variable

no, it's a reference to a declared function. However, it isn't calling it, just testing that it is zero or non-zero, and discarding the result.(it is non-zero, because such a function exists) If you're lucky, the compiler will optimise this away.

Simplified, curious?

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

uint8_t     pins[] = { 2, 3, 4, 5, 6, 7, 8, 9 };

void Bit(int bits)
{
    for (int i = 0, mask = 0b10000000; mask; i++, mask >>= 1)
    {
        digitalWrite(pins[i], ((bits & mask) ? LOW : HIGH));
    }
}

void loop()
{
    Bit(0b00000000);    // aucune led allumée
    delay(10);
    
    Bit(0b10000000);    // debut Y
    delay(1);

    Bit(0b01000000);
    delay(1);

    Bit(0b00100000);
    delay(1);

    Bit(0b00011111);
    delay(1);

    Bit(0b00100000);
    delay(1);

    Bit(0b01000000);
    delay(1);

    Bit(0b10000000);
    delay(1);           // Fin Y
}

void setup()
{
    for ( int i = ENTRIES(pins); i--; )
    {
        pinMode(pins[i], OUTPUT);
    }
}

If he picked his pins with care then on an UNO 2 Port writes would cover all 8 bits.
With 1 8-bit shift register he could get away with using 4 pins (SPI) but just a tiny bit slower.