In a function show me error, 'x' was not declared in this scope

In an array byte, according to Packet[1] i want to:

if ((Packet[1] | 0x7F) == 0x7F)
{for (byte x = 2; x <= y; x++)
{ Packet[x]; Functions(); } //Show me packets from Packet[2] to y
}
else
if((Packet[1] & 0xC0) == 0xC0)
{for (byte x = 3; x <= y; x++)
{ Packet[x]; Functions(); } //Show me packets from Packet[3] to y
}

void Functions()
{
Serial.print(Packet[x], HEX);
Serial.print(" ");
Serial.println("End");
}

But it is not working.
It show me error here: Serial.print(Packet[x], HEX);

'x' was not declared in this scope

What can i do ?

I add
byte x;
before error and it is working .

billys7:
I add
byte x;
before error and it is working .

That's how you declare it!

Working? Compiling more like. You need to pass "x" to the function.

http://snippets-r-us.com/

I can't understand it!

Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the # button above the posting area.


I can't understand it!

What is this doing?

Packet[x]; Functions();

More like:

Functions(Packet[x]); 


...

void Functions(byte c)
 { 
       Serial.print(c, HEX);
       Serial.print(" ");
       Serial.println("End");
 }

You are right,
I thought that if i will copy all the code will be boring for someone to read it.

Thank you!

                  { Packet[x]; Functions(); }                  //Show me packets from Packet[2] to y

Your comment and your code do not match. What do you think this is doing?

It's equivalent to

{
   47;
   Functions();
}

which makes no sense, since Functions() (a stupid name, by the way) takes an argument and you are not supplying one.

billys7:
I thought that if i will copy all the code will be boring for someone to read it.

Not as boring as trying to guess what you are doing.

I have borrow this code. The original works fine, if you want to read the packets.
I am trying to partially extract some infos from the code

Here is the main code :

byte dccPacket[6]; // buffer to hold a packet - shared with A_dccGetPacket.ino
word Address;
word CV = 210;

void setup() {
  Serial.begin(115200);
  Serial.println("Starting ReadDccPackets.ino");
  
  pinMode(3, OUTPUT);  //Function 1
  pinMode(4, OUTPUT);  //Function 1
  pinMode(5, OUTPUT);  //Function 1
  pinMode(6, OUTPUT);  //Function 1
  pinMode(7, OUTPUT);  //Function 1
  pinMode(8, OUTPUT);  //Function 1
  //PWM :pin 9
  //CS: pin 10
  //MOSI: pin 11
  //MISO: pin 12
  //SCK: pin 13
  //Reverse : pin A0
  //Forward : pin A1
  pinMode(A2, OUTPUT);  //Function 1
  pinMode(A3, OUTPUT);  //Function 1
  pinMode(A4, OUTPUT);  //Function 1
  //BEMF : pin A5 
  
  beginBitDetection();
    
}

//====================

void loop() {
   getPacket();
   showValidPacket();
}
//=====================

void showValidPacket()
{  // this only shows packets with valid check sums
  byte pktByteCount = dccPacket[0];
  if (pktByteCount == 0) {
    return;
  }

  byte checksum = 0;
  for (byte n = 1; n <= pktByteCount; n++) {    //Starts from dccPacket[1] and increment till the No of the packets, pktByteCount
     checksum ^= dccPacket[n];                  //checksum (error detection byte) is the XOR of the dccPackets[1 - n]
  }
  if (checksum) {
//    Serial.println("Invalid Checksum");
    return;
  }
  else 
  {
    
  
   //--------------------------Get Address------------------------------------------
    if ((dccPacket[1] | 0x7F) == 0x7F)                             //Bitwise OR to read the Most Significant bit (first left) and if it is 0 
        { Address = dccPacket[1];}                                 //then it is Short Address - Baseline  CV1
       else                                                        //else
        {  if(((dccPacket[1] & 0xC0) == 0xC0)&&(dccPacket[1]!=255))//Bitwise AND to read if the first two bits (first left) are 11, and the remaining not 111111
              { Address = word(dccPacket[1]-192,dccPacket[2]); }   //then it is Extended Address CV17 - CV18
        }   //εδω μάλλον λειπει η εντολή που θα μας στείλει πάλι στην αρχή. Να δοκιμάσω και το switch - case.
                  
  //---------------------------End Address------------------------------------------          

  if (Address == CV)
  //--------------------------Get Data and Functions--------------------------------
    {
        if ((dccPacket[1] | 0x7F) == 0x7F)                          //Get Data Byte from dccPacket[2] to dccPacket[n-1]
            {for (byte n = 2; n < pktByteCount; n++) 
                  { Functions(dccPacket[n]); }
            }
               else                                               
        if(((dccPacket[1] & 0xC0) == 0xC0)&&(dccPacket[1]!=255))   //Get Data Byte from dccPacket[3] to dccPacket[n-1]    
              {for (byte n = 3; n < pktByteCount; n++)
                     { Functions(dccPacket[n]); }
              }

    }
  }
}
void Functions(byte n)
 {  
       {Serial.print(dccPacket[n], HEX);
       Serial.print(" ");}
       Serial.println("=End=");
 }


 
//=====================

Unfortunately i can't make it to work correct.

From the last part -----Get Data and Functions------- i want to read only the data bytes.
If the dccPacket[1] starts from 0 then data bytes are from dccPacket[2] to n-1,
If the dccPacket[1] starts from 11 then data bytes are from dccPacket[3] to n-1,

You didn't do what I suggested in reply #5.

if ((dccPacket[1] | 0x7F) == 0x7F)                          //Get Data Byte from dccPacket[2] to dccPacket[n-1]
            {for (byte n = 2; n < pktByteCount; n++) 
                  { Functions(dccPacket[n]); }
Serial.println("=End=");
            }
else                                               
        if(((dccPacket[1] & 0xC0) == 0xC0)&&(dccPacket[1]!=255))   //Get Data Byte from dccPacket[3] to dccPacket[n-1]    
              {for (byte n = 3; n < pktByteCount; n++)
                     { Functions(dccPacket[n]); }
Serial.println("=End=");
              }
void Functions(byte myByte)
 {  

       Serial.print(myByte, HEX);
       Serial.print(" ");
 }

Calling a function "Functions" is like calling a variable "Variables" and a constant "Constants".

Can't you think of a better name?

Ok. I understood it.

Thank you!