Hi everyone I am a newcomer to arduino programing,
Can any one tell me how two arrays are compared in arduino ide.
please help me,I am stuck here with this problem.
Hi everyone I am a newcomer to arduino programing,
Can any one tell me how two arrays are compared in arduino ide.
please help me,I am stuck here with this problem.
Can any one tell me how two arrays are compared in arduino ide.
The IDE is a development environment. It does not make much sense to be comparing arrays in an IDE.
If you want to compare them in code, then you need to compare each element in the arrays, one pair at a time.
Elaborate, please.
Are these simple, 1 dimension arrays?
for (int i = 0; i<max_size+1; i=i+1){
if (array1[i] == array2[i]){
  Serial.print ("element "); Serial.print (i); Serial.println (" matches");
  }
else {
  Serial.print ("element "); Serial.print (i); Serial.println (" does not match");
  }
}
or however you'd like to indicate the match/not-matchedness
Dear PoulS,
my project is an RC car
I need to take inputs using digitalRead command from 4 pins to an array,say inputs[],that i did.
now i need to compare the inputs[] with four arrays already defined
a={0,0,0,1}
b={0,0,1,0}
c={0,0,1,1}
c={0,1,0,0}
this four cases will executed for different directions of motion using H-bridge codes.
Please help me.
Thanks in advance
You might want to compress the four bits into one byte:
const int PinA = 2;
const int PinB = 3;
const int PinC = 5;
const int PinD = 2;
const byte a = 0b0001;
const byte b = 0b0010;
const byte c = 0b0011;
const byte d = 0b0100;
byte input = 0;
if (digitalRead(PinA)) input += 0b1000;
if (digitalRead(PinB)) input += 0b0100;
if (digitalRead(PinC)) input += 0b0010;
if (digitalRead(PinD)) input += 0b0001;
switch (input)
  {
case a:
  // Put code for pattern 'a' here
  break;
case b:
  // Put code for pattern 'b' here
  break;
case c:
  // Put code for pattern 'c' here
  break;
case d:
  // Put code for pattern 'd' here
  break;
default:
  // Error condition! Did not match any of the patterns
 }
That is way too complicated.
You have no need to use an array at all.
At a very simple level just read your inputs into variables and compare each variable in a compound if statement.
This code fragment shows you what to do:-
int pin2 = digitalRead(2);
int pin3 = digitalRead(3);
int pin4 = digitalRead(4);
int pin5 = digitalRead(5);
if(pin2 == HIGH && pin3 == LOW && pin4 == LOW && pin5 == LOW) { // now do condition a
if(pin2 == LOW && pin3 == HIGH && pin4 == LOW && pin5 == LOW) { // now do condition b
// .........and so on
If you want to get sophisticated then make each input set / clear a bit in a byte and just use one compare, see the bitSet and bitClear operations.
What the folks above told you is true. In your case there are better ways to do what you're trying to do. However for future reference there is a simple way to compare two arrays OF THE SAME TYPE AND SIZE and that is to use the memcmp() function which compares two blocks of memory and returns an integer greater than 0 if the first block is greater than the second, returns 0 if the two blocks are the same, and an integer less than 0 if the first block is less than the second.
The prototype for the function is:
int memcmp(const void *s1, const void *s2, size_t n); where s1 is a pointer to the first block of memory, s2 is a pointer to the second block, and n is the number of bytes to compare.
To use the function your program needs to #include "string.h"
Example follows:
#include "string.h"
int array1[5];
int array2[5]; // the arrays must have the same number of elements. Could be 5, could be 50
//... your code here to put values into the arrays
// compare the arrays (cast the data type to const void * )
if ( memcmp( (const void *)array1, (const void *)array2, sizeof(array1)) == 0)
{
  // if the arrays are equal execute the code here
  // ...
}