Friday, June 7, 2013

Compare arrays in Arduino

Sample Code:


// Compare arrays

int array1[6] = {0,1,2,3,4,5}; // sample arrays
int array2[6] = {0,1,2,3,4,5};
boolean result;  // this variable will hold the compare result

void setup(){
     Serial.begin(9600); // stablish serial communication
  }
  
  
  
 void loop(){
  
  result = true; // set variable equal to one.
  for (int x=0; x<6; x++){ 
  int test1 = array1[x];// asign each index of arrays to test, one by one and compare
  int test2 = array2[x];
  
  
  if(test1 != test2){ // if any index comparison is not equal, then the arrays are not equal and result will be 0.
   result = false; 
  }
  
  }
  if(result == true){ // if the arrays are equal, do something.
  Serial.println(result); 
  }
  delay(100);
 }
 
 


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.