Pages: [1]   Go Down
Author Topic: comparing two byte arrays with a custom made progr  (Read 1124 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 49
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here's my code
Code:

byte array1[]={0x00,0x02,0xF6};
byte array2[]={0x00,0x02,0xF6};

void setup()
  pinMode(13,OUTPUT);
   digitalWrite(13,LOW);
   delay(1000);
   digitalWrite(13,HIGH);
   delay(1000);
    digitalWrite(13,LOW);
   delay(1000);
   digitalWrite(13,HIGH);
   delay(1000);
    digitalWrite(13,LOW);
    delay(1000);
   digitalWrite(13,HIGH);
   delay(1000);
    digitalWrite(13,LOW);


void loop()
{
if(ByteArrayCompare(array1,array2))
{
   digitalWrite(13,HIGH);
}
else
{
  digitalWrite(13,LOW);
}
 


}


boolean ByteArrayCompare(byte *a, byte *b)
{
   int e;
   #define countof(a) (sizeof(a) / sizeof(a[0]))
   #define countof(b) (sizeof(b) / sizeof(b[0]))

  if((sizeof(a)==sizeof(b)) && (countof(a)==countof(b)))
 {
   goto Arrayloop1;
 }
 else
 {
   return false;
 }
 Arrayloop1:
   for(int f=0;f<countof(b);f++)
   {
     if((a[f]==b[f]))
     {
       e++;
     }
     else
     {
        return false;  
     }
   }
   if(e==countof(b))
     {
       return true;
     }
     else
     {
       return false;
     }    
}






I don't understand where my mistake is, logically for me everything's fine so please help :'(
No matter what I do the led is constantly OFF no matter if I change the number of the elements in the arrays or change their values.
« Last Edit: August 01, 2010, 09:02:17 am by markov85 » Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46256
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
goto Arrayloop1;
This statement is completely unnecessary. Change the program logic to remove it.

Code:
  #define countof(a) (sizeof(a) / sizeof(a[0]))
   #define countof(b) (sizeof(b) / sizeof(b[0]))

There are issue with the processing of preprocessor directives if they appear after executable code. Move these to the top of the sketch. Also, explain why you need two identical functions named countof.

If the two arrays are not the same size, the you have already decided that they are not equal, so the for loop is not executed. In the for loop, you return whenever the arrays do not contain the same value. Why is it necessary to count the number of bytes that are equal?

Quote
I don't understand where my mistake is, logically for me everything's fine so please help
.
What leads you to believe that there is a mistake? Does the LED not turn on when it should?
Logged

Huntsville, Alabama, USA
Offline Offline
Sr. Member
****
Karma: 2
Posts: 327
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

markov,

There are several problems in this program:

1. A function cannot determine the size of an array passed to it with sizeof().  As you are using it, it will return the sizes of the array pointers, which will always be the same, no matter what size the arrays are.  If a function needs to know how long an array is, the usual way is to pass the array length to the function.

2. What is the value of 'e' when you enter the for loop?

3. You are defining a macro (with #define) that has parameters.  Why are you defining it twice?  The 'a' or 'b' is just a placeholder for the parameter passed in when the macro is invoked.  At any rate, since the sizeof is not working the way you think it is, the macro is useless inside ByeArrayCompare().

4. Do yourself a favor and write your programs without goto's.

Here's another way to write the function:
Code:
boolean ByteArrayCompare(byte a[],byte byte b[],int array_size)
{
    for (int i = 0; i < array_size; ++i)
      if (a[i] != b[i])
        return(false);
    return(true);
}
I hope this information helps.

Regards,

-Mike
« Last Edit: August 01, 2010, 09:10:09 am by mfm9 » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 49
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

We'll I rewrote my code like this:
Code:

byte array1[]={0x00,0x02,0xF1,0x10,0x01};
byte array2[]={0x00,0x02,0xF6,0x01};

void setup()
  pinMode(13,OUTPUT);
   digitalWrite(13,LOW);
   delay(1000);
   digitalWrite(13,HIGH);
   delay(1000);
    digitalWrite(13,LOW);
   delay(1000);
   digitalWrite(13,HIGH);
   delay(1000);
    digitalWrite(13,LOW);
    delay(1000);
   digitalWrite(13,HIGH);
   delay(1000);
    digitalWrite(13,LOW);

void loop()
{
 if(ByteArrayCompare(array1,array2))
{
   digitalWrite(13,HIGH);
}
else
{
  digitalWrite(13,LOW);
}
 
}


boolean ByteArrayCompare(byte *a, byte *b)
{
   int e=0;
   int sizeofa = sizeof(a);
   int sizeofb = sizeof(b);
   int countofa = (sizeof(a)/sizeof(a[0]));
   int countofb = (sizeof(b)/sizeof(b[0]));
   lcd.setCursor(0,1);
   lcd.print(countofa);
   lcd.write(0);// This is a clear character/blank character so I can   //erase specific blocks of the LCD.
   lcd.print(countofb);

  if(sizeofa==sizeofb && countofa==countofb)
 {
    for(int f=0;f<countofb;f++)
   {
     if((a[f]==b[f]))
     {
       e++;
     }
     else
     {
        return (false);  
     }
   }
   if(e==countofa && e==countofb)
     {
       return (true);
     }
     else
     {
       return (false);
     }    
 }
 else
 {
   return (false);
 }
 
  
}



The led is still turning on (which indicates that both of the arrays are the same, even that there are not) and the thing that I cannot undestand is why on the lcd screen i see countofa= "2" countofb="2"
I mean why the arrays are only two bytes they have to be more.
Every element of the array is byte.
I saw this command in a post a couple of days ago about how to calculate the number of elements in the array and I just coppied it and made minor modifications for my use.
Becasue I had two different byte arrays named a and b
i thought that I can use it in the syntaxis I wrote above  and I thought everything is OK, but now you tell me it's wrong, I am still a newbie so.. so as I understood this "selfmade" command "countof(something)" gives me the number of elements in an array
as far as I know sizeof determines the number of bytes in an array I can't figure out what's wrong?

The issue here is that I am going to read arrays from a serial so I need to know how much bytes is the code, how much elements it is, and what exaclty is the value of every element of that array, even more I have to compare that array to some other array that I have previously declared.So the bitch here is that I have to analyse the incoming byte/bytes from the serial and compare them.

« Last Edit: August 01, 2010, 10:38:12 am by markov85 » Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46256
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As Mike pointed out, you can not determine the size of the array inside the function. That needs to be an input to the function.

The loop() function knows about the two specific arrays to be compared. It can determine the size of the arrays.

The ByteArrayCompare function is given a pointer to the array. The sizeof(a) call returns the size of the pointer, not the size of the thing pointed to. The sizeofa and sizeofb variables contain the same value.

The countofa and countofb variables also contain the same value. So, the function assumes that the arrays are the same size, and that they are two elements long.

Since the first two elements match, the arrays are equal.

If you pass countofa and countofb to the ByteArrayCompare function, you have a better chance of getting good output.

[edit]However, you are still doing plenty of unnecessary work in the function. If the arrays do not contain the same number of values, they do not match. If any one position in the arrays does not contain the same value, the arrays do not match. Unless you need to know which byte differs, the e variable is useless.[/edit]
« Last Edit: August 01, 2010, 11:04:00 am by PaulS » Logged

Pages: [1]   Go Up
Jump to: