Compare two arrays and print the current array if there is any change

I tried the below program but it didnt work. I want to get the CurrentInPin [4] array in serial monitor if there is any change batween PreviousinPin [4] array and CurrentInPin [4] array.

int IN1=4;
int IN2=5;
int IN3=6;
int IN4=7;
int PreviousInPin [4];
int CurrentInPin [4];
int i;
int j;
int new_sensorValue1;
int new_sensorValue2;
int new_sensorValue3;
int new_sensorValue4;

void setup() {
// put your setup code here, to run once:
Serial.begin (9600);
pinMode (4, INPUT_PULLUP);
pinMode (5, INPUT_PULLUP);
pinMode (6, INPUT_PULLUP);
pinMode (7, INPUT_PULLUP);

Serial.print ("PrevoiusInPin [4]=");
Serial.print ("{");

for (i=0; i<4; i++){

Serial.print (PreviousInPin );

  • if (i<3){*
  • Serial.print (",");*
  • }*
  • }*
  • Serial.print ("}");*
  • Serial.println (" ");*

}
void loop() {

  • // put your main code here, to run repeatedly:*
    int sensorValue1= digitalRead (IN1);
    int sensorValue2= digitalRead (IN2);
    int sensorValue3= digitalRead (IN3);
    int sensorValue4= digitalRead (IN4);
    int PreviousInPin[4] = {sensorValue1,sensorValue2 ,sensorValue3 ,sensorValue4};
    /*PreviousInPin[0]= sensorValue1;
    PreviousInPin[1]=sensorValue2;
    PreviousInPin[2]=sensorValue3;
    PreviousInPin[3]=sensorValue4;*/
    int CurrentInPin[4] = {new_sensorValue1,new_sensorValue2 ,new_sensorValue3 ,new_sensorValue4};
    /CurrentInPin[0]=new_sensorValue1;
    CurrentInPin[1]=new_sensorValue2;
    CurrentInPin[2]=new_sensorValue3;
    CurrentInPin[3]=new_sensorValue4;/

    if (PreviousInPin[4] != CurrentInPin[4] ){
    Serial.print ("CurrentInPin [4]=");
    Serial.print ("{");
    for (j=0; j<4; j++){
    Serial.print (CurrentInPin );
    if (j<3){
    Serial.print (",");
    }
    }
    Serial.print ("}");
    Serial.println (" ");
    CurrentInPin [4] = PreviousInPin [4];
    }
    delay (10);
    }

Please edit the post to put your code in code tags. See Forum rules.

You say “it didn’t work”. You need to give forum members more info including description of what happens, copies of error messages, etc.

I’m guessing your code doesn’t even compile based on how you declare global arrays more than once.

Review some references and tutorials on how to use arrays. (Creating, updating, and reading arrays are essential array activities you should be comfortable with before using them in a sketch).

Your code contains some major errors related to arrays. Using online tutorials and references, write some basic sketches to practice how arrays work first.

each time loop() runs the values of CurrentInPin [] are re-initialized to the new_sensorValues.

    int CurrentInPin[4] = {new_sensorValue1,new_sensorValue2 ,new_sensorValue3 ,new_sensorValue4};

this line changes the value of CurrentInPin [4], but it is lost the next time loop() runs.

        CurrentInPin [4] = PreviousInPin [4];

the new_sensorValues remain persistent between calls to loop() but are never changed. I see no need for them. Instead you can declare CurrentInPin [] globally outside of loop() where any changes in values will remain persistent.

int CurrentInPin [4] = {};

you will need to copy all the values from PreviousInPin[] to CurrentInPin [] when there is a change